@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.
Files changed (55) hide show
  1. package/.build-manifest.json +86 -0
  2. package/README.md +6 -6
  3. package/fesm/reactive-storage.mjs +639 -0
  4. package/fesm/reactive-storage.mjs.map +1 -0
  5. package/index.cjs +17 -8
  6. package/index.cjs.map +1 -0
  7. package/index.d.cts +6 -0
  8. package/index.d.ts +6 -5
  9. package/index.js +5 -6
  10. package/index.js.map +1 -1
  11. package/json-parse.cjs +5 -3
  12. package/json-parse.cjs.map +1 -0
  13. package/json-parse.d.cts +8 -0
  14. package/json-parse.d.ts +3 -1
  15. package/json-parse.js +2 -1
  16. package/json-parse.js.map +1 -1
  17. package/package.json +60 -31
  18. package/reactive-cookie-storage.cjs +69 -50
  19. package/reactive-cookie-storage.cjs.map +1 -0
  20. package/reactive-cookie-storage.d.cts +77 -0
  21. package/reactive-cookie-storage.d.ts +48 -40
  22. package/reactive-cookie-storage.js +60 -41
  23. package/reactive-cookie-storage.js.map +1 -1
  24. package/reactive-storage.cjs +168 -83
  25. package/reactive-storage.cjs.map +1 -0
  26. package/reactive-storage.d.cts +165 -0
  27. package/reactive-storage.d.ts +118 -37
  28. package/reactive-storage.js +128 -43
  29. package/reactive-storage.js.map +1 -1
  30. package/reactive-web-local-storage.cjs +30 -8
  31. package/reactive-web-local-storage.cjs.map +1 -0
  32. package/reactive-web-local-storage.d.cts +37 -0
  33. package/reactive-web-local-storage.d.ts +30 -5
  34. package/reactive-web-local-storage.js +27 -5
  35. package/reactive-web-local-storage.js.map +1 -1
  36. package/reactive-web-session-storage.cjs +24 -8
  37. package/reactive-web-session-storage.cjs.map +1 -0
  38. package/reactive-web-session-storage.d.cts +31 -0
  39. package/reactive-web-session-storage.d.ts +24 -5
  40. package/reactive-web-session-storage.js +21 -5
  41. package/reactive-web-session-storage.js.map +1 -1
  42. package/reactive-web-storage.cjs +42 -27
  43. package/reactive-web-storage.cjs.map +1 -0
  44. package/reactive-web-storage.d.cts +47 -0
  45. package/reactive-web-storage.d.ts +28 -10
  46. package/reactive-web-storage.js +33 -18
  47. package/reactive-web-storage.js.map +1 -1
  48. package/async-iterator.polyfill.cjs +0 -87
  49. package/async-iterator.polyfill.d.ts +0 -2
  50. package/async-iterator.polyfill.js +0 -86
  51. package/async-iterator.polyfill.js.map +0 -1
  52. package/regex-parser.cjs +0 -29
  53. package/regex-parser.d.ts +0 -11
  54. package/regex-parser.js +0 -27
  55. package/regex-parser.js.map +0 -1
@@ -1,31 +1,55 @@
1
- "use strict";
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 for reactive storage implementations.
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
- * @template S - Storage store name type (defaults to string)
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 = (0, compact_1.compact)(prop.split('.'));
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$ = (0, rxjs_1.from)(this[GET_ALL]()).pipe((0, rxjs_1.switchMap)(data => {
68
+ this.#_stream$ = rxjs.from(this[GET_ALL]()).pipe(rxjs.switchMap(data => {
45
69
  data ??= {};
46
- return (0, rxjs_1.from)(this[CHANGES]).pipe((0, rxjs_1.map)(({ storeName, value }) => {
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 (0, deep_freeze_1.deepFreeze)({ ...data });
52
- }), (0, rxjs_1.startWith)((0, deep_freeze_1.deepFreeze)({ ...data })));
53
- }), (0, share_last_1.shareLast)());
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
- * Get an observable that emits the current value and all future changes.
62
- * Supports dot notation for nested property access (e.g., 'user.settings.theme').
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 - Expected return type
65
- * @param storeName - Name of the storage store
66
- * @param key - Property path using dot notation (empty string for entire store)
67
- * @returns Observable that emits current value and all updates
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((0, rxjs_1.map)(allData => ReactiveStorage.extractData(allData, `${storeName}.${key}`)), (0, rxjs_1.distinctUntilChanged)());
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 (0, rxjs_1.firstValueFrom)(this.get(storeName));
74
- const storeChangeEvent = (0, rxjs_1.lastValueFrom)(this.get(storeName).pipe((0, rxjs_1.take)(2)));
75
- const paths = (0, compact_1.compact)(key.split('.'));
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
- * Set a value in storage. Overwrites existing value.
108
- * Pass undefined as value to remove the property.
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 - Name of the storage store
111
- * @param key - Property path using dot notation
112
- * @param value - Value to set (undefined removes the 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
- * Merge a partial object into existing value.
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
- * @param storeName - Name of the storage store
122
- * @param key - Property path using dot notation
123
- * @param value - Partial object to merge with existing value
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 (0, rxjs_1.firstValueFrom)(this.get(storeName, key));
127
- await this.#set(storeName, key, Object.assign({}, existing, (0, compact_1.compact)(value)));
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
- * Delete a value from storage.
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 - Name of the storage store
133
- * @param key - Property path using dot notation (empty string deletes entire store)
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
- * Add items to an array in storage.
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
- * @param storeName - Name of the storage store
143
- * @param key - Path to array property using dot notation
144
- * @param values - Values to push into the array
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 (0, rxjs_1.firstValueFrom)(this.get(storeName, key)) ?? [];
212
+ const existing = await rxjs.firstValueFrom(this.get(storeName, key)) ?? [];
148
213
  await this.#set(storeName, key, [...existing, ...values]);
149
214
  }
150
215
  /**
151
- * Remove a specific value from an array in storage.
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
- * @param storeName - Name of the storage store
154
- * @param key - Path to array property using dot notation
155
- * @param value - Value to remove from the array
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 (0, rxjs_1.firstValueFrom)(this.get(storeName, key));
159
- await this.#set(storeName, key, (0, remove_1.remove)(existing, value));
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
- exports.ReactiveStorage = ReactiveStorage;
163
- tslib_1.__decorate([
164
- (0, fifo_promise_1.FifoPromise)({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
165
- tslib_1.__metadata("design:type", Function),
166
- tslib_1.__metadata("design:paramtypes", [String, String, Object]),
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
- tslib_1.__decorate([
170
- (0, fifo_promise_1.FifoPromise)({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
171
- tslib_1.__metadata("design:type", Function),
172
- tslib_1.__metadata("design:paramtypes", [String, String, Object]),
173
- tslib_1.__metadata("design:returntype", Promise)
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
- tslib_1.__decorate([
176
- (0, fifo_promise_1.FifoPromise)({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
177
- tslib_1.__metadata("design:type", Function),
178
- tslib_1.__metadata("design:paramtypes", [String, Object]),
179
- tslib_1.__metadata("design:returntype", Promise)
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
- tslib_1.__decorate([
182
- (0, fifo_promise_1.FifoPromise)({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
183
- tslib_1.__metadata("design:type", Function),
184
- tslib_1.__metadata("design:paramtypes", [String, String, Object]),
185
- tslib_1.__metadata("design:returntype", Promise)
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
- tslib_1.__decorate([
188
- (0, fifo_promise_1.FifoPromise)({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
189
- tslib_1.__metadata("design:type", Function),
190
- tslib_1.__metadata("design:paramtypes", [String, String, Object]),
191
- tslib_1.__metadata("design:returntype", Promise)
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","jsonEqual","__decorate","FifoPromise"],"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,MAAM,GAAG,MAAM,CAAC,yBAAyB,CAAC;AAahD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BG;MACmB,eAAe,CAAA;AAC3B,IAAA,OAAO,SAAS,GAAG,MAAM,EAAE;AAC3B,IAAA,OAAO,WAAW,CAAU,IAAS,EAAE,OAAe,IAAK,EAAA;AACjE,QAA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ACvF;;AAnGMC,gBAAA,CAAA;AADL,IAAAC,uBAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;AAGhE,CAAA,EAAA,eAAA,CAAA,SAAA,EAAA,KAAA,EAAA,IAAA,CAAA;AAqBKD,gBAAA,CAAA;AADL,IAAAC,uBAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;AAMhE,CAAA,EAAA,eAAA,CAAA,SAAA,EAAA,OAAA,EAAA,IAAA,CAAA;AAgBKD,gBAAA,CAAA;AADL,IAAAC,uBAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;AAGhE,CAAA,EAAA,eAAA,CAAA,SAAA,EAAA,QAAA,EAAA,IAAA,CAAA;AAgBKD,gBAAA,CAAA;AADL,IAAAC,uBAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;AAKhE,CAAA,EAAA,eAAA,CAAA,SAAA,EAAA,MAAA,EAAA,IAAA,CAAA;AAqBKD,gBAAA,CAAA;AADL,IAAAC,uBAAW,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 };