diff --git a/src/Gang/Gang.ts b/src/Gang/Gang.ts index c6b069da4..1b4954147 100644 --- a/src/Gang/Gang.ts +++ b/src/Gang/Gang.ts @@ -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 | 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; } } diff --git a/src/NetscriptFunctions/Gang.ts b/src/NetscriptFunctions/Gang.ts index bdbce7e63..c868bd457 100644 --- a/src/NetscriptFunctions/Gang.ts +++ b/src/NetscriptFunctions/Gang.ts @@ -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 { return Math.round(gang.storedCycles / 5) * 1000; }, nextUpdate: () => () => { - return new Promise((res) => GangResolvers.push(res)); + if (!GangNextUpdate.promise) { + GangNextUpdate.promise = new Promise((res) => (GangNextUpdate.resolver = res)); + } + return GangNextUpdate.promise; }, }; }