GRAFTING: Add new api for checking ongoing grafting (#1435)

This commit is contained in:
catloversg
2024-06-29 09:59:18 +07:00
committed by GitHub
parent 32eb6324fd
commit b1c1fc24a9
10 changed files with 97 additions and 10 deletions
+21 -2
View File
@@ -10,6 +10,8 @@ import { dialogBoxCreate } from "../ui/React/DialogBox";
import { constructorsForReviver, Generic_toJSON, Generic_fromJSON, IReviverValue } from "../utils/JSONReviver";
import { GraftableAugmentation } from "../PersonObjects/Grafting/GraftableAugmentation";
import { Augmentations } from "../Augmentation/Augmentations";
import { PromisePair } from "../Types/Promises";
import { getKeyList } from "../utils/helpers/getKeyList";
export const isGraftingWork = (w: Work | null): w is GraftingWork => w !== null && w.type === WorkType.GRAFTING;
@@ -22,6 +24,14 @@ export class GraftingWork extends Work {
augmentation: AugmentationName;
unitCompleted: number;
unitRate: number;
completionPromisePair: PromisePair<void> = { promise: null, resolve: null };
get completion(): Promise<void> {
if (!this.completionPromisePair.promise) {
this.completionPromisePair.promise = new Promise((r) => (this.completionPromisePair.resolve = r));
}
return this.completionPromisePair.promise;
}
constructor(params?: GraftingWorkParams) {
super(WorkType.GRAFTING, params?.singularity ?? true);
@@ -79,6 +89,12 @@ export class GraftingWork extends Work {
(CONSTANTS.IntelligenceGraftBaseExpGain * this.cyclesWorked * CONSTANTS.MilliPerCycle) / 10000,
);
}
if (this.completionPromisePair.resolve) {
this.completionPromisePair.resolve();
this.completionPromisePair.resolve = null;
this.completionPromisePair.promise = null;
}
}
APICopy() {
@@ -86,17 +102,20 @@ export class GraftingWork extends Work {
type: WorkType.GRAFTING as const,
cyclesWorked: this.cyclesWorked,
augmentation: this.augmentation,
completion: this.completion,
};
}
static savedKeys = getKeyList(GraftingWork, { removedKeys: ["completionPromisePair"] });
/** Serialize the current object to a JSON save state. */
toJSON(): IReviverValue {
return Generic_toJSON("GraftingWork", this);
return Generic_toJSON("GraftingWork", this, GraftingWork.savedKeys);
}
/** Initializes a GraftingWork object from a JSON save state. */
static fromJSON(value: IReviverValue): GraftingWork {
return Generic_fromJSON(GraftingWork, value.data);
return Generic_fromJSON(GraftingWork, value.data, GraftingWork.savedKeys);
}
}