@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
|
@@ -1,17 +1,39 @@
|
|
|
1
|
-
import { AbstractReactiveWebStorage } from
|
|
1
|
+
import { AbstractReactiveWebStorage } from './reactive-web-storage.js';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
|
-
* Reactive
|
|
4
|
-
*
|
|
4
|
+
* Reactive storage on `localStorage`: data stays until it is removed, across tabs and browser restarts.
|
|
5
|
+
*
|
|
6
|
+
* Each store is one localStorage key, `<appName>/<storeName>`, holding the store as JSON.
|
|
7
|
+
* Existing keys of the same app are read when the first value is requested.
|
|
8
|
+
*
|
|
9
|
+
* Changes made in other tabs, or by other instances, are not observed.
|
|
10
|
+
*
|
|
11
|
+
* Example usage:
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { ReactiveWebLocalStorage } from '@telperion/reactive-storage/reactive-web-local-storage';
|
|
14
|
+
*
|
|
15
|
+
* const storage = new ReactiveWebLocalStorage<'settings'>('my-app');
|
|
16
|
+
*
|
|
17
|
+
* storage.get<'light' | 'dark'>('settings', 'theme').subscribe((theme) => {
|
|
18
|
+
* document.documentElement.dataset.theme = theme ?? 'light';
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* await storage.set('settings', 'theme', 'dark'); // saved as my-app/settings = {"theme":"dark"}
|
|
22
|
+
* ```
|
|
5
23
|
*
|
|
6
|
-
* @template S -
|
|
24
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
7
25
|
*/
|
|
8
|
-
|
|
26
|
+
class ReactiveWebLocalStorage extends AbstractReactiveWebStorage {
|
|
9
27
|
appName;
|
|
10
28
|
storage = window.localStorage;
|
|
29
|
+
/**
|
|
30
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
31
|
+
*/
|
|
11
32
|
constructor(appName = 'app') {
|
|
12
33
|
super();
|
|
13
34
|
this.appName = appName;
|
|
14
35
|
}
|
|
15
36
|
}
|
|
16
37
|
|
|
38
|
+
export { ReactiveWebLocalStorage };
|
|
17
39
|
//# sourceMappingURL=reactive-web-local-storage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"file":"reactive-web-local-storage.js","sources":["../../../libs/reactive-storage/src/reactive-web-local-storage.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,MAAO,uBAAmD,SAAQ,0BAA6B,CAAA;AAO/E,IAAA,OAAA;AANF,IAAA,OAAO,GAAG,MAAM,CAAC,YAAY;AAE/C;;AAEG;AACH,IAAA,WAAA,CACoB,UAAU,KAAK,EAAA;AAEjC,QAAA,KAAK,EAAE;QAFW,IAAA,CAAA,OAAO,GAAP,OAAO;IAG3B;AACD;;;;"}
|
|
@@ -1,19 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var reactiveWebStorage = require('./reactive-web-storage.cjs');
|
|
4
|
+
|
|
5
5
|
/**
|
|
6
|
-
* Reactive
|
|
7
|
-
* Data is cleared when the browser tab is closed.
|
|
6
|
+
* Reactive storage on `sessionStorage`: data belongs to the tab and is cleared when the tab is closed.
|
|
8
7
|
*
|
|
9
|
-
* @
|
|
8
|
+
* Works exactly like {@link ReactiveWebLocalStorage}, with the same `<appName>/<storeName>` keys;
|
|
9
|
+
* only the underlying storage differs.
|
|
10
|
+
*
|
|
11
|
+
* Example usage:
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { ReactiveWebSessionStorage } from '@telperion/reactive-storage/reactive-web-session-storage';
|
|
14
|
+
*
|
|
15
|
+
* const draft = new ReactiveWebSessionStorage('my-app');
|
|
16
|
+
*
|
|
17
|
+
* await draft.set('checkout', 'step', 2); // survives a reload, not a closed tab
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
10
21
|
*/
|
|
11
|
-
class ReactiveWebSessionStorage extends
|
|
22
|
+
class ReactiveWebSessionStorage extends reactiveWebStorage.AbstractReactiveWebStorage {
|
|
12
23
|
appName;
|
|
13
24
|
storage = window.sessionStorage;
|
|
25
|
+
/**
|
|
26
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
27
|
+
*/
|
|
14
28
|
constructor(appName = 'app') {
|
|
15
29
|
super();
|
|
16
30
|
this.appName = appName;
|
|
17
31
|
}
|
|
18
32
|
}
|
|
33
|
+
|
|
19
34
|
exports.ReactiveWebSessionStorage = ReactiveWebSessionStorage;
|
|
35
|
+
//# sourceMappingURL=reactive-web-session-storage.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive-web-session-storage.cjs","sources":["../../../libs/reactive-storage/src/reactive-web-session-storage.ts"],"sourcesContent":[null],"names":["AbstractReactiveWebStorage"],"mappings":";;;;AAEA;;;;;;;;;;;;;;;;AAgBG;AACG,MAAO,yBAAqD,SAAQA,6CAA6B,CAAA;AAOjF,IAAA,OAAA;AANF,IAAA,OAAO,GAAG,MAAM,CAAC,cAAc;AAEjD;;AAEG;AACH,IAAA,WAAA,CACoB,UAAU,KAAK,EAAA;AAEjC,QAAA,KAAK,EAAE;QAFW,IAAA,CAAA,OAAO,GAAP,OAAO;IAG3B;AACD;;;;"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AbstractReactiveWebStorage } from './reactive-web-storage.cjs';
|
|
2
|
+
import './reactive-storage.cjs';
|
|
3
|
+
import 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Reactive storage on `sessionStorage`: data belongs to the tab and is cleared when the tab is closed.
|
|
7
|
+
*
|
|
8
|
+
* Works exactly like {@link ReactiveWebLocalStorage}, with the same `<appName>/<storeName>` keys;
|
|
9
|
+
* only the underlying storage differs.
|
|
10
|
+
*
|
|
11
|
+
* Example usage:
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { ReactiveWebSessionStorage } from '@telperion/reactive-storage/reactive-web-session-storage';
|
|
14
|
+
*
|
|
15
|
+
* const draft = new ReactiveWebSessionStorage('my-app');
|
|
16
|
+
*
|
|
17
|
+
* await draft.set('checkout', 'step', 2); // survives a reload, not a closed tab
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
21
|
+
*/
|
|
22
|
+
declare class ReactiveWebSessionStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
23
|
+
readonly appName: string;
|
|
24
|
+
readonly storage: Storage;
|
|
25
|
+
/**
|
|
26
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
27
|
+
*/
|
|
28
|
+
constructor(appName?: string);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { ReactiveWebSessionStorage };
|
|
@@ -1,12 +1,31 @@
|
|
|
1
|
-
import { AbstractReactiveWebStorage } from
|
|
1
|
+
import { AbstractReactiveWebStorage } from './reactive-web-storage.js';
|
|
2
|
+
import './reactive-storage.js';
|
|
3
|
+
import 'rxjs';
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
|
-
* Reactive
|
|
4
|
-
* Data is cleared when the browser tab is closed.
|
|
6
|
+
* Reactive storage on `sessionStorage`: data belongs to the tab and is cleared when the tab is closed.
|
|
5
7
|
*
|
|
6
|
-
* @
|
|
8
|
+
* Works exactly like {@link ReactiveWebLocalStorage}, with the same `<appName>/<storeName>` keys;
|
|
9
|
+
* only the underlying storage differs.
|
|
10
|
+
*
|
|
11
|
+
* Example usage:
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { ReactiveWebSessionStorage } from '@telperion/reactive-storage/reactive-web-session-storage';
|
|
14
|
+
*
|
|
15
|
+
* const draft = new ReactiveWebSessionStorage('my-app');
|
|
16
|
+
*
|
|
17
|
+
* await draft.set('checkout', 'step', 2); // survives a reload, not a closed tab
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
7
21
|
*/
|
|
8
|
-
|
|
22
|
+
declare class ReactiveWebSessionStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
9
23
|
readonly appName: string;
|
|
10
24
|
readonly storage: Storage;
|
|
25
|
+
/**
|
|
26
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
27
|
+
*/
|
|
11
28
|
constructor(appName?: string);
|
|
12
29
|
}
|
|
30
|
+
|
|
31
|
+
export { ReactiveWebSessionStorage };
|
|
@@ -1,17 +1,33 @@
|
|
|
1
|
-
import { AbstractReactiveWebStorage } from
|
|
1
|
+
import { AbstractReactiveWebStorage } from './reactive-web-storage.js';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
|
-
* Reactive
|
|
4
|
-
*
|
|
4
|
+
* Reactive storage on `sessionStorage`: data belongs to the tab and is cleared when the tab is closed.
|
|
5
|
+
*
|
|
6
|
+
* Works exactly like {@link ReactiveWebLocalStorage}, with the same `<appName>/<storeName>` keys;
|
|
7
|
+
* only the underlying storage differs.
|
|
8
|
+
*
|
|
9
|
+
* Example usage:
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { ReactiveWebSessionStorage } from '@telperion/reactive-storage/reactive-web-session-storage';
|
|
12
|
+
*
|
|
13
|
+
* const draft = new ReactiveWebSessionStorage('my-app');
|
|
14
|
+
*
|
|
15
|
+
* await draft.set('checkout', 'step', 2); // survives a reload, not a closed tab
|
|
16
|
+
* ```
|
|
5
17
|
*
|
|
6
|
-
* @template S -
|
|
18
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
7
19
|
*/
|
|
8
|
-
|
|
20
|
+
class ReactiveWebSessionStorage extends AbstractReactiveWebStorage {
|
|
9
21
|
appName;
|
|
10
22
|
storage = window.sessionStorage;
|
|
23
|
+
/**
|
|
24
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
25
|
+
*/
|
|
11
26
|
constructor(appName = 'app') {
|
|
12
27
|
super();
|
|
13
28
|
this.appName = appName;
|
|
14
29
|
}
|
|
15
30
|
}
|
|
16
31
|
|
|
32
|
+
export { ReactiveWebSessionStorage };
|
|
17
33
|
//# sourceMappingURL=reactive-web-session-storage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"file":"reactive-web-session-storage.js","sources":["../../../libs/reactive-storage/src/reactive-web-session-storage.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;AAgBG;AACG,MAAO,yBAAqD,SAAQ,0BAA6B,CAAA;AAOjF,IAAA,OAAA;AANF,IAAA,OAAO,GAAG,MAAM,CAAC,cAAc;AAEjD;;AAEG;AACH,IAAA,WAAA,CACoB,UAAU,KAAK,EAAA;AAEjC,QAAA,KAAK,EAAE;QAFW,IAAA,CAAA,OAAO,GAAP,OAAO;IAG3B;AACD;;;;"}
|
package/reactive-web-storage.cjs
CHANGED
|
@@ -1,58 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const json_parse_1 = require("./json-parse");
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rxjs = require('rxjs');
|
|
4
|
+
var toAsyncIteratable = require('@telperion/rx-utils/to-async-iteratable');
|
|
5
|
+
var reactiveStorage = require('./reactive-storage.cjs');
|
|
6
|
+
var jsonParse = require('./json-parse.cjs');
|
|
7
|
+
|
|
9
8
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* Base class of the Web Storage implementations: saves each store as JSON under `<appName>/<storeName>`
|
|
10
|
+
* in `storage`. Extend it to back a reactive storage with another `Storage` object.
|
|
12
11
|
*
|
|
13
|
-
* @template S -
|
|
12
|
+
* @template S - Store name type
|
|
14
13
|
*/
|
|
15
|
-
class AbstractReactiveWebStorage extends
|
|
14
|
+
class AbstractReactiveWebStorage extends reactiveStorage.ReactiveStorage {
|
|
16
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
16
|
static #parse(string) {
|
|
18
|
-
return
|
|
17
|
+
return jsonParse.jsonParse(string);
|
|
19
18
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
#changes$ = new rxjs.Subject();
|
|
20
|
+
async [reactiveStorage.ReactiveStorage.Props.GET_ALL]() {
|
|
21
|
+
const prefix = `${this.appName}/`;
|
|
22
|
+
// Through the Storage interface, so custom Storage implementations are read too
|
|
23
|
+
const keys = Array.from({ length: this.storage.length }, (_, index) => this.storage.key(index))
|
|
24
|
+
.filter((key) => !!key && key.startsWith(prefix) && key.length > prefix.length);
|
|
26
25
|
return Object.fromEntries(keys.map(key => [
|
|
27
|
-
key.
|
|
26
|
+
key.slice(prefix.length),
|
|
28
27
|
AbstractReactiveWebStorage.#parse(this.storage.getItem(key))
|
|
29
28
|
]));
|
|
30
29
|
}
|
|
30
|
+
// Prefer an Observable's own async iterator (rxjs 8, or `@telperion/rx-utils/static/async-iterator`)
|
|
31
31
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
-
[
|
|
33
|
-
async [
|
|
32
|
+
[reactiveStorage.ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator]?.() ?? toAsyncIteratable.makeAsyncIterator(this.#changes$);
|
|
33
|
+
async [reactiveStorage.ReactiveStorage.Props.SET](storeName, value) {
|
|
34
34
|
this.storage.setItem(`${this.appName}/${storeName}`, JSON.stringify(value));
|
|
35
35
|
this.#changes$.next({ storeName, value });
|
|
36
36
|
}
|
|
37
|
-
async [
|
|
37
|
+
async [reactiveStorage.ReactiveStorage.Props.REMOVE](storeName) {
|
|
38
38
|
this.storage.removeItem(`${this.appName}/${storeName}`);
|
|
39
39
|
this.#changes$.next({ storeName, value: undefined });
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
exports.AbstractReactiveWebStorage = AbstractReactiveWebStorage;
|
|
43
42
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
43
|
+
* Reactive storage on any `Storage` object you pass in: `localStorage`, `sessionStorage`, or your own
|
|
44
|
+
* implementation of the Web Storage interface (an in-memory one for tests or server rendering, say).
|
|
45
|
+
*
|
|
46
|
+
* Keys are those of {@link ReactiveWebLocalStorage}: `<appName>/<storeName>`.
|
|
46
47
|
*
|
|
47
|
-
*
|
|
48
|
+
* Example usage:
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { ReactiveWebStorage } from '@telperion/reactive-storage/reactive-web-storage';
|
|
51
|
+
*
|
|
52
|
+
* const storage = new ReactiveWebStorage(window.localStorage, 'my-app');
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
48
56
|
*/
|
|
49
57
|
class ReactiveWebStorage extends AbstractReactiveWebStorage {
|
|
50
58
|
storage;
|
|
51
59
|
appName;
|
|
60
|
+
/**
|
|
61
|
+
* @param storage - Web Storage object to save the stores in
|
|
62
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
63
|
+
*/
|
|
52
64
|
constructor(storage, appName = 'app') {
|
|
53
65
|
super();
|
|
54
66
|
this.storage = storage;
|
|
55
67
|
this.appName = appName;
|
|
56
68
|
}
|
|
57
69
|
}
|
|
70
|
+
|
|
71
|
+
exports.AbstractReactiveWebStorage = AbstractReactiveWebStorage;
|
|
58
72
|
exports.ReactiveWebStorage = ReactiveWebStorage;
|
|
73
|
+
//# sourceMappingURL=reactive-web-storage.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive-web-storage.cjs","sources":["../../../libs/reactive-storage/src/reactive-web-storage.ts"],"sourcesContent":[null],"names":["ReactiveStorage","jsonParse","Subject","makeAsyncIterator"],"mappings":";;;;;;;AAMA;;;;;AAKG;AACG,MAAgB,0BAAsD,SAAQA,+BAAkB,CAAA;;IAEpG,OAAO,MAAM,CAAC,MAAc,EAAA;AAC1B,QAAA,OAAOC,mBAAS,CAAC,MAAM,CAAC;IAC1B;AAMA,IAAA,SAAS,GAAG,IAAIC,YAAO,EAAiC;AAE/C,IAAA,OAAOF,+BAAe,CAAC,KAAK,CAAC,OAAO,CAAC,GAAA;AAC9C,QAAA,MAAM,MAAM,GAAG,CAAA,EAAG,IAAI,CAAC,OAAO,GAAG;;AAEjC,QAAA,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC;aAC5F,MAAM,CAAC,CAAC,GAAG,KAAoB,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAE7F,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI;AACxC,YAAA,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;YACxB,0BAA0B,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAE;AAC7D,SAAA,CAAC,CAAC;IACN;;;IAIU,CAACA,+BAAe,CAAC,KAAK,CAAC,OAAO,IAAoD,IAAI,CAAC,SAAiB,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,IAAIG,mCAAiB,CAAC,IAAI,CAAC,SAAS,CAAC;IAEvK,OAAOH,+BAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,SAAY,EAAE,KAAc,EAAA;QACrE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA,EAAG,IAAI,CAAC,OAAO,CAAA,CAAA,EAAI,SAAS,CAAA,CAAE,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAC3E,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC5C;IAEW,OAAOA,+BAAe,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,SAAY,EAAA;AACzD,QAAA,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA,EAAG,IAAI,CAAC,OAAO,CAAA,CAAA,EAAI,SAAS,CAAA,CAAE,CAAC;AACvD,QAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IACtD;AACD;AAED;;;;;;;;;;;;;;AAcG;AACG,MAAO,kBAA8C,SAAQ,0BAA6B,CAAA;AAM1E,IAAA,OAAA;AACA,IAAA,OAAA;AANpB;;;AAGG;IACH,WAAA,CACoB,OAAgB,EAChB,OAAA,GAAU,KAAK,EAAA;AAEjC,QAAA,KAAK,EAAE;QAHW,IAAA,CAAA,OAAO,GAAP,OAAO;QACP,IAAA,CAAA,OAAO,GAAP,OAAO;IAG3B;AACD;;;;;"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ReactiveStorage, ReactiveStorageChangeEvent } from './reactive-storage.cjs';
|
|
2
|
+
import 'rxjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base class of the Web Storage implementations: saves each store as JSON under `<appName>/<storeName>`
|
|
6
|
+
* in `storage`. Extend it to back a reactive storage with another `Storage` object.
|
|
7
|
+
*
|
|
8
|
+
* @template S - Store name type
|
|
9
|
+
*/
|
|
10
|
+
declare abstract class AbstractReactiveWebStorage<S extends string = string> extends ReactiveStorage<S> {
|
|
11
|
+
#private;
|
|
12
|
+
protected abstract readonly appName: string;
|
|
13
|
+
/** The Web Storage object the stores are saved in */
|
|
14
|
+
abstract readonly storage: Storage;
|
|
15
|
+
protected [ReactiveStorage.Props.GET_ALL](): Promise<{
|
|
16
|
+
[k: string]: any;
|
|
17
|
+
}>;
|
|
18
|
+
protected [ReactiveStorage.Props.CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;
|
|
19
|
+
protected [ReactiveStorage.Props.SET](storeName: S, value: unknown): Promise<void>;
|
|
20
|
+
protected [ReactiveStorage.Props.REMOVE](storeName: S): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Reactive storage on any `Storage` object you pass in: `localStorage`, `sessionStorage`, or your own
|
|
24
|
+
* implementation of the Web Storage interface (an in-memory one for tests or server rendering, say).
|
|
25
|
+
*
|
|
26
|
+
* Keys are those of {@link ReactiveWebLocalStorage}: `<appName>/<storeName>`.
|
|
27
|
+
*
|
|
28
|
+
* Example usage:
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { ReactiveWebStorage } from '@telperion/reactive-storage/reactive-web-storage';
|
|
31
|
+
*
|
|
32
|
+
* const storage = new ReactiveWebStorage(window.localStorage, 'my-app');
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
36
|
+
*/
|
|
37
|
+
declare class ReactiveWebStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
38
|
+
readonly storage: Storage;
|
|
39
|
+
readonly appName: string;
|
|
40
|
+
/**
|
|
41
|
+
* @param storage - Web Storage object to save the stores in
|
|
42
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
43
|
+
*/
|
|
44
|
+
constructor(storage: Storage, appName?: string);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { AbstractReactiveWebStorage, ReactiveWebStorage };
|
|
@@ -1,29 +1,47 @@
|
|
|
1
|
-
import { ReactiveStorage } from
|
|
1
|
+
import { ReactiveStorage, ReactiveStorageChangeEvent } from './reactive-storage.js';
|
|
2
|
+
import 'rxjs';
|
|
3
|
+
|
|
2
4
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
+
* Base class of the Web Storage implementations: saves each store as JSON under `<appName>/<storeName>`
|
|
6
|
+
* in `storage`. Extend it to back a reactive storage with another `Storage` object.
|
|
5
7
|
*
|
|
6
|
-
* @template S -
|
|
8
|
+
* @template S - Store name type
|
|
7
9
|
*/
|
|
8
|
-
|
|
10
|
+
declare abstract class AbstractReactiveWebStorage<S extends string = string> extends ReactiveStorage<S> {
|
|
9
11
|
#private;
|
|
10
12
|
protected abstract readonly appName: string;
|
|
13
|
+
/** The Web Storage object the stores are saved in */
|
|
11
14
|
abstract readonly storage: Storage;
|
|
12
15
|
protected [ReactiveStorage.Props.GET_ALL](): Promise<{
|
|
13
16
|
[k: string]: any;
|
|
14
17
|
}>;
|
|
15
|
-
protected [ReactiveStorage.Props.CHANGES]:
|
|
18
|
+
protected [ReactiveStorage.Props.CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;
|
|
16
19
|
protected [ReactiveStorage.Props.SET](storeName: S, value: unknown): Promise<void>;
|
|
17
20
|
protected [ReactiveStorage.Props.REMOVE](storeName: S): Promise<void>;
|
|
18
21
|
}
|
|
19
22
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
23
|
+
* Reactive storage on any `Storage` object you pass in: `localStorage`, `sessionStorage`, or your own
|
|
24
|
+
* implementation of the Web Storage interface (an in-memory one for tests or server rendering, say).
|
|
22
25
|
*
|
|
23
|
-
*
|
|
26
|
+
* Keys are those of {@link ReactiveWebLocalStorage}: `<appName>/<storeName>`.
|
|
27
|
+
*
|
|
28
|
+
* Example usage:
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { ReactiveWebStorage } from '@telperion/reactive-storage/reactive-web-storage';
|
|
31
|
+
*
|
|
32
|
+
* const storage = new ReactiveWebStorage(window.localStorage, 'my-app');
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
24
36
|
*/
|
|
25
|
-
|
|
37
|
+
declare class ReactiveWebStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
26
38
|
readonly storage: Storage;
|
|
27
39
|
readonly appName: string;
|
|
40
|
+
/**
|
|
41
|
+
* @param storage - Web Storage object to save the stores in
|
|
42
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
43
|
+
*/
|
|
28
44
|
constructor(storage: Storage, appName?: string);
|
|
29
45
|
}
|
|
46
|
+
|
|
47
|
+
export { AbstractReactiveWebStorage, ReactiveWebStorage };
|
package/reactive-web-storage.js
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
import { Subject } from 'rxjs';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
2
|
+
import { makeAsyncIterator } from '@telperion/rx-utils/to-async-iteratable';
|
|
3
|
+
import { ReactiveStorage } from './reactive-storage.js';
|
|
4
|
+
import { jsonParse } from './json-parse.js';
|
|
5
|
+
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Base class of the Web Storage implementations: saves each store as JSON under `<appName>/<storeName>`
|
|
8
|
+
* in `storage`. Extend it to back a reactive storage with another `Storage` object.
|
|
9
9
|
*
|
|
10
|
-
* @template S -
|
|
10
|
+
* @template S - Store name type
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
class AbstractReactiveWebStorage extends ReactiveStorage {
|
|
13
13
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
14
|
static #parse(string) {
|
|
15
15
|
return jsonParse(string);
|
|
16
16
|
}
|
|
17
|
-
get #keyNameRegex() {
|
|
18
|
-
return RegexParser(`^${this.appName}\\/\\w+$`);
|
|
19
|
-
}
|
|
20
17
|
#changes$ = new Subject();
|
|
21
18
|
async [ReactiveStorage.Props.GET_ALL]() {
|
|
22
|
-
const
|
|
19
|
+
const prefix = `${this.appName}/`;
|
|
20
|
+
// Through the Storage interface, so custom Storage implementations are read too
|
|
21
|
+
const keys = Array.from({ length: this.storage.length }, (_, index) => this.storage.key(index))
|
|
22
|
+
.filter((key) => !!key && key.startsWith(prefix) && key.length > prefix.length);
|
|
23
23
|
return Object.fromEntries(keys.map(key => [
|
|
24
|
-
key.
|
|
24
|
+
key.slice(prefix.length),
|
|
25
25
|
AbstractReactiveWebStorage.#parse(this.storage.getItem(key))
|
|
26
26
|
]));
|
|
27
27
|
}
|
|
28
|
+
// Prefer an Observable's own async iterator (rxjs 8, or `@telperion/rx-utils/static/async-iterator`)
|
|
28
29
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
-
[ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator] ??
|
|
30
|
+
[ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator]?.() ?? makeAsyncIterator(this.#changes$);
|
|
30
31
|
async [ReactiveStorage.Props.SET](storeName, value) {
|
|
31
32
|
this.storage.setItem(`${this.appName}/${storeName}`, JSON.stringify(value));
|
|
32
33
|
this.#changes$.next({ storeName, value });
|
|
@@ -37,14 +38,27 @@ export class AbstractReactiveWebStorage extends ReactiveStorage {
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
41
|
+
* Reactive storage on any `Storage` object you pass in: `localStorage`, `sessionStorage`, or your own
|
|
42
|
+
* implementation of the Web Storage interface (an in-memory one for tests or server rendering, say).
|
|
43
|
+
*
|
|
44
|
+
* Keys are those of {@link ReactiveWebLocalStorage}: `<appName>/<storeName>`.
|
|
45
|
+
*
|
|
46
|
+
* Example usage:
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { ReactiveWebStorage } from '@telperion/reactive-storage/reactive-web-storage';
|
|
49
|
+
*
|
|
50
|
+
* const storage = new ReactiveWebStorage(window.localStorage, 'my-app');
|
|
51
|
+
* ```
|
|
42
52
|
*
|
|
43
|
-
* @template S -
|
|
53
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
44
54
|
*/
|
|
45
|
-
|
|
55
|
+
class ReactiveWebStorage extends AbstractReactiveWebStorage {
|
|
46
56
|
storage;
|
|
47
57
|
appName;
|
|
58
|
+
/**
|
|
59
|
+
* @param storage - Web Storage object to save the stores in
|
|
60
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
61
|
+
*/
|
|
48
62
|
constructor(storage, appName = 'app') {
|
|
49
63
|
super();
|
|
50
64
|
this.storage = storage;
|
|
@@ -52,4 +66,5 @@ export class ReactiveWebStorage extends AbstractReactiveWebStorage {
|
|
|
52
66
|
}
|
|
53
67
|
}
|
|
54
68
|
|
|
69
|
+
export { AbstractReactiveWebStorage, ReactiveWebStorage };
|
|
55
70
|
//# sourceMappingURL=reactive-web-storage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"file":"reactive-web-storage.js","sources":["../../../libs/reactive-storage/src/reactive-web-storage.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAMA;;;;;AAKG;AACG,MAAgB,0BAAsD,SAAQ,eAAkB,CAAA;;IAEpG,OAAO,MAAM,CAAC,MAAc,EAAA;AAC1B,QAAA,OAAO,SAAS,CAAC,MAAM,CAAC;IAC1B;AAMA,IAAA,SAAS,GAAG,IAAI,OAAO,EAAiC;AAE/C,IAAA,OAAO,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,GAAA;AAC9C,QAAA,MAAM,MAAM,GAAG,CAAA,EAAG,IAAI,CAAC,OAAO,GAAG;;AAEjC,QAAA,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC;aAC5F,MAAM,CAAC,CAAC,GAAG,KAAoB,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAE7F,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI;AACxC,YAAA,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;YACxB,0BAA0B,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAE;AAC7D,SAAA,CAAC,CAAC;IACN;;;IAIU,CAAC,eAAe,CAAC,KAAK,CAAC,OAAO,IAAoD,IAAI,CAAC,SAAiB,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,IAAI,iBAAiB,CAAC,IAAI,CAAC,SAAS,CAAC;IAEvK,OAAO,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,SAAY,EAAE,KAAc,EAAA;QACrE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA,EAAG,IAAI,CAAC,OAAO,CAAA,CAAA,EAAI,SAAS,CAAA,CAAE,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QAC3E,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC5C;IAEW,OAAO,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,SAAY,EAAA;AACzD,QAAA,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA,EAAG,IAAI,CAAC,OAAO,CAAA,CAAA,EAAI,SAAS,CAAA,CAAE,CAAC;AACvD,QAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IACtD;AACD;AAED;;;;;;;;;;;;;;AAcG;AACG,MAAO,kBAA8C,SAAQ,0BAA6B,CAAA;AAM1E,IAAA,OAAA;AACA,IAAA,OAAA;AANpB;;;AAGG;IACH,WAAA,CACoB,OAAgB,EAChB,OAAA,GAAU,KAAK,EAAA;AAEjC,QAAA,KAAK,EAAE;QAHW,IAAA,CAAA,OAAO,GAAP,OAAO;QACP,IAAA,CAAA,OAAO,GAAP,OAAO;IAG3B;AACD;;;;"}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Copied from https://github.com/ReactiveX/rxjs/blob/master/packages/observable/src/observable.ts
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.toAsyncIterator = toAsyncIterator;
|
|
5
|
-
function toAsyncIterator(observable) {
|
|
6
|
-
let subscription;
|
|
7
|
-
let hasError = false;
|
|
8
|
-
let error;
|
|
9
|
-
let completed = false;
|
|
10
|
-
const values = [];
|
|
11
|
-
const deferreds = [];
|
|
12
|
-
const handleError = (err) => {
|
|
13
|
-
hasError = true;
|
|
14
|
-
error = err;
|
|
15
|
-
while (deferreds.length) {
|
|
16
|
-
const [_, reject] = deferreds.shift();
|
|
17
|
-
reject(err);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
const handleComplete = () => {
|
|
21
|
-
completed = true;
|
|
22
|
-
while (deferreds.length) {
|
|
23
|
-
const [resolve] = deferreds.shift();
|
|
24
|
-
resolve({ value: undefined, done: true });
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
return {
|
|
28
|
-
next: () => {
|
|
29
|
-
if (!subscription) {
|
|
30
|
-
// We only want to start the subscription when the user starts iterating.
|
|
31
|
-
subscription = observable.subscribe({
|
|
32
|
-
next: (value) => {
|
|
33
|
-
if (deferreds.length) {
|
|
34
|
-
const [resolve] = deferreds.shift();
|
|
35
|
-
resolve({ value, done: false });
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
values.push(value);
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
error: handleError,
|
|
42
|
-
complete: handleComplete,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
// If we already have some values in our buffer, we'll return the next one.
|
|
46
|
-
if (values.length) {
|
|
47
|
-
return Promise.resolve({ value: values.shift(), done: false });
|
|
48
|
-
}
|
|
49
|
-
// This was already completed, so we're just going to return a done result.
|
|
50
|
-
if (completed) {
|
|
51
|
-
return Promise.resolve({ value: undefined, done: true });
|
|
52
|
-
}
|
|
53
|
-
// There was an error, so we're going to return an error result.
|
|
54
|
-
if (hasError) {
|
|
55
|
-
return Promise.reject(error);
|
|
56
|
-
}
|
|
57
|
-
// Otherwise, we need to make them wait for a value.
|
|
58
|
-
return new Promise((resolve, reject) => {
|
|
59
|
-
deferreds.push([resolve, reject]);
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
throw: (err) => {
|
|
63
|
-
subscription?.unsubscribe();
|
|
64
|
-
// NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do
|
|
65
|
-
// anything with pending promises returned from `next()` when `throw()` is called.
|
|
66
|
-
// However, for consumption of observables, I don't want RxJS taking the heat for that
|
|
67
|
-
// quirk/leak of the type. So we're going to reject all pending promises we've nexted out here.
|
|
68
|
-
handleError(err);
|
|
69
|
-
return Promise.reject(err);
|
|
70
|
-
},
|
|
71
|
-
return: () => {
|
|
72
|
-
subscription?.unsubscribe();
|
|
73
|
-
// NOTE: I did some research on this, and as of Feb 2023, Chrome doesn't seem to do
|
|
74
|
-
// anything with pending promises returned from `next()` when `throw()` is called.
|
|
75
|
-
// However, for consumption of observables, I don't want RxJS taking the heat for that
|
|
76
|
-
// quirk/leak of the type. So we're going to resolve all pending promises we've nexted out here.
|
|
77
|
-
handleComplete();
|
|
78
|
-
return Promise.resolve({ value: undefined, done: true });
|
|
79
|
-
},
|
|
80
|
-
[Symbol.asyncIterator]() {
|
|
81
|
-
return this;
|
|
82
|
-
},
|
|
83
|
-
async [Symbol.asyncDispose]() {
|
|
84
|
-
subscription?.unsubscribe();
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
}
|