mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2025-03-07 19:14:37 +01:00
GANG: Reuse one promise for ns.gang.nextUpdate() (#1068)
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user