@telperion/reactive-storage 1.1.0
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/LICENSE +21 -0
- package/README.md +307 -0
- package/async-iterator.polyfill.cjs +87 -0
- package/async-iterator.polyfill.d.ts +2 -0
- package/async-iterator.polyfill.js +86 -0
- package/async-iterator.polyfill.js.map +1 -0
- package/index.cjs +7 -0
- package/index.d.ts +4 -0
- package/index.js +6 -0
- package/index.js.map +1 -0
- package/package.json +68 -0
- package/reactive-storage.cjs +192 -0
- package/reactive-storage.d.ts +84 -0
- package/reactive-storage.js +190 -0
- package/reactive-storage.js.map +1 -0
- package/reactive-web-local-storage.cjs +19 -0
- package/reactive-web-local-storage.d.ts +12 -0
- package/reactive-web-local-storage.js +17 -0
- package/reactive-web-local-storage.js.map +1 -0
- package/reactive-web-session-storage.cjs +19 -0
- package/reactive-web-session-storage.d.ts +12 -0
- package/reactive-web-session-storage.js +17 -0
- package/reactive-web-session-storage.js.map +1 -0
- package/reactive-web-storage.cjs +62 -0
- package/reactive-web-storage.d.ts +30 -0
- package/reactive-web-storage.js +59 -0
- package/reactive-web-storage.js.map +1 -0
- package/regex-parser.cjs +29 -0
- package/regex-parser.d.ts +11 -0
- package/regex-parser.js +27 -0
- package/regex-parser.js.map +1 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* 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
|
+
const GET_ALL = Symbol('Reactive Storage Get All');
|
|
13
|
+
const CHANGES = Symbol('Reactive Storage Changes');
|
|
14
|
+
const SET = Symbol('Reactive Storage Set');
|
|
15
|
+
const REMOVE = Symbol('Reactive Storage Remove');
|
|
16
|
+
/**
|
|
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.
|
|
20
|
+
*
|
|
21
|
+
* @template S - Storage store name type (defaults to string)
|
|
22
|
+
*/
|
|
23
|
+
class ReactiveStorage {
|
|
24
|
+
static FifoGroup = Symbol();
|
|
25
|
+
static extractData(data, prop = null) {
|
|
26
|
+
if (!prop)
|
|
27
|
+
return data;
|
|
28
|
+
const fragments = (0, compact_1.compact)(prop.split('.'));
|
|
29
|
+
let value = data;
|
|
30
|
+
do {
|
|
31
|
+
try {
|
|
32
|
+
value = value[fragments[0]];
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
fragments.shift();
|
|
38
|
+
} while (fragments.length);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
static Props = { GET_ALL, CHANGES, SET, REMOVE };
|
|
42
|
+
#_stream$ = null;
|
|
43
|
+
#initializeStream() {
|
|
44
|
+
this.#_stream$ = (0, rxjs_1.from)(this[GET_ALL]()).pipe((0, rxjs_1.switchMap)(data => {
|
|
45
|
+
data ??= {};
|
|
46
|
+
return (0, rxjs_1.from)(this[CHANGES]).pipe((0, rxjs_1.map)(({ storeName, value }) => {
|
|
47
|
+
if (value === undefined)
|
|
48
|
+
delete data[storeName];
|
|
49
|
+
else
|
|
50
|
+
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)());
|
|
54
|
+
}
|
|
55
|
+
get #stream$() {
|
|
56
|
+
if (!this.#_stream$)
|
|
57
|
+
this.#initializeStream();
|
|
58
|
+
return this.#_stream$;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
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').
|
|
63
|
+
*
|
|
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
|
|
68
|
+
*/
|
|
69
|
+
get(storeName, key = '') {
|
|
70
|
+
return this.#stream$.pipe((0, rxjs_1.map)(allData => ReactiveStorage.extractData(allData, `${storeName}.${key}`)), (0, rxjs_1.distinctUntilChanged)());
|
|
71
|
+
}
|
|
72
|
+
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('.'));
|
|
76
|
+
if (!paths.length) {
|
|
77
|
+
if (value === undefined) {
|
|
78
|
+
await this[REMOVE](storeName);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
await this[SET](storeName, value);
|
|
82
|
+
}
|
|
83
|
+
await storeChangeEvent;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const newStore = JSON.parse(JSON.stringify(typeof store === 'object' ? store : {}));
|
|
87
|
+
let lastPiece = newStore;
|
|
88
|
+
while (paths.length > 1) {
|
|
89
|
+
if (typeof lastPiece[paths[0]] !== 'object')
|
|
90
|
+
lastPiece[paths[0]] = {};
|
|
91
|
+
lastPiece = lastPiece[paths[0]];
|
|
92
|
+
paths.shift();
|
|
93
|
+
}
|
|
94
|
+
if (lastPiece[paths[0]] === value)
|
|
95
|
+
return;
|
|
96
|
+
if (value === undefined) {
|
|
97
|
+
delete lastPiece[paths[0]];
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
lastPiece[paths[0]] = value;
|
|
101
|
+
}
|
|
102
|
+
await this[SET](storeName, newStore);
|
|
103
|
+
await storeChangeEvent;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Set a value in storage. Overwrites existing value.
|
|
108
|
+
* Pass undefined as value to remove the property.
|
|
109
|
+
*
|
|
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)
|
|
113
|
+
*/
|
|
114
|
+
async set(storeName, key, value) {
|
|
115
|
+
return await this.#set(storeName, key, value);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Merge a partial object into existing value.
|
|
119
|
+
* Only works with object values.
|
|
120
|
+
*
|
|
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
|
|
124
|
+
*/
|
|
125
|
+
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)));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Delete a value from storage.
|
|
131
|
+
*
|
|
132
|
+
* @param storeName - Name of the storage store
|
|
133
|
+
* @param key - Property path using dot notation (empty string deletes entire store)
|
|
134
|
+
*/
|
|
135
|
+
async delete(storeName, key = '') {
|
|
136
|
+
return await this.#set(storeName, key, undefined);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Add items to an array in storage.
|
|
140
|
+
* Creates array if it doesn't exist.
|
|
141
|
+
*
|
|
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
|
|
145
|
+
*/
|
|
146
|
+
async push(storeName, key, ...values) {
|
|
147
|
+
const existing = await (0, rxjs_1.firstValueFrom)(this.get(storeName, key)) ?? [];
|
|
148
|
+
await this.#set(storeName, key, [...existing, ...values]);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Remove a specific value from an array in storage.
|
|
152
|
+
*
|
|
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
|
|
156
|
+
*/
|
|
157
|
+
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));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
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)
|
|
168
|
+
], 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)
|
|
174
|
+
], 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)
|
|
180
|
+
], 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)
|
|
186
|
+
], 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)
|
|
192
|
+
], ReactiveStorage.prototype, "drop", null);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
declare const GET_ALL: unique symbol;
|
|
3
|
+
declare const CHANGES: unique symbol;
|
|
4
|
+
declare const SET: unique symbol;
|
|
5
|
+
declare const REMOVE: unique symbol;
|
|
6
|
+
export interface ReactiveStorageChangeEvent<S = string> {
|
|
7
|
+
storeName: S;
|
|
8
|
+
value: any;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Abstract base class for reactive storage implementations.
|
|
12
|
+
* Provides RxJS-based reactive API for storage with support for nested property access.
|
|
13
|
+
* All write operations are queued using FIFO promises to ensure consistency.
|
|
14
|
+
*
|
|
15
|
+
* @template S - Storage store name type (defaults to string)
|
|
16
|
+
*/
|
|
17
|
+
export declare abstract class ReactiveStorage<S extends string = string> {
|
|
18
|
+
#private;
|
|
19
|
+
private static FifoGroup;
|
|
20
|
+
private static extractData;
|
|
21
|
+
static Props: {
|
|
22
|
+
readonly GET_ALL: typeof GET_ALL;
|
|
23
|
+
readonly CHANGES: typeof CHANGES;
|
|
24
|
+
readonly SET: typeof SET;
|
|
25
|
+
readonly REMOVE: typeof REMOVE;
|
|
26
|
+
};
|
|
27
|
+
protected abstract [GET_ALL](): Promise<any>;
|
|
28
|
+
protected abstract [CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;
|
|
29
|
+
protected abstract [SET](store: S, value: any): Promise<void>;
|
|
30
|
+
protected abstract [REMOVE](store: S): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Get an observable that emits the current value and all future changes.
|
|
33
|
+
* Supports dot notation for nested property access (e.g., 'user.settings.theme').
|
|
34
|
+
*
|
|
35
|
+
* @template T - Expected return type
|
|
36
|
+
* @param storeName - Name of the storage store
|
|
37
|
+
* @param key - Property path using dot notation (empty string for entire store)
|
|
38
|
+
* @returns Observable that emits current value and all updates
|
|
39
|
+
*/
|
|
40
|
+
get<T = Record<string, any>>(storeName: S, key?: string): Observable<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Set a value in storage. Overwrites existing value.
|
|
43
|
+
* Pass undefined as value to remove the property.
|
|
44
|
+
*
|
|
45
|
+
* @param storeName - Name of the storage store
|
|
46
|
+
* @param key - Property path using dot notation
|
|
47
|
+
* @param value - Value to set (undefined removes the value)
|
|
48
|
+
*/
|
|
49
|
+
set(storeName: S, key: string, value: any): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Merge a partial object into existing value.
|
|
52
|
+
* Only works with object values.
|
|
53
|
+
*
|
|
54
|
+
* @param storeName - Name of the storage store
|
|
55
|
+
* @param key - Property path using dot notation
|
|
56
|
+
* @param value - Partial object to merge with existing value
|
|
57
|
+
*/
|
|
58
|
+
patch(storeName: S, key: string, value: any): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Delete a value from storage.
|
|
61
|
+
*
|
|
62
|
+
* @param storeName - Name of the storage store
|
|
63
|
+
* @param key - Property path using dot notation (empty string deletes entire store)
|
|
64
|
+
*/
|
|
65
|
+
delete(storeName: S, key?: string): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Add items to an array in storage.
|
|
68
|
+
* Creates array if it doesn't exist.
|
|
69
|
+
*
|
|
70
|
+
* @param storeName - Name of the storage store
|
|
71
|
+
* @param key - Path to array property using dot notation
|
|
72
|
+
* @param values - Values to push into the array
|
|
73
|
+
*/
|
|
74
|
+
push(storeName: S, key: string, ...values: any[]): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Remove a specific value from an array in storage.
|
|
77
|
+
*
|
|
78
|
+
* @param storeName - Name of the storage store
|
|
79
|
+
* @param key - Path to array property using dot notation
|
|
80
|
+
* @param value - Value to remove from the array
|
|
81
|
+
*/
|
|
82
|
+
drop(storeName: S, key: string, value: any): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/* 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
|
+
const GET_ALL = Symbol('Reactive Storage Get All');
|
|
10
|
+
const CHANGES = Symbol('Reactive Storage Changes');
|
|
11
|
+
const SET = Symbol('Reactive Storage Set');
|
|
12
|
+
const REMOVE = Symbol('Reactive Storage Remove');
|
|
13
|
+
/**
|
|
14
|
+
* Abstract base class for reactive storage implementations.
|
|
15
|
+
* Provides RxJS-based reactive API for storage with support for nested property access.
|
|
16
|
+
* All write operations are queued using FIFO promises to ensure consistency.
|
|
17
|
+
*
|
|
18
|
+
* @template S - Storage store name type (defaults to string)
|
|
19
|
+
*/
|
|
20
|
+
export class ReactiveStorage {
|
|
21
|
+
static FifoGroup = Symbol();
|
|
22
|
+
static extractData(data, prop = null) {
|
|
23
|
+
if (!prop)
|
|
24
|
+
return data;
|
|
25
|
+
const fragments = compact(prop.split('.'));
|
|
26
|
+
let value = data;
|
|
27
|
+
do {
|
|
28
|
+
try {
|
|
29
|
+
value = value[fragments[0]];
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
fragments.shift();
|
|
35
|
+
} while (fragments.length);
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
static Props = { GET_ALL, CHANGES, SET, REMOVE };
|
|
39
|
+
#_stream$ = null;
|
|
40
|
+
#initializeStream() {
|
|
41
|
+
this.#_stream$ = from(this[GET_ALL]()).pipe(switchMap(data => {
|
|
42
|
+
data ??= {};
|
|
43
|
+
return from(this[CHANGES]).pipe(map(({ storeName, value }) => {
|
|
44
|
+
if (value === undefined)
|
|
45
|
+
delete data[storeName];
|
|
46
|
+
else
|
|
47
|
+
data[storeName] = value;
|
|
48
|
+
return deepFreeze({ ...data });
|
|
49
|
+
}), startWith(deepFreeze({ ...data })));
|
|
50
|
+
}), shareLast());
|
|
51
|
+
}
|
|
52
|
+
get #stream$() {
|
|
53
|
+
if (!this.#_stream$)
|
|
54
|
+
this.#initializeStream();
|
|
55
|
+
return this.#_stream$;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get an observable that emits the current value and all future changes.
|
|
59
|
+
* Supports dot notation for nested property access (e.g., 'user.settings.theme').
|
|
60
|
+
*
|
|
61
|
+
* @template T - Expected return type
|
|
62
|
+
* @param storeName - Name of the storage store
|
|
63
|
+
* @param key - Property path using dot notation (empty string for entire store)
|
|
64
|
+
* @returns Observable that emits current value and all updates
|
|
65
|
+
*/
|
|
66
|
+
get(storeName, key = '') {
|
|
67
|
+
return this.#stream$.pipe(map(allData => ReactiveStorage.extractData(allData, `${storeName}.${key}`)), distinctUntilChanged());
|
|
68
|
+
}
|
|
69
|
+
async #set(storeName, key, value) {
|
|
70
|
+
const store = await firstValueFrom(this.get(storeName));
|
|
71
|
+
const storeChangeEvent = lastValueFrom(this.get(storeName).pipe(take(2)));
|
|
72
|
+
const paths = compact(key.split('.'));
|
|
73
|
+
if (!paths.length) {
|
|
74
|
+
if (value === undefined) {
|
|
75
|
+
await this[REMOVE](storeName);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
await this[SET](storeName, value);
|
|
79
|
+
}
|
|
80
|
+
await storeChangeEvent;
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const newStore = JSON.parse(JSON.stringify(typeof store === 'object' ? store : {}));
|
|
84
|
+
let lastPiece = newStore;
|
|
85
|
+
while (paths.length > 1) {
|
|
86
|
+
if (typeof lastPiece[paths[0]] !== 'object')
|
|
87
|
+
lastPiece[paths[0]] = {};
|
|
88
|
+
lastPiece = lastPiece[paths[0]];
|
|
89
|
+
paths.shift();
|
|
90
|
+
}
|
|
91
|
+
if (lastPiece[paths[0]] === value)
|
|
92
|
+
return;
|
|
93
|
+
if (value === undefined) {
|
|
94
|
+
delete lastPiece[paths[0]];
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
lastPiece[paths[0]] = value;
|
|
98
|
+
}
|
|
99
|
+
await this[SET](storeName, newStore);
|
|
100
|
+
await storeChangeEvent;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Set a value in storage. Overwrites existing value.
|
|
105
|
+
* Pass undefined as value to remove the property.
|
|
106
|
+
*
|
|
107
|
+
* @param storeName - Name of the storage store
|
|
108
|
+
* @param key - Property path using dot notation
|
|
109
|
+
* @param value - Value to set (undefined removes the value)
|
|
110
|
+
*/
|
|
111
|
+
async set(storeName, key, value) {
|
|
112
|
+
return await this.#set(storeName, key, value);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Merge a partial object into existing value.
|
|
116
|
+
* Only works with object values.
|
|
117
|
+
*
|
|
118
|
+
* @param storeName - Name of the storage store
|
|
119
|
+
* @param key - Property path using dot notation
|
|
120
|
+
* @param value - Partial object to merge with existing value
|
|
121
|
+
*/
|
|
122
|
+
async patch(storeName, key, value) {
|
|
123
|
+
const existing = await firstValueFrom(this.get(storeName, key));
|
|
124
|
+
await this.#set(storeName, key, Object.assign({}, existing, compact(value)));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Delete a value from storage.
|
|
128
|
+
*
|
|
129
|
+
* @param storeName - Name of the storage store
|
|
130
|
+
* @param key - Property path using dot notation (empty string deletes entire store)
|
|
131
|
+
*/
|
|
132
|
+
async delete(storeName, key = '') {
|
|
133
|
+
return await this.#set(storeName, key, undefined);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Add items to an array in storage.
|
|
137
|
+
* Creates array if it doesn't exist.
|
|
138
|
+
*
|
|
139
|
+
* @param storeName - Name of the storage store
|
|
140
|
+
* @param key - Path to array property using dot notation
|
|
141
|
+
* @param values - Values to push into the array
|
|
142
|
+
*/
|
|
143
|
+
async push(storeName, key, ...values) {
|
|
144
|
+
const existing = await firstValueFrom(this.get(storeName, key)) ?? [];
|
|
145
|
+
await this.#set(storeName, key, [...existing, ...values]);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Remove a specific value from an array in storage.
|
|
149
|
+
*
|
|
150
|
+
* @param storeName - Name of the storage store
|
|
151
|
+
* @param key - Path to array property using dot notation
|
|
152
|
+
* @param value - Value to remove from the array
|
|
153
|
+
*/
|
|
154
|
+
async drop(storeName, key, value) {
|
|
155
|
+
const existing = await firstValueFrom(this.get(storeName, key));
|
|
156
|
+
await this.#set(storeName, key, remove(existing, value));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
__decorate([
|
|
160
|
+
FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
161
|
+
__metadata("design:type", Function),
|
|
162
|
+
__metadata("design:paramtypes", [String, String, Object]),
|
|
163
|
+
__metadata("design:returntype", Promise)
|
|
164
|
+
], ReactiveStorage.prototype, "set", null);
|
|
165
|
+
__decorate([
|
|
166
|
+
FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
167
|
+
__metadata("design:type", Function),
|
|
168
|
+
__metadata("design:paramtypes", [String, String, Object]),
|
|
169
|
+
__metadata("design:returntype", Promise)
|
|
170
|
+
], ReactiveStorage.prototype, "patch", null);
|
|
171
|
+
__decorate([
|
|
172
|
+
FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
173
|
+
__metadata("design:type", Function),
|
|
174
|
+
__metadata("design:paramtypes", [String, Object]),
|
|
175
|
+
__metadata("design:returntype", Promise)
|
|
176
|
+
], ReactiveStorage.prototype, "delete", null);
|
|
177
|
+
__decorate([
|
|
178
|
+
FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
179
|
+
__metadata("design:type", Function),
|
|
180
|
+
__metadata("design:paramtypes", [String, String, Object]),
|
|
181
|
+
__metadata("design:returntype", Promise)
|
|
182
|
+
], ReactiveStorage.prototype, "push", null);
|
|
183
|
+
__decorate([
|
|
184
|
+
FifoPromise({ group: ReactiveStorage.FifoGroup, timeout: 1000 }),
|
|
185
|
+
__metadata("design:type", Function),
|
|
186
|
+
__metadata("design:paramtypes", [String, String, Object]),
|
|
187
|
+
__metadata("design:returntype", Promise)
|
|
188
|
+
], ReactiveStorage.prototype, "drop", null);
|
|
189
|
+
|
|
190
|
+
//# sourceMappingURL=reactive-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/reactive-storage/src/reactive-storage.ts"],"names":[],"mappings":"AAAA,uDAAuD;;AAEvD,OAAO,EAAc,oBAAoB,EAAE,cAAc,EAAE,IAAI,E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,GAAG,EAAE,CAAC,GAAG,QAAQ,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;OAMG;IAEH,AAAM,KAAD,CAAC,IAAI,CAAC,SAAY,EAAE,GAAW,EAAE,KAAU;QAC9C,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,IAAI,CAAC,GAAG,CAAQ,SAAS,EAAE,GAAG,CAAC,CAAC,CAAC;QAEvE,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3D,CAAC;;AAzDK;IADL,WAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;0CAGhE;AAWK;IADL,WAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;4CAKhE;AASK;IADL,WAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;6CAGhE;AAWK;IADL,WAAW,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;;;2CAKhE;AAUK;IADL,WAAW,CAAC,EAAE,KAAK,EAAE,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"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReactiveWebLocalStorage = void 0;
|
|
4
|
+
const reactive_web_storage_1 = require("./reactive-web-storage");
|
|
5
|
+
/**
|
|
6
|
+
* Reactive wrapper for browser localStorage.
|
|
7
|
+
* Data persists across browser sessions.
|
|
8
|
+
*
|
|
9
|
+
* @template S - Storage store name type
|
|
10
|
+
*/
|
|
11
|
+
class ReactiveWebLocalStorage extends reactive_web_storage_1.AbstractReactiveWebStorage {
|
|
12
|
+
appName;
|
|
13
|
+
storage = window.localStorage;
|
|
14
|
+
constructor(appName = 'app') {
|
|
15
|
+
super();
|
|
16
|
+
this.appName = appName;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.ReactiveWebLocalStorage = ReactiveWebLocalStorage;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractReactiveWebStorage } from "./reactive-web-storage";
|
|
2
|
+
/**
|
|
3
|
+
* Reactive wrapper for browser localStorage.
|
|
4
|
+
* Data persists across browser sessions.
|
|
5
|
+
*
|
|
6
|
+
* @template S - Storage store name type
|
|
7
|
+
*/
|
|
8
|
+
export declare class ReactiveWebLocalStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
9
|
+
readonly appName: string;
|
|
10
|
+
readonly storage: Storage;
|
|
11
|
+
constructor(appName?: string);
|
|
12
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AbstractReactiveWebStorage } from "./reactive-web-storage";
|
|
2
|
+
/**
|
|
3
|
+
* Reactive wrapper for browser localStorage.
|
|
4
|
+
* Data persists across browser sessions.
|
|
5
|
+
*
|
|
6
|
+
* @template S - Storage store name type
|
|
7
|
+
*/
|
|
8
|
+
export class ReactiveWebLocalStorage extends AbstractReactiveWebStorage {
|
|
9
|
+
appName;
|
|
10
|
+
storage = window.localStorage;
|
|
11
|
+
constructor(appName = 'app') {
|
|
12
|
+
super();
|
|
13
|
+
this.appName = appName;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=reactive-web-local-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/reactive-storage/src/reactive-web-local-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AAEpE;;;;;GAKG;AACH,MAAM,OAAO,uBAAmD,SAAQ,0BAA6B;IAI/E;IAHF,OAAO,GAAG,MAAM,CAAC,YAAY,CAAC;IAEhD,YACoB,UAAU,KAAK;QAEjC,KAAK,EAAE,CAAC;QAFU,YAAO,GAAP,OAAO,CAAQ;IAGnC,CAAC;CACF","file":"reactive-web-local-storage.js","sourcesContent":["import { AbstractReactiveWebStorage } from \"./reactive-web-storage\";\n\n/**\n * Reactive wrapper for browser localStorage.\n * Data persists across browser sessions.\n *\n * @template S - Storage store name type\n */\nexport class ReactiveWebLocalStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {\n override readonly storage = window.localStorage;\n\n constructor(\n override readonly appName = 'app'\n ) {\n super();\n }\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReactiveWebSessionStorage = void 0;
|
|
4
|
+
const reactive_web_storage_1 = require("./reactive-web-storage");
|
|
5
|
+
/**
|
|
6
|
+
* Reactive wrapper for browser sessionStorage.
|
|
7
|
+
* Data is cleared when the browser tab is closed.
|
|
8
|
+
*
|
|
9
|
+
* @template S - Storage store name type
|
|
10
|
+
*/
|
|
11
|
+
class ReactiveWebSessionStorage extends reactive_web_storage_1.AbstractReactiveWebStorage {
|
|
12
|
+
appName;
|
|
13
|
+
storage = window.sessionStorage;
|
|
14
|
+
constructor(appName = 'app') {
|
|
15
|
+
super();
|
|
16
|
+
this.appName = appName;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.ReactiveWebSessionStorage = ReactiveWebSessionStorage;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractReactiveWebStorage } from "./reactive-web-storage";
|
|
2
|
+
/**
|
|
3
|
+
* Reactive wrapper for browser sessionStorage.
|
|
4
|
+
* Data is cleared when the browser tab is closed.
|
|
5
|
+
*
|
|
6
|
+
* @template S - Storage store name type
|
|
7
|
+
*/
|
|
8
|
+
export declare class ReactiveWebSessionStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
9
|
+
readonly appName: string;
|
|
10
|
+
readonly storage: Storage;
|
|
11
|
+
constructor(appName?: string);
|
|
12
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AbstractReactiveWebStorage } from "./reactive-web-storage";
|
|
2
|
+
/**
|
|
3
|
+
* Reactive wrapper for browser sessionStorage.
|
|
4
|
+
* Data is cleared when the browser tab is closed.
|
|
5
|
+
*
|
|
6
|
+
* @template S - Storage store name type
|
|
7
|
+
*/
|
|
8
|
+
export class ReactiveWebSessionStorage extends AbstractReactiveWebStorage {
|
|
9
|
+
appName;
|
|
10
|
+
storage = window.sessionStorage;
|
|
11
|
+
constructor(appName = 'app') {
|
|
12
|
+
super();
|
|
13
|
+
this.appName = appName;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=reactive-web-session-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/reactive-storage/src/reactive-web-session-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AAEpE;;;;;GAKG;AACH,MAAM,OAAO,yBAAqD,SAAQ,0BAA6B;IAIjF;IAHF,OAAO,GAAG,MAAM,CAAC,cAAc,CAAC;IAElD,YACoB,UAAU,KAAK;QAEjC,KAAK,EAAE,CAAC;QAFU,YAAO,GAAP,OAAO,CAAQ;IAGnC,CAAC;CACF","file":"reactive-web-session-storage.js","sourcesContent":["import { AbstractReactiveWebStorage } from \"./reactive-web-storage\";\n\n/**\n * Reactive wrapper for browser sessionStorage.\n * Data is cleared when the browser tab is closed.\n *\n * @template S - Storage store name type\n */\nexport class ReactiveWebSessionStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {\n override readonly storage = window.sessionStorage;\n\n constructor(\n override readonly appName = 'app'\n ) {\n super();\n }\n}\n"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReactiveWebStorage = exports.AbstractReactiveWebStorage = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const reactive_storage_1 = require("./reactive-storage");
|
|
6
|
+
const regex_parser_1 = require("./regex-parser");
|
|
7
|
+
const async_iterator_polyfill_1 = require("./async-iterator.polyfill");
|
|
8
|
+
/**
|
|
9
|
+
* Abstract implementation of ReactiveStorage for browser Web Storage API.
|
|
10
|
+
* Stores data as JSON in localStorage or sessionStorage with app namespacing.
|
|
11
|
+
*
|
|
12
|
+
* @template S - Storage store name type
|
|
13
|
+
*/
|
|
14
|
+
class AbstractReactiveWebStorage extends reactive_storage_1.ReactiveStorage {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
static JsonParse(string) {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(string);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
get #keyNameRegex() {
|
|
25
|
+
return (0, regex_parser_1.RegexParser)(`^${this.appName}\\/\\w+$`);
|
|
26
|
+
}
|
|
27
|
+
#changes$ = new rxjs_1.Subject();
|
|
28
|
+
async [reactive_storage_1.ReactiveStorage.Props.GET_ALL]() {
|
|
29
|
+
const keys = Object.keys(this.storage).filter(key => this.#keyNameRegex.test(key));
|
|
30
|
+
return Object.fromEntries(keys.map(key => [
|
|
31
|
+
key.split('/')[1],
|
|
32
|
+
AbstractReactiveWebStorage.JsonParse(this.storage.getItem(key))
|
|
33
|
+
]));
|
|
34
|
+
}
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
[reactive_storage_1.ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator] ?? (0, async_iterator_polyfill_1.toAsyncIterator)(this.#changes$);
|
|
37
|
+
async [reactive_storage_1.ReactiveStorage.Props.SET](storeName, value) {
|
|
38
|
+
this.storage.setItem(`${this.appName}/${storeName}`, JSON.stringify(value));
|
|
39
|
+
this.#changes$.next({ storeName, value });
|
|
40
|
+
}
|
|
41
|
+
async [reactive_storage_1.ReactiveStorage.Props.REMOVE](storeName) {
|
|
42
|
+
this.storage.removeItem(`${this.appName}/${storeName}`);
|
|
43
|
+
this.#changes$.next({ storeName, value: undefined });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.AbstractReactiveWebStorage = AbstractReactiveWebStorage;
|
|
47
|
+
/**
|
|
48
|
+
* Generic reactive web storage wrapper.
|
|
49
|
+
* Allows using any Storage implementation (localStorage, sessionStorage, or custom).
|
|
50
|
+
*
|
|
51
|
+
* @template S - Storage store name type
|
|
52
|
+
*/
|
|
53
|
+
class ReactiveWebStorage extends AbstractReactiveWebStorage {
|
|
54
|
+
storage;
|
|
55
|
+
appName;
|
|
56
|
+
constructor(storage, appName = 'app') {
|
|
57
|
+
super();
|
|
58
|
+
this.storage = storage;
|
|
59
|
+
this.appName = appName;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.ReactiveWebStorage = ReactiveWebStorage;
|