GANG: Reuse one promise for ns.gang.nextUpdate() (#1068)

This commit is contained in:
LJ
2024-01-27 15:25:30 -07:00
committed by GitHub
parent 05295598a4
commit b6b4788845
2 changed files with 12 additions and 6 deletions

View File

@ -26,7 +26,8 @@ import { PowerMultiplier } from "./data/power";
import { FactionName } from "@enums";
import { CONSTANTS } from "../Constants";
export const GangResolvers: ((msProcessed: number) => void)[] = [];
type NextUpdateData = { resolver: ((msProcessed: number) => void) | null; promise: Promise<number> | null };
export const GangNextUpdate: NextUpdateData = { resolver: null, promise: null };
export class Gang {
facName: FactionName;
@ -106,9 +107,11 @@ export class Gang {
console.error(`Exception caught when processing Gang: ${e}`);
}
// Handle "nextUpdate" resolvers after this update
for (const resolve of GangResolvers.splice(0)) {
resolve(cycles * CONSTANTS.MilliPerCycle);
// Handle "nextUpdate" resolver after this update
if (GangNextUpdate.resolver) {
GangNextUpdate.resolver(cycles * CONSTANTS.MilliPerCycle);
GangNextUpdate.resolver = null;
GangNextUpdate.promise = null;
}
}

View File

@ -4,7 +4,7 @@ import type { GangMember } from "../Gang/GangMember";
import type { GangMemberTask } from "../Gang/GangMemberTask";
import type { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
import { GangResolvers } from "../Gang/Gang";
import { GangNextUpdate } from "../Gang/Gang";
import { Player } from "@player";
import { FactionName } from "@enums";
import { GangConstants } from "../Gang/data/Constants";
@ -325,7 +325,10 @@ export function NetscriptGang(): InternalAPI<IGang> {
return Math.round(gang.storedCycles / 5) * 1000;
},
nextUpdate: () => () => {
return new Promise<number>((res) => GangResolvers.push(res));
if (!GangNextUpdate.promise) {
GangNextUpdate.promise = new Promise<number>((res) => (GangNextUpdate.resolver = res));
}
return GangNextUpdate.promise;
},
};
}