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

This commit is contained in:
2023-12-27 17:23:17 -05:00
parent 039b393eeb
commit 9757b7aaca
5 changed files with 188 additions and 48 deletions

View File

@ -1,5 +1,5 @@
/**
* Configurations persistence behaviour
* Configurable options to change persistence behavior
*
* @group Options
*/
@ -8,7 +8,7 @@ export type PersistOptions<T> = {
default?: T,
/** Storage implementation, defaults to LocalStorage */
storage?: Storage,
/** Force value to have prototype */
/** Force value to have [proto]type */
type?: any,
}
@ -27,12 +27,15 @@ export type PersistOptions<T> = {
* ```
*/
export class Persist<T> {
/** Backend service to store data, must implement `Storage` interface */
private readonly storage!: Storage;
/** Listeners which should be notified on changes */
private watches: { [key: string]: Function } = {};
/** Private value field */
private _value!: T;
/** Current value or default if undefined */
get value(): T { return this._value !== undefined ? this._value : <T>this.options?.default; }
/** Set value with proxy object wrapper to sync future changes */
set value(v: T | undefined) {
if(v == null || typeof v != 'object') this._value = <T>v;
@ -56,13 +59,8 @@ export class Persist<T> {
this.save();
}
/** Where data gets physically stored */
private readonly storage!: Storage;
/** Listeners which should be notified on changes */
private watches: { [key: string]: Function } = {};
/**
* @param {string} key Unique key value will be stored under
* @param {string} key Primary key value will be stored under
* @param {PersistOptions<T>} options Configure using {@link PersistOptions}
*/
constructor(public readonly key: string, public options: PersistOptions<T> = {}) {
@ -101,8 +99,8 @@ export class Persist<T> {
/** Return value as JSON string */
toString() { return JSON.stringify(this.value); }
/** Return raw value */
valueOf() { return this.value?.valueOf(); }
/** Return current value */
valueOf() { return this.value; }
/** Notify listeners of change */
private notify(value: T) {