Files
eewing cf73cd3b4c INIT
2026-02-17 14:10:16 -06:00

26 lines
666 B
JavaScript

/**
* Holds the previous value of a getter.
*
* @see {@link https://runed.dev/docs/utilities/previous}
*/
export class Previous {
#previousCallback = () => undefined;
#previous = $derived.by(() => this.#previousCallback());
constructor(getter, initialValue) {
let actualPrevious = undefined;
if (initialValue !== undefined)
actualPrevious = initialValue;
this.#previousCallback = () => {
try {
return actualPrevious;
}
finally {
actualPrevious = getter();
}
};
}
get current() {
return this.#previous;
}
}