@telperion/reactive-storage 1.2.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.build-manifest.json +86 -0
- package/README.md +6 -6
- package/fesm/reactive-storage.mjs +639 -0
- package/fesm/reactive-storage.mjs.map +1 -0
- package/index.cjs +17 -8
- package/index.cjs.map +1 -0
- package/index.d.cts +6 -0
- package/index.d.ts +6 -5
- package/index.js +5 -6
- package/index.js.map +1 -1
- package/json-parse.cjs +5 -3
- package/json-parse.cjs.map +1 -0
- package/json-parse.d.cts +8 -0
- package/json-parse.d.ts +3 -1
- package/json-parse.js +2 -1
- package/json-parse.js.map +1 -1
- package/package.json +60 -31
- package/reactive-cookie-storage.cjs +69 -50
- package/reactive-cookie-storage.cjs.map +1 -0
- package/reactive-cookie-storage.d.cts +77 -0
- package/reactive-cookie-storage.d.ts +48 -40
- package/reactive-cookie-storage.js +60 -41
- package/reactive-cookie-storage.js.map +1 -1
- package/reactive-storage.cjs +168 -83
- package/reactive-storage.cjs.map +1 -0
- package/reactive-storage.d.cts +165 -0
- package/reactive-storage.d.ts +118 -37
- package/reactive-storage.js +128 -43
- package/reactive-storage.js.map +1 -1
- package/reactive-web-local-storage.cjs +30 -8
- package/reactive-web-local-storage.cjs.map +1 -0
- package/reactive-web-local-storage.d.cts +37 -0
- package/reactive-web-local-storage.d.ts +30 -5
- package/reactive-web-local-storage.js +27 -5
- package/reactive-web-local-storage.js.map +1 -1
- package/reactive-web-session-storage.cjs +24 -8
- package/reactive-web-session-storage.cjs.map +1 -0
- package/reactive-web-session-storage.d.cts +31 -0
- package/reactive-web-session-storage.d.ts +24 -5
- package/reactive-web-session-storage.js +21 -5
- package/reactive-web-session-storage.js.map +1 -1
- package/reactive-web-storage.cjs +42 -27
- package/reactive-web-storage.cjs.map +1 -0
- package/reactive-web-storage.d.cts +47 -0
- package/reactive-web-storage.d.ts +28 -10
- package/reactive-web-storage.js +33 -18
- package/reactive-web-storage.js.map +1 -1
- package/async-iterator.polyfill.cjs +0 -87
- package/async-iterator.polyfill.d.ts +0 -2
- package/async-iterator.polyfill.js +0 -86
- package/async-iterator.polyfill.js.map +0 -1
- package/regex-parser.cjs +0 -29
- package/regex-parser.d.ts +0 -11
- package/regex-parser.js +0 -27
- package/regex-parser.js.map +0 -1
package/reactive-storage.d.ts
CHANGED
|
@@ -1,20 +1,50 @@
|
|
|
1
|
-
import { Observable } from
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
|
|
2
3
|
declare const GET_ALL: unique symbol;
|
|
3
4
|
declare const CHANGES: unique symbol;
|
|
4
5
|
declare const SET: unique symbol;
|
|
5
6
|
declare const REMOVE: unique symbol;
|
|
6
|
-
|
|
7
|
+
/**
|
|
8
|
+
* A change to one store, as a storage implementation reports it to {@link ReactiveStorage}.
|
|
9
|
+
* `value` is the store's new content, or `undefined` when the store was removed.
|
|
10
|
+
*
|
|
11
|
+
* @template S - Store name type
|
|
12
|
+
*/
|
|
13
|
+
interface ReactiveStorageChangeEvent<S = string> {
|
|
7
14
|
storeName: S;
|
|
8
15
|
value: any;
|
|
9
16
|
}
|
|
10
17
|
/**
|
|
11
|
-
* Abstract base class
|
|
12
|
-
*
|
|
13
|
-
*
|
|
18
|
+
* Abstract base class of every reactive storage: the shared `get`/`set`/`patch`/`delete`/`push`/`drop` API.
|
|
19
|
+
*
|
|
20
|
+
* Data is organised in named **stores**: one JSON value per store name (one localStorage key, one cookie).
|
|
21
|
+
* Read and write inside a store with a dot-separated **key path** (`'profile.name'`); an empty key
|
|
22
|
+
* means the whole store.
|
|
23
|
+
*
|
|
24
|
+
* - `get()` returns an Observable that emits the current value right away and again whenever it changes.
|
|
25
|
+
* Repeats of the same value (`===`) are skipped, so a primitive at a path emits only when it changes.
|
|
26
|
+
* - Emitted values are deep-frozen: copy them before changing them.
|
|
27
|
+
* - Writes return a Promise that resolves once subscribers have been notified. Writes from every
|
|
28
|
+
* storage instance run one at a time, first in first out; one that takes longer than a second
|
|
29
|
+
* is rejected with a `TimeoutError`.
|
|
30
|
+
* - Only writes made through the same instance (or, for cookies, linked instances) are observed.
|
|
14
31
|
*
|
|
15
|
-
*
|
|
32
|
+
* Example usage:
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { ReactiveWebLocalStorage } from '@telperion/reactive-storage/reactive-web-local-storage';
|
|
35
|
+
*
|
|
36
|
+
* const storage = new ReactiveWebLocalStorage<'user' | 'settings'>('my-app');
|
|
37
|
+
*
|
|
38
|
+
* storage.get<string>('user', 'profile.name').subscribe((name) => console.log(name));
|
|
39
|
+
* // undefined
|
|
40
|
+
*
|
|
41
|
+
* await storage.set('user', 'profile.name', 'Ada');
|
|
42
|
+
* // Ada
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
16
46
|
*/
|
|
17
|
-
|
|
47
|
+
declare abstract class ReactiveStorage<S extends string = string> {
|
|
18
48
|
#private;
|
|
19
49
|
private static FifoGroup;
|
|
20
50
|
private static extractData;
|
|
@@ -29,56 +59,107 @@ export declare abstract class ReactiveStorage<S extends string = string> {
|
|
|
29
59
|
protected abstract [SET](store: S, value: any): Promise<void>;
|
|
30
60
|
protected abstract [REMOVE](store: S): Promise<void>;
|
|
31
61
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
62
|
+
* Observable of a store, or of one value inside it, that emits the current value and then every change.
|
|
63
|
+
*
|
|
64
|
+
* The value is `undefined` while the store or path does not exist. Emitted values are deep-frozen.
|
|
34
65
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
66
|
+
* Example usage:
|
|
67
|
+
* ```ts
|
|
68
|
+
* storage.get('user').subscribe((user) => console.log(user)); // whole store
|
|
69
|
+
* storage.get<string>('user', 'profile.name').subscribe(console.log); // one value
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @template T - Type of the value at `key`
|
|
73
|
+
* @param storeName - Store to read
|
|
74
|
+
* @param key - Dot-separated path inside the store; empty (default) for the whole store
|
|
75
|
+
* @returns Observable of the value, which never completes
|
|
39
76
|
*/
|
|
40
77
|
get<T = Record<string, any>>(storeName: S, key?: string): Observable<T>;
|
|
41
78
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
79
|
+
* Writes a value at a path, replacing what was there and creating missing parent objects.
|
|
80
|
+
*
|
|
81
|
+
* The value is stored as JSON, so it must be JSON-serializable. Passing `undefined` removes
|
|
82
|
+
* the property (or, with an empty key, the whole store).
|
|
83
|
+
*
|
|
84
|
+
* Example usage:
|
|
85
|
+
* ```ts
|
|
86
|
+
* await storage.set('user', '', { name: 'Ada' }); // whole store
|
|
87
|
+
* await storage.set('user', 'profile.theme', 'dark'); // { name: 'Ada', profile: { theme: 'dark' } }
|
|
88
|
+
* ```
|
|
44
89
|
*
|
|
45
|
-
* @param storeName -
|
|
46
|
-
* @param key -
|
|
47
|
-
* @param value -
|
|
90
|
+
* @param storeName - Store to write
|
|
91
|
+
* @param key - Dot-separated path inside the store; empty for the whole store
|
|
92
|
+
* @param value - New value; `undefined` removes it
|
|
93
|
+
* @returns Resolves once subscribers have received the new value
|
|
48
94
|
*/
|
|
49
95
|
set(storeName: S, key: string, value: any): Promise<void>;
|
|
50
96
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
97
|
+
* Merges the properties of an object into the object at a path (one level deep, like `Object.assign`).
|
|
98
|
+
*
|
|
99
|
+
* Properties of `value` that are `null` or `undefined` are skipped, so they never erase stored ones;
|
|
100
|
+
* use `delete` for that. When there is no object at the path yet, `value` starts a new one.
|
|
101
|
+
*
|
|
102
|
+
* Example usage:
|
|
103
|
+
* ```ts
|
|
104
|
+
* await storage.set('user', '', { name: 'Ada', age: 36 });
|
|
105
|
+
* await storage.patch('user', '', { age: 37, email: 'ada@example.com' });
|
|
106
|
+
* // { name: 'Ada', age: 37, email: 'ada@example.com' }
|
|
107
|
+
* ```
|
|
53
108
|
*
|
|
54
|
-
* @param storeName -
|
|
55
|
-
* @param key -
|
|
56
|
-
* @param value -
|
|
109
|
+
* @param storeName - Store to change
|
|
110
|
+
* @param key - Dot-separated path of the object inside the store; empty for the whole store
|
|
111
|
+
* @param value - Properties to merge in
|
|
112
|
+
* @returns Resolves once subscribers have been notified
|
|
57
113
|
*/
|
|
58
|
-
patch(storeName: S, key: string, value: any): Promise<void>;
|
|
114
|
+
patch(storeName: S, key: string, value: Record<string, any>): Promise<void>;
|
|
59
115
|
/**
|
|
60
|
-
*
|
|
116
|
+
* Removes a value at a path, or the whole store. Same as `set(storeName, key, undefined)`.
|
|
61
117
|
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
118
|
+
* Example usage:
|
|
119
|
+
* ```ts
|
|
120
|
+
* await storage.delete('user', 'profile.theme'); // one property
|
|
121
|
+
* await storage.delete('user'); // the whole store
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* @param storeName - Store to change
|
|
125
|
+
* @param key - Dot-separated path inside the store; empty (default) removes the whole store
|
|
126
|
+
* @returns Resolves once subscribers have been notified
|
|
64
127
|
*/
|
|
65
128
|
delete(storeName: S, key?: string): Promise<void>;
|
|
66
129
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
130
|
+
* Appends values to the array at a path, creating the array when there is none.
|
|
131
|
+
*
|
|
132
|
+
* Example usage:
|
|
133
|
+
* ```ts
|
|
134
|
+
* await storage.push('todos', 'items', 'Buy milk', 'Walk the dog');
|
|
135
|
+
* ```
|
|
69
136
|
*
|
|
70
|
-
* @param storeName -
|
|
71
|
-
* @param key -
|
|
72
|
-
* @param values - Values to
|
|
137
|
+
* @param storeName - Store to change
|
|
138
|
+
* @param key - Dot-separated path of the array inside the store
|
|
139
|
+
* @param values - Values to append
|
|
140
|
+
* @returns Resolves once subscribers have been notified
|
|
73
141
|
*/
|
|
74
142
|
push(storeName: S, key: string, ...values: any[]): Promise<void>;
|
|
75
143
|
/**
|
|
76
|
-
*
|
|
144
|
+
* Removes the first item of the array at a path that equals `value`.
|
|
145
|
+
*
|
|
146
|
+
* Items are compared by content, as JSON: objects match when they have the same properties and
|
|
147
|
+
* values, in any order. Nothing happens when there is no array at the path or no item matches.
|
|
148
|
+
*
|
|
149
|
+
* Example usage:
|
|
150
|
+
* ```ts
|
|
151
|
+
* await storage.set('todos', 'items', [{ id: 1, text: 'Buy milk' }, { id: 2, text: 'Walk the dog' }]);
|
|
152
|
+
* await storage.drop('todos', 'items', { id: 1, text: 'Buy milk' });
|
|
153
|
+
* // [{ id: 2, text: 'Walk the dog' }]
|
|
154
|
+
* ```
|
|
77
155
|
*
|
|
78
|
-
* @param storeName -
|
|
79
|
-
* @param key -
|
|
80
|
-
* @param value -
|
|
156
|
+
* @param storeName - Store to change
|
|
157
|
+
* @param key - Dot-separated path of the array inside the store
|
|
158
|
+
* @param value - Item to remove
|
|
159
|
+
* @returns Resolves once subscribers have been notified
|
|
81
160
|
*/
|
|
82
161
|
drop(storeName: S, key: string, value: any): Promise<void>;
|
|
83
162
|
}
|
|
84
|
-
|
|
163
|
+
|
|
164
|
+
export { ReactiveStorage };
|
|
165
|
+
export type { ReactiveStorageChangeEvent };
|
package/reactive-storage.js
CHANGED
|
@@ -1,23 +1,48 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { from, switchMap, map, startWith, distinctUntilChanged, firstValueFrom, lastValueFrom, take } from 'rxjs';
|
|
3
|
+
import { compact } from '@telperion/js-utils/array/compact';
|
|
4
|
+
import { compact as compact$1 } from '@telperion/js-utils/object/compact';
|
|
5
|
+
import { deepFreeze } from '@telperion/js-utils/object/deep-freeze';
|
|
6
|
+
import { jsonEqual } from '@telperion/js-utils/object/json-equal';
|
|
7
|
+
import { shareLast } from '@telperion/rx-utils/operators/share-last';
|
|
8
|
+
import { FifoPromise } from '@telperion/ts-utils/fifo-promise';
|
|
9
|
+
|
|
1
10
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { __decorate, __metadata } from "tslib";
|
|
3
|
-
import { distinctUntilChanged, firstValueFrom, from, lastValueFrom, map, startWith, switchMap, take } from "rxjs";
|
|
4
|
-
import { compact } from "@thalesrc/js-utils/array/compact";
|
|
5
|
-
import { remove } from "@thalesrc/js-utils/array/remove";
|
|
6
|
-
import { deepFreeze } from "@thalesrc/js-utils/object/deep-freeze";
|
|
7
|
-
import { shareLast } from "@thalesrc/rx-utils/operators/share-last";
|
|
8
|
-
import { FifoPromise } from "@thalesrc/ts-utils/fifo-promise";
|
|
9
11
|
const GET_ALL = Symbol('Reactive Storage Get All');
|
|
10
12
|
const CHANGES = Symbol('Reactive Storage Changes');
|
|
11
13
|
const SET = Symbol('Reactive Storage Set');
|
|
12
14
|
const REMOVE = Symbol('Reactive Storage Remove');
|
|
13
15
|
/**
|
|
14
|
-
* Abstract base class
|
|
15
|
-
*
|
|
16
|
-
*
|
|
16
|
+
* Abstract base class of every reactive storage: the shared `get`/`set`/`patch`/`delete`/`push`/`drop` API.
|
|
17
|
+
*
|
|
18
|
+
* Data is organised in named **stores**: one JSON value per store name (one localStorage key, one cookie).
|
|
19
|
+
* Read and write inside a store with a dot-separated **key path** (`'profile.name'`); an empty key
|
|
20
|
+
* means the whole store.
|
|
21
|
+
*
|
|
22
|
+
* - `get()` returns an Observable that emits the current value right away and again whenever it changes.
|
|
23
|
+
* Repeats of the same value (`===`) are skipped, so a primitive at a path emits only when it changes.
|
|
24
|
+
* - Emitted values are deep-frozen: copy them before changing them.
|
|
25
|
+
* - Writes return a Promise that resolves once subscribers have been notified. Writes from every
|
|
26
|
+
* storage instance run one at a time, first in first out; one that takes longer than a second
|
|
27
|
+
* is rejected with a `TimeoutError`.
|
|
28
|
+
* - Only writes made through the same instance (or, for cookies, linked instances) are observed.
|
|
29
|
+
*
|
|
30
|
+
* Example usage:
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { ReactiveWebLocalStorage } from '@telperion/reactive-storage/reactive-web-local-storage';
|
|
33
|
+
*
|
|
34
|
+
* const storage = new ReactiveWebLocalStorage<'user' | 'settings'>('my-app');
|
|
17
35
|
*
|
|
18
|
-
*
|
|
36
|
+
* storage.get<string>('user', 'profile.name').subscribe((name) => console.log(name));
|
|
37
|
+
* // undefined
|
|
38
|
+
*
|
|
39
|
+
* await storage.set('user', 'profile.name', 'Ada');
|
|
40
|
+
* // Ada
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
19
44
|
*/
|
|
20
|
-
|
|
45
|
+
class ReactiveStorage {
|
|
21
46
|
static FifoGroup = Symbol();
|
|
22
47
|
static extractData(data, prop = null) {
|
|
23
48
|
if (!prop)
|
|
@@ -55,16 +80,25 @@ export class ReactiveStorage {
|
|
|
55
80
|
return this.#_stream$;
|
|
56
81
|
}
|
|
57
82
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
83
|
+
* Observable of a store, or of one value inside it, that emits the current value and then every change.
|
|
84
|
+
*
|
|
85
|
+
* The value is `undefined` while the store or path does not exist. Emitted values are deep-frozen.
|
|
60
86
|
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
87
|
+
* Example usage:
|
|
88
|
+
* ```ts
|
|
89
|
+
* storage.get('user').subscribe((user) => console.log(user)); // whole store
|
|
90
|
+
* storage.get<string>('user', 'profile.name').subscribe(console.log); // one value
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @template T - Type of the value at `key`
|
|
94
|
+
* @param storeName - Store to read
|
|
95
|
+
* @param key - Dot-separated path inside the store; empty (default) for the whole store
|
|
96
|
+
* @returns Observable of the value, which never completes
|
|
65
97
|
*/
|
|
66
98
|
get(storeName, key = '') {
|
|
67
|
-
return this.#stream$.pipe(
|
|
99
|
+
return this.#stream$.pipe(
|
|
100
|
+
// The store by name first: store names may contain dots, key paths are split on them
|
|
101
|
+
map(allData => ReactiveStorage.extractData(allData[storeName], key)), distinctUntilChanged());
|
|
68
102
|
}
|
|
69
103
|
async #set(storeName, key, value) {
|
|
70
104
|
const store = await firstValueFrom(this.get(storeName));
|
|
@@ -101,59 +135,109 @@ export class ReactiveStorage {
|
|
|
101
135
|
return;
|
|
102
136
|
}
|
|
103
137
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
138
|
+
* Writes a value at a path, replacing what was there and creating missing parent objects.
|
|
139
|
+
*
|
|
140
|
+
* The value is stored as JSON, so it must be JSON-serializable. Passing `undefined` removes
|
|
141
|
+
* the property (or, with an empty key, the whole store).
|
|
106
142
|
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
143
|
+
* Example usage:
|
|
144
|
+
* ```ts
|
|
145
|
+
* await storage.set('user', '', { name: 'Ada' }); // whole store
|
|
146
|
+
* await storage.set('user', 'profile.theme', 'dark'); // { name: 'Ada', profile: { theme: 'dark' } }
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @param storeName - Store to write
|
|
150
|
+
* @param key - Dot-separated path inside the store; empty for the whole store
|
|
151
|
+
* @param value - New value; `undefined` removes it
|
|
152
|
+
* @returns Resolves once subscribers have received the new value
|
|
110
153
|
*/
|
|
111
154
|
async set(storeName, key, value) {
|
|
112
155
|
return await this.#set(storeName, key, value);
|
|
113
156
|
}
|
|
114
157
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
158
|
+
* Merges the properties of an object into the object at a path (one level deep, like `Object.assign`).
|
|
159
|
+
*
|
|
160
|
+
* Properties of `value` that are `null` or `undefined` are skipped, so they never erase stored ones;
|
|
161
|
+
* use `delete` for that. When there is no object at the path yet, `value` starts a new one.
|
|
162
|
+
*
|
|
163
|
+
* Example usage:
|
|
164
|
+
* ```ts
|
|
165
|
+
* await storage.set('user', '', { name: 'Ada', age: 36 });
|
|
166
|
+
* await storage.patch('user', '', { age: 37, email: 'ada@example.com' });
|
|
167
|
+
* // { name: 'Ada', age: 37, email: 'ada@example.com' }
|
|
168
|
+
* ```
|
|
117
169
|
*
|
|
118
|
-
* @param storeName -
|
|
119
|
-
* @param key -
|
|
120
|
-
* @param value -
|
|
170
|
+
* @param storeName - Store to change
|
|
171
|
+
* @param key - Dot-separated path of the object inside the store; empty for the whole store
|
|
172
|
+
* @param value - Properties to merge in
|
|
173
|
+
* @returns Resolves once subscribers have been notified
|
|
121
174
|
*/
|
|
122
175
|
async patch(storeName, key, value) {
|
|
123
176
|
const existing = await firstValueFrom(this.get(storeName, key));
|
|
124
|
-
|
|
177
|
+
const base = typeof existing === 'object' && existing !== null && !Array.isArray(existing) ? existing : {};
|
|
178
|
+
await this.#set(storeName, key, { ...base, ...compact$1(value) });
|
|
125
179
|
}
|
|
126
180
|
/**
|
|
127
|
-
*
|
|
181
|
+
* Removes a value at a path, or the whole store. Same as `set(storeName, key, undefined)`.
|
|
128
182
|
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
183
|
+
* Example usage:
|
|
184
|
+
* ```ts
|
|
185
|
+
* await storage.delete('user', 'profile.theme'); // one property
|
|
186
|
+
* await storage.delete('user'); // the whole store
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @param storeName - Store to change
|
|
190
|
+
* @param key - Dot-separated path inside the store; empty (default) removes the whole store
|
|
191
|
+
* @returns Resolves once subscribers have been notified
|
|
131
192
|
*/
|
|
132
193
|
async delete(storeName, key = '') {
|
|
133
194
|
return await this.#set(storeName, key, undefined);
|
|
134
195
|
}
|
|
135
196
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
197
|
+
* Appends values to the array at a path, creating the array when there is none.
|
|
198
|
+
*
|
|
199
|
+
* Example usage:
|
|
200
|
+
* ```ts
|
|
201
|
+
* await storage.push('todos', 'items', 'Buy milk', 'Walk the dog');
|
|
202
|
+
* ```
|
|
138
203
|
*
|
|
139
|
-
* @param storeName -
|
|
140
|
-
* @param key -
|
|
141
|
-
* @param values - Values to
|
|
204
|
+
* @param storeName - Store to change
|
|
205
|
+
* @param key - Dot-separated path of the array inside the store
|
|
206
|
+
* @param values - Values to append
|
|
207
|
+
* @returns Resolves once subscribers have been notified
|
|
142
208
|
*/
|
|
143
209
|
async push(storeName, key, ...values) {
|
|
144
210
|
const existing = await firstValueFrom(this.get(storeName, key)) ?? [];
|
|
145
211
|
await this.#set(storeName, key, [...existing, ...values]);
|
|
146
212
|
}
|
|
147
213
|
/**
|
|
148
|
-
*
|
|
214
|
+
* Removes the first item of the array at a path that equals `value`.
|
|
149
215
|
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
216
|
+
* Items are compared by content, as JSON: objects match when they have the same properties and
|
|
217
|
+
* values, in any order. Nothing happens when there is no array at the path or no item matches.
|
|
218
|
+
*
|
|
219
|
+
* Example usage:
|
|
220
|
+
* ```ts
|
|
221
|
+
* await storage.set('todos', 'items', [{ id: 1, text: 'Buy milk' }, { id: 2, text: 'Walk the dog' }]);
|
|
222
|
+
* await storage.drop('todos', 'items', { id: 1, text: 'Buy milk' });
|
|
223
|
+
* // [{ id: 2, text: 'Walk the dog' }]
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @param storeName - Store to change
|
|
227
|
+
* @param key - Dot-separated path of the array inside the store
|
|
228
|
+
* @param value - Item to remove
|
|
229
|
+
* @returns Resolves once subscribers have been notified
|
|
153
230
|
*/
|
|
154
231
|
async drop(storeName, key, value) {
|
|
155
232
|
const existing = await firstValueFrom(this.get(storeName, key));
|
|
156
|
-
|
|
233
|
+
if (!Array.isArray(existing))
|
|
234
|
+
return;
|
|
235
|
+
// Compare with the value as it would be stored (e.g. without `undefined` properties)
|
|
236
|
+
const stored = value === undefined ? undefined : JSON.parse(JSON.stringify(value));
|
|
237
|
+
const index = existing.findIndex((item) => jsonEqual(item, stored));
|
|
238
|
+
if (index < 0)
|
|
239
|
+
return;
|
|
240
|
+
await this.#set(storeName, key, existing.filter((_, position) => position !== index));
|
|
157
241
|
}
|
|
158
242
|
}
|
|
159
243
|
__decorate([
|
|
@@ -187,4 +271,5 @@ __decorate([
|
|
|
187
271
|
__metadata("design:returntype", Promise)
|
|
188
272
|
], ReactiveStorage.prototype, "drop", null);
|
|
189
273
|
|
|
274
|
+
export { ReactiveStorage };
|
|
190
275
|
//# sourceMappingURL=reactive-storage.js.map
|
package/reactive-storage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../libs/reactive-storage/src/reactive-storage.ts"],"names":[],"mappings":"AAAA,uDAAuD;;AAEvD,OAAO,EAAc,oBAAoB,EAAE,cAAc,EAAE,IAAI,EAAE,aAAa,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC9H,OAAO,EAAE,OAAO,EAAE,MAAM,kCAAkC,CAAC;AAC3D,OAAO,EAAE,MAAM,EAAE,MAAM,iCAAiC,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,uCAAuC,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAE9D,MAAM,OAAO,GAAG,MAAM,CAAC,0BAA0B,CAAC,CAAC;AACnD,MAAM,OAAO,GAAG,MAAM,CAAC,0BAA0B,CAAC,CAAC;AACnD,MAAM,GAAG,GAAG,MAAM,CAAC,sBAAsB,CAAC,CAAC;AAC3C,MAAM,MAAM,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAOjD;;;;;;GAMG;AACH,MAAM,OAAgB,eAAe;IAC3B,MAAM,CAAC,SAAS,GAAG,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,WAAW,CAAU,IAAS,EAAE,OAAe,IAAK;QACjE,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAEvB,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QAC3C,IAAI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AAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;2CAKhE","file":"reactive-storage.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { Observable, distinctUntilChanged, firstValueFrom, from, lastValueFrom, map, startWith, switchMap, take } from \"rxjs\";\nimport { compact } from \"@thalesrc/js-utils/array/compact\";\nimport { remove } from \"@thalesrc/js-utils/array/remove\";\nimport { deepFreeze } from \"@thalesrc/js-utils/object/deep-freeze\";\nimport { shareLast } from \"@thalesrc/rx-utils/operators/share-last\";\nimport { FifoPromise } from \"@thalesrc/ts-utils/fifo-promise\";\n\nconst GET_ALL = Symbol('Reactive Storage Get All');\nconst CHANGES = Symbol('Reactive Storage Changes');\nconst SET = Symbol('Reactive Storage Set');\nconst REMOVE = Symbol('Reactive Storage Remove');\n\nexport interface ReactiveStorageChangeEvent<S = string> {\n storeName: S;\n value: any;\n}\n\n/**\n * Abstract base class for reactive storage implementations.\n * Provides RxJS-based reactive API for storage with support for nested property access.\n * All write operations are queued using FIFO promises to ensure consistency.\n *\n * @template S - Storage store name type (defaults to string)\n */\nexport abstract class ReactiveStorage<S extends string = string> {\n private static FifoGroup = Symbol();\n private static extractData<T = any>(data: any, prop: string = null!): T {\n if (!prop) return data;\n\n const fragments = compact(prop.split('.'));\n let value = data;\n\n do {\n try {\n value = value[fragments[0]];\n } catch {\n return undefined!;\n }\n\n fragments.shift();\n } while (fragments.length);\n\n return value;\n }\n\n static Props = { GET_ALL, CHANGES, SET, REMOVE } as const;\n\n protected abstract [GET_ALL](): Promise<any>;\n protected abstract [CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;\n protected abstract [SET](store: S, value: any): Promise<void>;\n protected abstract [REMOVE](store: S): Promise<void>;\n\n #_stream$: Observable<Readonly<Record<S, any>>> = null!;\n\n #initializeStream() {\n this.#_stream$ = from(this[GET_ALL]()).pipe(\n switchMap(data => {\n data ??= {};\n\n return from(this[CHANGES]).pipe(\n map(({ storeName, value }) => {\n if (value === undefined) delete data[storeName];\n else data[storeName] = value;\n\n return deepFreeze({ ...data });\n }),\n startWith(deepFreeze({ ...data }))\n );\n }),\n shareLast()\n );\n }\n\n get #stream$() {\n if (!this.#_stream$) this.#initializeStream();\n\n return this.#_stream$;\n }\n\n /**\n * Get an observable that emits the current value and all future changes.\n * Supports dot notation for nested property access (e.g., 'user.settings.theme').\n *\n * @template T - Expected return type\n * @param storeName - Name of the storage store\n * @param key - Property path using dot notation (empty string for entire store)\n * @returns Observable that emits current value and all updates\n */\n get<T = Record<string, any>>(storeName: S, key = ''): Observable<T> {\n return this.#stream$.pipe(\n map(allData => ReactiveStorage.extractData(allData, `${storeName}.${key}`)),\n distinctUntilChanged()\n );\n }\n\n async #set(storeName: S, key: string, value: any): Promise<void> {\n const store = await firstValueFrom(this.get(storeName));\n const storeChangeEvent = lastValueFrom(this.get(storeName).pipe(take(2)));\n const paths = compact(key.split('.'));\n\n if (!paths.length) {\n if (value === undefined) {\n await this[REMOVE](storeName);\n } else {\n await this[SET](storeName, value);\n }\n\n await storeChangeEvent;\n\n return;\n }\n\n const newStore = JSON.parse(JSON.stringify(typeof store === 'object' ? store : {}));\n let lastPiece = newStore;\n\n while (paths.length > 1) {\n if (typeof lastPiece[paths[0]] !== 'object') lastPiece[paths[0]] = {};\n\n lastPiece = lastPiece[paths[0]];\n paths.shift();\n }\n\n if (lastPiece[paths[0]] === value) return;\n\n if (value === undefined) {\n delete lastPiece[paths[0]];\n } else {\n lastPiece[paths[0]] = value;\n }\n\n await this[SET](storeName, newStore);\n await storeChangeEvent;\n\n return;\n }\n\n /**\n * Set a value in storage. Overwrites existing value.\n * Pass undefined as value to remove the property.\n *\n * @param storeName - Name of the storage store\n * @param key - Property path using dot notation\n * @param value - Value to set (undefined removes the value)\n */\n @FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 })\n async set(storeName: S, key: string, value: any): Promise<void> {\n return await this.#set(storeName, key, value);\n }\n\n /**\n * Merge a partial object into existing value.\n * Only works with object values.\n *\n * @param storeName - Name of the storage store\n * @param key - Property path using dot notation\n * @param value - Partial object to merge with existing value\n */\n @FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 })\n async patch(storeName: S, key: string, value: any): Promise<void> {\n const existing = await firstValueFrom(this.get<any[]>(storeName, key));\n\n await this.#set(storeName, key, Object.assign({}, existing, compact(value)));\n }\n\n /**\n * Delete a value from storage.\n *\n * @param storeName - Name of the storage store\n * @param key - Property path using dot notation (empty string deletes entire store)\n */\n @FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 })\n async delete(storeName: S, key = ''): Promise<void> {\n return await this.#set(storeName, key, undefined);\n }\n\n /**\n * Add items to an array in storage.\n * Creates array if it doesn't exist.\n *\n * @param storeName - Name of the storage store\n * @param key - Path to array property using dot notation\n * @param values - Values to push into the array\n */\n @FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 })\n async push(storeName: S, key: string, ...values: any[]): Promise<void> {\n const existing = await firstValueFrom(this.get<any[]>(storeName, key)) ?? [];\n\n await this.#set(storeName, key, [...existing, ...values]);\n }\n\n /**\n * Remove a specific value from an array in storage.\n *\n * @param storeName - Name of the storage store\n * @param key - Path to array property using dot notation\n * @param value - Value to remove from the array\n */\n @FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 })\n async drop(storeName: S, key: string, value: any): Promise<void> {\n const existing = await firstValueFrom(this.get<any[]>(storeName, key));\n\n await this.#set(storeName, key, remove(existing, value));\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"reactive-storage.js","sources":["../../../libs/reactive-storage/src/reactive-storage.ts"],"sourcesContent":[null],"names":["compactObject"],"mappings":";;;;;;;;;AAAA;AAUA,MAAM,OAAO,GAAG,MAAM,CAAC,0BAA0B,CAAC;AAClD,MAAM,OAAO,GAAG,MAAM,CAAC,0BAA0B,CAAC;AAClD,MAAM,GAAG,GAAG,MAAM,CAAC,sBAAsB,CAAC;AAC1C,MAAM,MA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
|
+
var reactiveWebStorage = require('./reactive-web-storage.cjs');
|
|
4
|
+
|
|
5
5
|
/**
|
|
6
|
-
* Reactive
|
|
7
|
-
* Data persists across browser sessions.
|
|
6
|
+
* Reactive storage on `localStorage`: data stays until it is removed, across tabs and browser restarts.
|
|
8
7
|
*
|
|
9
|
-
*
|
|
8
|
+
* Each store is one localStorage key, `<appName>/<storeName>`, holding the store as JSON.
|
|
9
|
+
* Existing keys of the same app are read when the first value is requested.
|
|
10
|
+
*
|
|
11
|
+
* Changes made in other tabs, or by other instances, are not observed.
|
|
12
|
+
*
|
|
13
|
+
* Example usage:
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { ReactiveWebLocalStorage } from '@telperion/reactive-storage/reactive-web-local-storage';
|
|
16
|
+
*
|
|
17
|
+
* const storage = new ReactiveWebLocalStorage<'settings'>('my-app');
|
|
18
|
+
*
|
|
19
|
+
* storage.get<'light' | 'dark'>('settings', 'theme').subscribe((theme) => {
|
|
20
|
+
* document.documentElement.dataset.theme = theme ?? 'light';
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await storage.set('settings', 'theme', 'dark'); // saved as my-app/settings = {"theme":"dark"}
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
10
27
|
*/
|
|
11
|
-
class ReactiveWebLocalStorage extends
|
|
28
|
+
class ReactiveWebLocalStorage extends reactiveWebStorage.AbstractReactiveWebStorage {
|
|
12
29
|
appName;
|
|
13
30
|
storage = window.localStorage;
|
|
31
|
+
/**
|
|
32
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
33
|
+
*/
|
|
14
34
|
constructor(appName = 'app') {
|
|
15
35
|
super();
|
|
16
36
|
this.appName = appName;
|
|
17
37
|
}
|
|
18
38
|
}
|
|
39
|
+
|
|
19
40
|
exports.ReactiveWebLocalStorage = ReactiveWebLocalStorage;
|
|
41
|
+
//# sourceMappingURL=reactive-web-local-storage.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive-web-local-storage.cjs","sources":["../../../libs/reactive-storage/src/reactive-web-local-storage.ts"],"sourcesContent":[null],"names":["AbstractReactiveWebStorage"],"mappings":";;;;AAEA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,MAAO,uBAAmD,SAAQA,6CAA6B,CAAA;AAO/E,IAAA,OAAA;AANF,IAAA,OAAO,GAAG,MAAM,CAAC,YAAY;AAE/C;;AAEG;AACH,IAAA,WAAA,CACoB,UAAU,KAAK,EAAA;AAEjC,QAAA,KAAK,EAAE;QAFW,IAAA,CAAA,OAAO,GAAP,OAAO;IAG3B;AACD;;;;"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AbstractReactiveWebStorage } from './reactive-web-storage.cjs';
|
|
2
|
+
import './reactive-storage.cjs';
|
|
3
|
+
import 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Reactive storage on `localStorage`: data stays until it is removed, across tabs and browser restarts.
|
|
7
|
+
*
|
|
8
|
+
* Each store is one localStorage key, `<appName>/<storeName>`, holding the store as JSON.
|
|
9
|
+
* Existing keys of the same app are read when the first value is requested.
|
|
10
|
+
*
|
|
11
|
+
* Changes made in other tabs, or by other instances, are not observed.
|
|
12
|
+
*
|
|
13
|
+
* Example usage:
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { ReactiveWebLocalStorage } from '@telperion/reactive-storage/reactive-web-local-storage';
|
|
16
|
+
*
|
|
17
|
+
* const storage = new ReactiveWebLocalStorage<'settings'>('my-app');
|
|
18
|
+
*
|
|
19
|
+
* storage.get<'light' | 'dark'>('settings', 'theme').subscribe((theme) => {
|
|
20
|
+
* document.documentElement.dataset.theme = theme ?? 'light';
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await storage.set('settings', 'theme', 'dark'); // saved as my-app/settings = {"theme":"dark"}
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
27
|
+
*/
|
|
28
|
+
declare class ReactiveWebLocalStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
29
|
+
readonly appName: string;
|
|
30
|
+
readonly storage: Storage;
|
|
31
|
+
/**
|
|
32
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
33
|
+
*/
|
|
34
|
+
constructor(appName?: string);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { ReactiveWebLocalStorage };
|
|
@@ -1,12 +1,37 @@
|
|
|
1
|
-
import { AbstractReactiveWebStorage } from
|
|
1
|
+
import { AbstractReactiveWebStorage } from './reactive-web-storage.js';
|
|
2
|
+
import './reactive-storage.js';
|
|
3
|
+
import 'rxjs';
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
|
-
* Reactive
|
|
4
|
-
* Data persists across browser sessions.
|
|
6
|
+
* Reactive storage on `localStorage`: data stays until it is removed, across tabs and browser restarts.
|
|
5
7
|
*
|
|
6
|
-
*
|
|
8
|
+
* Each store is one localStorage key, `<appName>/<storeName>`, holding the store as JSON.
|
|
9
|
+
* Existing keys of the same app are read when the first value is requested.
|
|
10
|
+
*
|
|
11
|
+
* Changes made in other tabs, or by other instances, are not observed.
|
|
12
|
+
*
|
|
13
|
+
* Example usage:
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { ReactiveWebLocalStorage } from '@telperion/reactive-storage/reactive-web-local-storage';
|
|
16
|
+
*
|
|
17
|
+
* const storage = new ReactiveWebLocalStorage<'settings'>('my-app');
|
|
18
|
+
*
|
|
19
|
+
* storage.get<'light' | 'dark'>('settings', 'theme').subscribe((theme) => {
|
|
20
|
+
* document.documentElement.dataset.theme = theme ?? 'light';
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await storage.set('settings', 'theme', 'dark'); // saved as my-app/settings = {"theme":"dark"}
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
7
27
|
*/
|
|
8
|
-
|
|
28
|
+
declare class ReactiveWebLocalStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
9
29
|
readonly appName: string;
|
|
10
30
|
readonly storage: Storage;
|
|
31
|
+
/**
|
|
32
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
33
|
+
*/
|
|
11
34
|
constructor(appName?: string);
|
|
12
35
|
}
|
|
36
|
+
|
|
37
|
+
export { ReactiveWebLocalStorage };
|