tsuite
Version:
A collection of useful utility functions, All fully typed and documented
45 lines (44 loc) • 1.22 kB
JavaScript
/**
* tsuite v0.7.2
* tijn.dev
* @license MIT
**/
//#region src/create-state.ts
var InternalState = class {
currentValue;
callbackfn;
constructor(initialValue, callbackfn) {
this.currentValue = initialValue;
this.callbackfn = callbackfn;
}
get() {
return this.currentValue;
}
set(newValue) {
const previousValue = this.currentValue;
this.currentValue = newValue;
this.callbackfn(this.currentValue, previousValue);
}
};
/**
* Creates a stateful value with an associated side-effect that runs
* whenever the state is updated.
*
* @template T The type of the state value.
* @param initialValue The initial value of the state.
* @param callbackfn A function to run with the new and (optionally) previous value after each update.
* @returns A tuple containing a getter and a setter for the state.
*
* @example
* const [getCount, setCount] = createState(0, (value, prev) => {
* console.log('Count changed from', prev, 'to', value);
* });
* setCount(1); // Logs: Count changed from 0 to 1
* console.log(getCount()); // 1
*/
function createState(initialValue, callbackfn) {
const state = new InternalState(initialValue, callbackfn);
return [state.get, state.set];
}
//#endregion
export { createState };