import { ControlledPromise } from "./controlledPromise"; import type { BlockerFn, DefaultLayerError, LayerKey, LayerState, } from "./types"; import { hashKey } from "./utils"; /** Distinguishes concurrent instances that share a key. */ let instanceCounter = 0; /** Runtime state and cancellation resources for one stack entry. */ export class Layer
{
readonly id: string;
readonly key: LayerKey;
readonly promise: ControlledPromise ;
constructor(opts: {
key: LayerKey;
payload: P;
index: number;
stackSize: number;
component?: unknown;
enteringDelay?: number;
exitingDelay?: number;
data?: D;
}) {
this.id = `${hashKey(opts.key)}#${++instanceCounter}`;
this.key = opts.key;
this.promise = new ControlledPromise {
return this.#state;
}
get blockers(): ReadonlySet )[]) {
if (!Object.is(patch[key], this.#state[key])) {
this.#state = { ...this.#state, ...patch };
return true;
}
}
return false;
}
setRunning(running: boolean): void {
this.setPartial({ actionStatus: running ? "running" : "idle" });
}
/** Cancel any in-flight `loadFn`; subsequent resolutions are ignored. */
abort(): void {
this.aborted = true;
this.abortController.abort();
}
resolve(response: R): void {
this.promise.resolve(response);
}
reject(error: E): void {
this.promise.reject(error);
}
}