diff --git a/Home.md b/Home.md index 5d08b7b..f05ebc9 100644 --- a/Home.md +++ b/Home.md @@ -1 +1,102 @@ -Welcome to the Wiki. \ No newline at end of file +persist + +# persist + +## Table of contents + +### Classes + +- [Persist](classes/Persist.md) + +### Decorators + +- [persist](Home.md#persist) + +### Options + +- [PersistOptions](Home.md#persistoptions) + +## Decorators + +### persist + +▸ **persist**\<`T`\>(`options?`): (`target`: `any`, `prop`: `any`) => `void` + +Sync class property with persistent storage (LocalStorage by default) + +#### Type parameters + +| Name | +| :------ | +| `T` | + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `options?` | [`PersistOptions`](Home.md#persistoptions)\<`T`\> & \{ `key?`: `string` } | Configure using [PersistOptions](Home.md#persistoptions) | + +#### Returns + +`fn` + +Decorator function + +▸ (`target`, `prop`): `void` + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `target` | `any` | +| `prop` | `any` | + +##### Returns + +`void` + +**`Example`** + +```ts +class ThemeEngine { + @persist({default: 'os'}) current!: string; +} + +const theme = new ThemeEngine(); +console.log(theme.current) // Output: os + +theme.current = 'light'; //Any changes will be automatically saved to localStorage + +location.reload(); // Simulate refresh +console.log(theme.current) // Output: light +``` + +#### Defined in + +persist.ts:135 + +## Options + +### PersistOptions + +Ƭ **PersistOptions**\<`T`\>: `Object` + +Configurations persistence behaviour + +#### Type parameters + +| Name | +| :------ | +| `T` | + +#### Type declaration + +| Name | Type | Description | +| :------ | :------ | :------ | +| `default?` | `T` | Default/Initial value if undefined | +| `storage?` | `Storage` | Storage implementation, defaults to LocalStorage | +| `type?` | `any` | Force value to have prototype | + +#### Defined in + +persist.ts:6 diff --git a/classes/Persist.md b/classes/Persist.md new file mode 100644 index 0000000..e0e5ef2 --- /dev/null +++ b/classes/Persist.md @@ -0,0 +1,308 @@ +[persist](../Home.md) / Persist + +# Class: Persist\ + +Sync variable's value with persistent storage (LocalStorage by default) + +**`Example`** + +```ts +const theme = new Persist('theme.current', {default: 'os'}); +console.log(theme.value); // Output: os + +theme.value = 'light'; // Any changes to `.value` will automatically sync with localStorage + +location.reload(); // Simulate refresh +console.log(theme.value); // Output: light +``` + +## Type parameters + +| Name | +| :------ | +| `T` | + +## Table of contents + +### Constructors + +- [constructor](Persist.md#constructor) + +### Properties + +- [\_value](Persist.md#_value) +- [key](Persist.md#key) +- [options](Persist.md#options) +- [storage](Persist.md#storage) +- [watches](Persist.md#watches) + +### Accessors + +- [value](Persist.md#value) + +### Methods + +- [clear](Persist.md#clear) +- [load](Persist.md#load) +- [notify](Persist.md#notify) +- [save](Persist.md#save) +- [toString](Persist.md#tostring) +- [valueOf](Persist.md#valueof) +- [watch](Persist.md#watch) + +## Constructors + +### constructor + +• **new Persist**\<`T`\>(`key`, `options?`): [`Persist`](Persist.md)\<`T`\> + +#### Type parameters + +| Name | +| :------ | +| `T` | + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `key` | `string` | Unique key value will be stored under | +| `options` | [`PersistOptions`](../Home.md#persistoptions)\<`T`\> | Configure using [PersistOptions](../Home.md#persistoptions) | + +#### Returns + +[`Persist`](Persist.md)\<`T`\> + +#### Defined in + +persist.ts:68 + +## Properties + +### \_value + +• `Private` **\_value**: `T` + +Private value field + +#### Defined in + +persist.ts:31 + +___ + +### key + +• `Readonly` **key**: `string` + +Unique key value will be stored under + +#### Defined in + +persist.ts:68 + +___ + +### options + +• **options**: [`PersistOptions`](../Home.md#persistoptions)\<`T`\> = `{}` + +Configure using [PersistOptions](../Home.md#persistoptions) + +#### Defined in + +persist.ts:68 + +___ + +### storage + +• `Private` `Readonly` **storage**: `Storage` + +Where data gets physically stored + +#### Defined in + +persist.ts:60 + +___ + +### watches + +• `Private` **watches**: `Object` = `{}` + +Listeners which should be notified on changes + +#### Index signature + +▪ [key: `string`]: `Function` + +#### Defined in + +persist.ts:62 + +## Accessors + +### value + +• `get` **value**(): `T` + +Current value or default if undefined + +#### Returns + +`T` + +#### Defined in + +persist.ts:34 + +• `set` **value**(`v`): `void` + +Set value with proxy object wrapper to sync future changes + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `v` | `undefined` \| `T` | + +#### Returns + +`void` + +#### Defined in + +persist.ts:37 + +## Methods + +### clear + +▸ **clear**(): `void` + +Delete value from storage + +#### Returns + +`void` + +#### Defined in + +persist.ts:74 + +___ + +### load + +▸ **load**(): `void` + +Load value from storage + +#### Returns + +`void` + +#### Defined in + +persist.ts:86 + +___ + +### notify + +▸ **notify**(`value`): `void` + +Notify listeners of change + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `value` | `T` | + +#### Returns + +`void` + +#### Defined in + +persist.ts:108 + +___ + +### save + +▸ **save**(): `void` + +Save current value to storage + +#### Returns + +`void` + +#### Defined in + +persist.ts:79 + +___ + +### toString + +▸ **toString**(): `string` + +Return value as JSON string + +#### Returns + +`string` + +#### Defined in + +persist.ts:102 + +___ + +### valueOf + +▸ **valueOf**(): `undefined` \| `Object` + +Return raw value + +#### Returns + +`undefined` \| `Object` + +#### Defined in + +persist.ts:105 + +___ + +### watch + +▸ **watch**(`fn`): () => `void` + +Callback to listen for changes + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `fn` | (`value`: `T`) => `any` | + +#### Returns + +`fn` + +▸ (): `void` + +Callback to listen for changes + +##### Returns + +`void` + +#### Defined in + +persist.ts:95