Updated documentation
All checks were successful
Build / Build NPM Project (push) Successful in 44s
Build / Tag Version (push) Successful in 3s
Build / Publish (push) Successful in 11s

This commit is contained in:
2024-01-05 15:57:45 -05:00
parent 09bbfc0b75
commit a567e3782c
4 changed files with 66 additions and 13 deletions

View File

@ -94,17 +94,30 @@ export class Persist<T> {
} else this.value = this.options.default || <T>undefined;
}
/** Callback to listen for changes */
/**
* Callback function which is run when there are changes
*
* @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
* @returns {() => void} Function which will unsubscribe the watch/callback when called
*/
watch(fn: (value: T) => any): () => void {
const index = Object.keys(this.watches).length;
this.watches[index] = fn;
return () => { delete this.watches[index]; };
}
/** Return value as JSON string */
/**
* Return value as JSON string
*
* @returns {string} Stringified object as JSON
*/
toString() { return JSON.stringify(this.value); }
/** Return current value */
/**
* Return current value
*
* @returns {T} Current value
*/
valueOf() { return this.value; }
}