@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.cjs
CHANGED
|
@@ -1,31 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var rxjs = require('rxjs');
|
|
5
|
+
var compact = require('@telperion/js-utils/array/compact');
|
|
6
|
+
var compact$1 = require('@telperion/js-utils/object/compact');
|
|
7
|
+
var deepFreeze = require('@telperion/js-utils/object/deep-freeze');
|
|
8
|
+
var jsonEqual = require('@telperion/js-utils/object/json-equal');
|
|
9
|
+
var shareLast = require('@telperion/rx-utils/operators/share-last');
|
|
10
|
+
var fifoPromise = require('@telperion/ts-utils/fifo-promise');
|
|
11
|
+
|
|
2
12
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.ReactiveStorage = void 0;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
|
-
const rxjs_1 = require("rxjs");
|
|
7
|
-
const compact_1 = require("@thalesrc/js-utils/array/compact");
|
|
8
|
-
const remove_1 = require("@thalesrc/js-utils/array/remove");
|
|
9
|
-
const deep_freeze_1 = require("@thalesrc/js-utils/object/deep-freeze");
|
|
10
|
-
const share_last_1 = require("@thalesrc/rx-utils/operators/share-last");
|
|
11
|
-
const fifo_promise_1 = require("@thalesrc/ts-utils/fifo-promise");
|
|
12
13
|
const GET_ALL = Symbol('Reactive Storage Get All');
|
|
13
14
|
const CHANGES = Symbol('Reactive Storage Changes');
|
|
14
15
|
const SET = Symbol('Reactive Storage Set');
|
|
15
16
|
const REMOVE = Symbol('Reactive Storage Remove');
|
|
16
17
|
/**
|
|
17
|
-
* Abstract base class
|
|
18
|
-
* Provides RxJS-based reactive API for storage with support for nested property access.
|
|
19
|
-
* All write operations are queued using FIFO promises to ensure consistency.
|
|
18
|
+
* Abstract base class of every reactive storage: the shared `get`/`set`/`patch`/`delete`/`push`/`drop` API.
|
|
20
19
|
*
|
|
21
|
-
*
|
|
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.
|
|
31
|
+
*
|
|
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
|
|
22
46
|
*/
|
|
23
47
|
class ReactiveStorage {
|
|
24
48
|
static FifoGroup = Symbol();
|
|
25
49
|
static extractData(data, prop = null) {
|
|
26
50
|
if (!prop)
|
|
27
51
|
return data;
|
|
28
|
-
const fragments =
|
|
52
|
+
const fragments = compact.compact(prop.split('.'));
|
|
29
53
|
let value = data;
|
|
30
54
|
do {
|
|
31
55
|
try {
|
|
@@ -41,16 +65,16 @@ class ReactiveStorage {
|
|
|
41
65
|
static Props = { GET_ALL, CHANGES, SET, REMOVE };
|
|
42
66
|
#_stream$ = null;
|
|
43
67
|
#initializeStream() {
|
|
44
|
-
this.#_stream$ =
|
|
68
|
+
this.#_stream$ = rxjs.from(this[GET_ALL]()).pipe(rxjs.switchMap(data => {
|
|
45
69
|
data ??= {};
|
|
46
|
-
return
|
|
70
|
+
return rxjs.from(this[CHANGES]).pipe(rxjs.map(({ storeName, value }) => {
|
|
47
71
|
if (value === undefined)
|
|
48
72
|
delete data[storeName];
|
|
49
73
|
else
|
|
50
74
|
data[storeName] = value;
|
|
51
|
-
return
|
|
52
|
-
}),
|
|
53
|
-
}),
|
|
75
|
+
return deepFreeze.deepFreeze({ ...data });
|
|
76
|
+
}), rxjs.startWith(deepFreeze.deepFreeze({ ...data })));
|
|
77
|
+
}), shareLast.shareLast());
|
|
54
78
|
}
|
|
55
79
|
get #stream$() {
|
|
56
80
|
if (!this.#_stream$)
|
|
@@ -58,21 +82,30 @@ class ReactiveStorage {
|
|
|
58
82
|
return this.#_stream$;
|
|
59
83
|
}
|
|
60
84
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
85
|
+
* Observable of a store, or of one value inside it, that emits the current value and then every change.
|
|
86
|
+
*
|
|
87
|
+
* The value is `undefined` while the store or path does not exist. Emitted values are deep-frozen.
|
|
88
|
+
*
|
|
89
|
+
* Example usage:
|
|
90
|
+
* ```ts
|
|
91
|
+
* storage.get('user').subscribe((user) => console.log(user)); // whole store
|
|
92
|
+
* storage.get<string>('user', 'profile.name').subscribe(console.log); // one value
|
|
93
|
+
* ```
|
|
63
94
|
*
|
|
64
|
-
* @template T -
|
|
65
|
-
* @param storeName -
|
|
66
|
-
* @param key -
|
|
67
|
-
* @returns Observable
|
|
95
|
+
* @template T - Type of the value at `key`
|
|
96
|
+
* @param storeName - Store to read
|
|
97
|
+
* @param key - Dot-separated path inside the store; empty (default) for the whole store
|
|
98
|
+
* @returns Observable of the value, which never completes
|
|
68
99
|
*/
|
|
69
100
|
get(storeName, key = '') {
|
|
70
|
-
return this.#stream$.pipe(
|
|
101
|
+
return this.#stream$.pipe(
|
|
102
|
+
// The store by name first: store names may contain dots, key paths are split on them
|
|
103
|
+
rxjs.map(allData => ReactiveStorage.extractData(allData[storeName], key)), rxjs.distinctUntilChanged());
|
|
71
104
|
}
|
|
72
105
|
async #set(storeName, key, value) {
|
|
73
|
-
const store = await
|
|
74
|
-
const storeChangeEvent =
|
|
75
|
-
const paths =
|
|
106
|
+
const store = await rxjs.firstValueFrom(this.get(storeName));
|
|
107
|
+
const storeChangeEvent = rxjs.lastValueFrom(this.get(storeName).pipe(rxjs.take(2)));
|
|
108
|
+
const paths = compact.compact(key.split('.'));
|
|
76
109
|
if (!paths.length) {
|
|
77
110
|
if (value === undefined) {
|
|
78
111
|
await this[REMOVE](storeName);
|
|
@@ -104,89 +137,141 @@ class ReactiveStorage {
|
|
|
104
137
|
return;
|
|
105
138
|
}
|
|
106
139
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
140
|
+
* Writes a value at a path, replacing what was there and creating missing parent objects.
|
|
141
|
+
*
|
|
142
|
+
* The value is stored as JSON, so it must be JSON-serializable. Passing `undefined` removes
|
|
143
|
+
* the property (or, with an empty key, the whole store).
|
|
144
|
+
*
|
|
145
|
+
* Example usage:
|
|
146
|
+
* ```ts
|
|
147
|
+
* await storage.set('user', '', { name: 'Ada' }); // whole store
|
|
148
|
+
* await storage.set('user', 'profile.theme', 'dark'); // { name: 'Ada', profile: { theme: 'dark' } }
|
|
149
|
+
* ```
|
|
109
150
|
*
|
|
110
|
-
* @param storeName -
|
|
111
|
-
* @param key -
|
|
112
|
-
* @param value -
|
|
151
|
+
* @param storeName - Store to write
|
|
152
|
+
* @param key - Dot-separated path inside the store; empty for the whole store
|
|
153
|
+
* @param value - New value; `undefined` removes it
|
|
154
|
+
* @returns Resolves once subscribers have received the new value
|
|
113
155
|
*/
|
|
114
156
|
async set(storeName, key, value) {
|
|
115
157
|
return await this.#set(storeName, key, value);
|
|
116
158
|
}
|
|
117
159
|
/**
|
|
118
|
-
*
|
|
119
|
-
* Only works with object values.
|
|
160
|
+
* Merges the properties of an object into the object at a path (one level deep, like `Object.assign`).
|
|
120
161
|
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
162
|
+
* Properties of `value` that are `null` or `undefined` are skipped, so they never erase stored ones;
|
|
163
|
+
* use `delete` for that. When there is no object at the path yet, `value` starts a new one.
|
|
164
|
+
*
|
|
165
|
+
* Example usage:
|
|
166
|
+
* ```ts
|
|
167
|
+
* await storage.set('user', '', { name: 'Ada', age: 36 });
|
|
168
|
+
* await storage.patch('user', '', { age: 37, email: 'ada@example.com' });
|
|
169
|
+
* // { name: 'Ada', age: 37, email: 'ada@example.com' }
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* @param storeName - Store to change
|
|
173
|
+
* @param key - Dot-separated path of the object inside the store; empty for the whole store
|
|
174
|
+
* @param value - Properties to merge in
|
|
175
|
+
* @returns Resolves once subscribers have been notified
|
|
124
176
|
*/
|
|
125
177
|
async patch(storeName, key, value) {
|
|
126
|
-
const existing = await
|
|
127
|
-
|
|
178
|
+
const existing = await rxjs.firstValueFrom(this.get(storeName, key));
|
|
179
|
+
const base = typeof existing === 'object' && existing !== null && !Array.isArray(existing) ? existing : {};
|
|
180
|
+
await this.#set(storeName, key, { ...base, ...compact$1.compact(value) });
|
|
128
181
|
}
|
|
129
182
|
/**
|
|
130
|
-
*
|
|
183
|
+
* Removes a value at a path, or the whole store. Same as `set(storeName, key, undefined)`.
|
|
184
|
+
*
|
|
185
|
+
* Example usage:
|
|
186
|
+
* ```ts
|
|
187
|
+
* await storage.delete('user', 'profile.theme'); // one property
|
|
188
|
+
* await storage.delete('user'); // the whole store
|
|
189
|
+
* ```
|
|
131
190
|
*
|
|
132
|
-
* @param storeName -
|
|
133
|
-
* @param key -
|
|
191
|
+
* @param storeName - Store to change
|
|
192
|
+
* @param key - Dot-separated path inside the store; empty (default) removes the whole store
|
|
193
|
+
* @returns Resolves once subscribers have been notified
|
|
134
194
|
*/
|
|
135
195
|
async delete(storeName, key = '') {
|
|
136
196
|
return await this.#set(storeName, key, undefined);
|
|
137
197
|
}
|
|
138
198
|
/**
|
|
139
|
-
*
|
|
140
|
-
* Creates array if it doesn't exist.
|
|
199
|
+
* Appends values to the array at a path, creating the array when there is none.
|
|
141
200
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
201
|
+
* Example usage:
|
|
202
|
+
* ```ts
|
|
203
|
+
* await storage.push('todos', 'items', 'Buy milk', 'Walk the dog');
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* @param storeName - Store to change
|
|
207
|
+
* @param key - Dot-separated path of the array inside the store
|
|
208
|
+
* @param values - Values to append
|
|
209
|
+
* @returns Resolves once subscribers have been notified
|
|
145
210
|
*/
|
|
146
211
|
async push(storeName, key, ...values) {
|
|
147
|
-
const existing = await
|
|
212
|
+
const existing = await rxjs.firstValueFrom(this.get(storeName, key)) ?? [];
|
|
148
213
|
await this.#set(storeName, key, [...existing, ...values]);
|
|
149
214
|
}
|
|
150
215
|
/**
|
|
151
|
-
*
|
|
216
|
+
* Removes the first item of the array at a path that equals `value`.
|
|
217
|
+
*
|
|
218
|
+
* Items are compared by content, as JSON: objects match when they have the same properties and
|
|
219
|
+
* values, in any order. Nothing happens when there is no array at the path or no item matches.
|
|
152
220
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
221
|
+
* Example usage:
|
|
222
|
+
* ```ts
|
|
223
|
+
* await storage.set('todos', 'items', [{ id: 1, text: 'Buy milk' }, { id: 2, text: 'Walk the dog' }]);
|
|
224
|
+
* await storage.drop('todos', 'items', { id: 1, text: 'Buy milk' });
|
|
225
|
+
* // [{ id: 2, text: 'Walk the dog' }]
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* @param storeName - Store to change
|
|
229
|
+
* @param key - Dot-separated path of the array inside the store
|
|
230
|
+
* @param value - Item to remove
|
|
231
|
+
* @returns Resolves once subscribers have been notified
|
|
156
232
|
*/
|
|
157
233
|
async drop(storeName, key, value) {
|
|
158
|
-
const existing = await
|
|
159
|
-
|
|
234
|
+
const existing = await rxjs.firstValueFrom(this.get(storeName, key));
|
|
235
|
+
if (!Array.isArray(existing))
|
|
236
|
+
return;
|
|
237
|
+
// Compare with the value as it would be stored (e.g. without `undefined` properties)
|
|
238
|
+
const stored = value === undefined ? undefined : JSON.parse(JSON.stringify(value));
|
|
239
|
+
const index = existing.findIndex((item) => jsonEqual.jsonEqual(item, stored));
|
|
240
|
+
if (index < 0)
|
|
241
|
+
return;
|
|
242
|
+
await this.#set(storeName, key, existing.filter((_, position) => position !== index));
|
|
160
243
|
}
|
|
161
244
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
tslib_1.__metadata("design:returntype", Promise)
|
|
245
|
+
tslib.__decorate([
|
|
246
|
+
fifoPromise.FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
247
|
+
tslib.__metadata("design:type", Function),
|
|
248
|
+
tslib.__metadata("design:paramtypes", [String, String, Object]),
|
|
249
|
+
tslib.__metadata("design:returntype", Promise)
|
|
168
250
|
], ReactiveStorage.prototype, "set", null);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
251
|
+
tslib.__decorate([
|
|
252
|
+
fifoPromise.FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
253
|
+
tslib.__metadata("design:type", Function),
|
|
254
|
+
tslib.__metadata("design:paramtypes", [String, String, Object]),
|
|
255
|
+
tslib.__metadata("design:returntype", Promise)
|
|
174
256
|
], ReactiveStorage.prototype, "patch", null);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
257
|
+
tslib.__decorate([
|
|
258
|
+
fifoPromise.FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
259
|
+
tslib.__metadata("design:type", Function),
|
|
260
|
+
tslib.__metadata("design:paramtypes", [String, Object]),
|
|
261
|
+
tslib.__metadata("design:returntype", Promise)
|
|
180
262
|
], ReactiveStorage.prototype, "delete", null);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
263
|
+
tslib.__decorate([
|
|
264
|
+
fifoPromise.FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
265
|
+
tslib.__metadata("design:type", Function),
|
|
266
|
+
tslib.__metadata("design:paramtypes", [String, String, Object]),
|
|
267
|
+
tslib.__metadata("design:returntype", Promise)
|
|
186
268
|
], ReactiveStorage.prototype, "push", null);
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
269
|
+
tslib.__decorate([
|
|
270
|
+
fifoPromise.FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
271
|
+
tslib.__metadata("design:type", Function),
|
|
272
|
+
tslib.__metadata("design:paramtypes", [String, String, Object]),
|
|
273
|
+
tslib.__metadata("design:returntype", Promise)
|
|
192
274
|
], ReactiveStorage.prototype, "drop", null);
|
|
275
|
+
|
|
276
|
+
exports.ReactiveStorage = ReactiveStorage;
|
|
277
|
+
//# sourceMappingURL=reactive-storage.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive-storage.cjs","sources":["../../../libs/reactive-storage/src/reactive-storage.ts"],"sourcesContent":[null],"names":["compact","from","switchMap","map","deepFreeze","startWith","shareLast","distinctUntilChanged","firstValueFrom","lastValueFrom","take","compactObject","j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BAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;AAahE,CAAA,EAAA,eAAA,CAAA,SAAA,EAAA,MAAA,EAAA,IAAA,CAAA;;;;"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
|
|
3
|
+
declare const GET_ALL: unique symbol;
|
|
4
|
+
declare const CHANGES: unique symbol;
|
|
5
|
+
declare const SET: unique symbol;
|
|
6
|
+
declare const REMOVE: unique symbol;
|
|
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> {
|
|
14
|
+
storeName: S;
|
|
15
|
+
value: any;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
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.
|
|
31
|
+
*
|
|
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
|
|
46
|
+
*/
|
|
47
|
+
declare abstract class ReactiveStorage<S extends string = string> {
|
|
48
|
+
#private;
|
|
49
|
+
private static FifoGroup;
|
|
50
|
+
private static extractData;
|
|
51
|
+
static Props: {
|
|
52
|
+
readonly GET_ALL: typeof GET_ALL;
|
|
53
|
+
readonly CHANGES: typeof CHANGES;
|
|
54
|
+
readonly SET: typeof SET;
|
|
55
|
+
readonly REMOVE: typeof REMOVE;
|
|
56
|
+
};
|
|
57
|
+
protected abstract [GET_ALL](): Promise<any>;
|
|
58
|
+
protected abstract [CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;
|
|
59
|
+
protected abstract [SET](store: S, value: any): Promise<void>;
|
|
60
|
+
protected abstract [REMOVE](store: S): Promise<void>;
|
|
61
|
+
/**
|
|
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.
|
|
65
|
+
*
|
|
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
|
|
76
|
+
*/
|
|
77
|
+
get<T = Record<string, any>>(storeName: S, key?: string): Observable<T>;
|
|
78
|
+
/**
|
|
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
|
+
* ```
|
|
89
|
+
*
|
|
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
|
|
94
|
+
*/
|
|
95
|
+
set(storeName: S, key: string, value: any): Promise<void>;
|
|
96
|
+
/**
|
|
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
|
+
* ```
|
|
108
|
+
*
|
|
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
|
|
113
|
+
*/
|
|
114
|
+
patch(storeName: S, key: string, value: Record<string, any>): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Removes a value at a path, or the whole store. Same as `set(storeName, key, undefined)`.
|
|
117
|
+
*
|
|
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
|
|
127
|
+
*/
|
|
128
|
+
delete(storeName: S, key?: string): Promise<void>;
|
|
129
|
+
/**
|
|
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
|
+
* ```
|
|
136
|
+
*
|
|
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
|
|
141
|
+
*/
|
|
142
|
+
push(storeName: S, key: string, ...values: any[]): Promise<void>;
|
|
143
|
+
/**
|
|
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
|
+
* ```
|
|
155
|
+
*
|
|
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
|
|
160
|
+
*/
|
|
161
|
+
drop(storeName: S, key: string, value: any): Promise<void>;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export { ReactiveStorage };
|
|
165
|
+
export type { ReactiveStorageChangeEvent };
|