@telperion/reactive-storage 1.1.0 → 1.3.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/.build-manifest.json +96 -0
- package/README.md +172 -1
- package/fesm/reactive-storage.mjs +620 -0
- package/fesm/reactive-storage.mjs.map +1 -0
- package/index.cjs +17 -7
- package/index.cjs.map +1 -0
- package/index.d.cts +6 -0
- package/index.d.ts +6 -4
- package/index.js +5 -5
- package/index.js.map +1 -1
- package/json-parse.cjs +18 -0
- package/json-parse.cjs.map +1 -0
- package/json-parse.d.cts +8 -0
- package/json-parse.d.ts +8 -0
- package/json-parse.js +16 -0
- package/json-parse.js.map +1 -0
- package/package.json +91 -25
- package/reactive-cookie-storage.cjs +217 -0
- package/reactive-cookie-storage.cjs.map +1 -0
- package/reactive-cookie-storage.d.cts +77 -0
- package/reactive-cookie-storage.d.ts +77 -0
- package/reactive-cookie-storage.js +215 -0
- package/reactive-cookie-storage.js.map +1 -0
- package/reactive-storage.cjs +127 -74
- package/reactive-storage.cjs.map +1 -0
- package/reactive-storage.d.cts +144 -0
- package/reactive-storage.d.ts +87 -27
- package/reactive-storage.js +84 -31
- package/reactive-storage.js.map +1 -1
- package/reactive-web-local-storage.cjs +31 -8
- package/reactive-web-local-storage.cjs.map +1 -0
- package/reactive-web-local-storage.d.cts +38 -0
- package/reactive-web-local-storage.d.ts +31 -5
- package/reactive-web-local-storage.js +28 -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 +41 -29
- package/reactive-web-storage.cjs.map +1 -0
- package/reactive-web-storage.d.cts +47 -0
- package/reactive-web-storage.d.ts +28 -11
- package/reactive-web-storage.js +32 -20
- package/reactive-web-storage.js.map +1 -1
- package/regex-parser.cjs +4 -3
- package/regex-parser.cjs.map +1 -0
- package/regex-parser.d.cts +13 -0
- package/regex-parser.d.ts +3 -1
- package/regex-parser.js +2 -1
- package/regex-parser.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
|
@@ -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
|
+
* and store name rules; 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
|
+
* and store name rules; 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,62 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 regexParser = require('./regex-parser.cjs');
|
|
7
|
+
var jsonParse = require('./json-parse.cjs');
|
|
8
|
+
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
10
|
+
* Base class of the Web Storage implementations: saves each store as JSON under `<appName>/<storeName>`
|
|
11
|
+
* in `storage`. Extend it to back a reactive storage with another `Storage` object.
|
|
11
12
|
*
|
|
12
|
-
* @template S -
|
|
13
|
+
* @template S - Store name type
|
|
13
14
|
*/
|
|
14
|
-
class AbstractReactiveWebStorage extends
|
|
15
|
+
class AbstractReactiveWebStorage extends reactiveStorage.ReactiveStorage {
|
|
15
16
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
-
static
|
|
17
|
-
|
|
18
|
-
return JSON.parse(string);
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
17
|
+
static #parse(string) {
|
|
18
|
+
return jsonParse.jsonParse(string);
|
|
23
19
|
}
|
|
24
20
|
get #keyNameRegex() {
|
|
25
|
-
return
|
|
21
|
+
return regexParser.RegexParser(`^${this.appName}\\/\\w+$`);
|
|
26
22
|
}
|
|
27
|
-
#changes$ = new
|
|
28
|
-
async [
|
|
23
|
+
#changes$ = new rxjs.Subject();
|
|
24
|
+
async [reactiveStorage.ReactiveStorage.Props.GET_ALL]() {
|
|
29
25
|
const keys = Object.keys(this.storage).filter(key => this.#keyNameRegex.test(key));
|
|
30
26
|
return Object.fromEntries(keys.map(key => [
|
|
31
27
|
key.split('/')[1],
|
|
32
|
-
AbstractReactiveWebStorage
|
|
28
|
+
AbstractReactiveWebStorage.#parse(this.storage.getItem(key))
|
|
33
29
|
]));
|
|
34
30
|
}
|
|
31
|
+
// Prefer an Observable's own async iterator (rxjs 8, or `@telperion/rx-utils/static/async-iterator`)
|
|
35
32
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
[
|
|
37
|
-
async [
|
|
33
|
+
[reactiveStorage.ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator]?.() ?? toAsyncIteratable.makeAsyncIterator(this.#changes$);
|
|
34
|
+
async [reactiveStorage.ReactiveStorage.Props.SET](storeName, value) {
|
|
38
35
|
this.storage.setItem(`${this.appName}/${storeName}`, JSON.stringify(value));
|
|
39
36
|
this.#changes$.next({ storeName, value });
|
|
40
37
|
}
|
|
41
|
-
async [
|
|
38
|
+
async [reactiveStorage.ReactiveStorage.Props.REMOVE](storeName) {
|
|
42
39
|
this.storage.removeItem(`${this.appName}/${storeName}`);
|
|
43
40
|
this.#changes$.next({ storeName, value: undefined });
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
|
-
exports.AbstractReactiveWebStorage = AbstractReactiveWebStorage;
|
|
47
43
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
44
|
+
* Reactive storage on any `Storage` object you pass in: `localStorage`, `sessionStorage`, or your own
|
|
45
|
+
* implementation of the Web Storage interface (an in-memory one for tests or server rendering, say).
|
|
46
|
+
*
|
|
47
|
+
* Keys and store name rules are those of {@link ReactiveWebLocalStorage}.
|
|
48
|
+
*
|
|
49
|
+
* Example usage:
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { ReactiveWebStorage } from '@telperion/reactive-storage/reactive-web-storage';
|
|
50
52
|
*
|
|
51
|
-
*
|
|
53
|
+
* const storage = new ReactiveWebStorage(window.localStorage, 'my-app');
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
52
57
|
*/
|
|
53
58
|
class ReactiveWebStorage extends AbstractReactiveWebStorage {
|
|
54
59
|
storage;
|
|
55
60
|
appName;
|
|
61
|
+
/**
|
|
62
|
+
* @param storage - Web Storage object to save the stores in
|
|
63
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
64
|
+
*/
|
|
56
65
|
constructor(storage, appName = 'app') {
|
|
57
66
|
super();
|
|
58
67
|
this.storage = storage;
|
|
59
68
|
this.appName = appName;
|
|
60
69
|
}
|
|
61
70
|
}
|
|
71
|
+
|
|
72
|
+
exports.AbstractReactiveWebStorage = AbstractReactiveWebStorage;
|
|
62
73
|
exports.ReactiveWebStorage = ReactiveWebStorage;
|
|
74
|
+
//# 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","RegexParser","Subject","makeAsyncIterator"],"mappings":";;;;;;;;AAOA;;;;;AAKG;AACG,MAAgB,0BAAsD,SAAQA,+BAAkB,CAAA;;IAEpG,OAAO,MAAM,CAAC,MAAc,EAAA;AAC1B,QAAA,OAAOC,mBAAS,CAAC,MAAM,CAAC;IAC1B;AAMA,IAAA,IAAI,aAAa,GAAA;QACf,OAAOC,uBAAW,CAAC,CAAA,CAAA,EAAI,IAAI,CAAC,OAAO,CAAA,QAAA,CAAU,CAAC;IAChD;AAEA,IAAA,SAAS,GAAG,IAAIC,YAAO,EAAiC;AAE/C,IAAA,OAAOH,+BAAe,CAAC,KAAK,CAAC,OAAO,CAAC,GAAA;QAC9C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAEhF,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI;AACxC,YAAA,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACjB,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,IAAII,mCAAiB,CAAC,IAAI,CAAC,SAAS,CAAC;IAEvK,OAAOJ,+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 and store name rules are those of {@link ReactiveWebLocalStorage}.
|
|
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,30 +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
|
-
static JsonParse(string: string): any;
|
|
11
12
|
protected abstract readonly appName: string;
|
|
13
|
+
/** The Web Storage object the stores are saved in */
|
|
12
14
|
abstract readonly storage: Storage;
|
|
13
15
|
protected [ReactiveStorage.Props.GET_ALL](): Promise<{
|
|
14
16
|
[k: string]: any;
|
|
15
17
|
}>;
|
|
16
|
-
protected [ReactiveStorage.Props.CHANGES]:
|
|
18
|
+
protected [ReactiveStorage.Props.CHANGES]: AsyncGenerator<ReactiveStorageChangeEvent<S>>;
|
|
17
19
|
protected [ReactiveStorage.Props.SET](storeName: S, value: unknown): Promise<void>;
|
|
18
20
|
protected [ReactiveStorage.Props.REMOVE](storeName: S): Promise<void>;
|
|
19
21
|
}
|
|
20
22
|
/**
|
|
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).
|
|
23
25
|
*
|
|
24
|
-
*
|
|
26
|
+
* Keys and store name rules are those of {@link ReactiveWebLocalStorage}.
|
|
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
|
|
25
36
|
*/
|
|
26
|
-
|
|
37
|
+
declare class ReactiveWebStorage<S extends string = string> extends AbstractReactiveWebStorage<S> {
|
|
27
38
|
readonly storage: Storage;
|
|
28
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
|
+
*/
|
|
29
44
|
constructor(storage: Storage, appName?: string);
|
|
30
45
|
}
|
|
46
|
+
|
|
47
|
+
export { AbstractReactiveWebStorage, ReactiveWebStorage };
|
package/reactive-web-storage.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
1
|
import { Subject } from 'rxjs';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { makeAsyncIterator } from '@telperion/rx-utils/to-async-iteratable';
|
|
3
|
+
import { ReactiveStorage } from './reactive-storage.js';
|
|
4
|
+
import { RegexParser } from './regex-parser.js';
|
|
5
|
+
import { jsonParse } from './json-parse.js';
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
+
* Base class of the Web Storage implementations: saves each store as JSON under `<appName>/<storeName>`
|
|
9
|
+
* in `storage`. Extend it to back a reactive storage with another `Storage` object.
|
|
8
10
|
*
|
|
9
|
-
* @template S -
|
|
11
|
+
* @template S - Store name type
|
|
10
12
|
*/
|
|
11
|
-
|
|
13
|
+
class AbstractReactiveWebStorage extends ReactiveStorage {
|
|
12
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
-
static
|
|
14
|
-
|
|
15
|
-
return JSON.parse(string);
|
|
16
|
-
}
|
|
17
|
-
catch {
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
15
|
+
static #parse(string) {
|
|
16
|
+
return jsonParse(string);
|
|
20
17
|
}
|
|
21
18
|
get #keyNameRegex() {
|
|
22
19
|
return RegexParser(`^${this.appName}\\/\\w+$`);
|
|
@@ -26,11 +23,12 @@ export class AbstractReactiveWebStorage extends ReactiveStorage {
|
|
|
26
23
|
const keys = Object.keys(this.storage).filter(key => this.#keyNameRegex.test(key));
|
|
27
24
|
return Object.fromEntries(keys.map(key => [
|
|
28
25
|
key.split('/')[1],
|
|
29
|
-
AbstractReactiveWebStorage
|
|
26
|
+
AbstractReactiveWebStorage.#parse(this.storage.getItem(key))
|
|
30
27
|
]));
|
|
31
28
|
}
|
|
29
|
+
// Prefer an Observable's own async iterator (rxjs 8, or `@telperion/rx-utils/static/async-iterator`)
|
|
32
30
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
-
[ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator] ??
|
|
31
|
+
[ReactiveStorage.Props.CHANGES] = this.#changes$[Symbol.asyncIterator]?.() ?? makeAsyncIterator(this.#changes$);
|
|
34
32
|
async [ReactiveStorage.Props.SET](storeName, value) {
|
|
35
33
|
this.storage.setItem(`${this.appName}/${storeName}`, JSON.stringify(value));
|
|
36
34
|
this.#changes$.next({ storeName, value });
|
|
@@ -41,14 +39,27 @@ export class AbstractReactiveWebStorage extends ReactiveStorage {
|
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
42
|
+
* Reactive storage on any `Storage` object you pass in: `localStorage`, `sessionStorage`, or your own
|
|
43
|
+
* implementation of the Web Storage interface (an in-memory one for tests or server rendering, say).
|
|
44
|
+
*
|
|
45
|
+
* Keys and store name rules are those of {@link ReactiveWebLocalStorage}.
|
|
46
|
+
*
|
|
47
|
+
* Example usage:
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { ReactiveWebStorage } from '@telperion/reactive-storage/reactive-web-storage';
|
|
50
|
+
*
|
|
51
|
+
* const storage = new ReactiveWebStorage(window.localStorage, 'my-app');
|
|
52
|
+
* ```
|
|
46
53
|
*
|
|
47
|
-
* @template S -
|
|
54
|
+
* @template S - Store name type: a union of string literals restricts the store names you can use
|
|
48
55
|
*/
|
|
49
|
-
|
|
56
|
+
class ReactiveWebStorage extends AbstractReactiveWebStorage {
|
|
50
57
|
storage;
|
|
51
58
|
appName;
|
|
59
|
+
/**
|
|
60
|
+
* @param storage - Web Storage object to save the stores in
|
|
61
|
+
* @param appName - Namespace of this app's keys: each store is saved under `<appName>/<storeName>`. Defaults to `'app'`
|
|
62
|
+
*/
|
|
52
63
|
constructor(storage, appName = 'app') {
|
|
53
64
|
super();
|
|
54
65
|
this.storage = storage;
|
|
@@ -56,4 +67,5 @@ export class ReactiveWebStorage extends AbstractReactiveWebStorage {
|
|
|
56
67
|
}
|
|
57
68
|
}
|
|
58
69
|
|
|
70
|
+
export { AbstractReactiveWebStorage, ReactiveWebStorage };
|
|
59
71
|
//# 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":";;;;;;AAOA;;;;;AAKG;AACG,MAAgB,0BAAsD,SAAQ,eAAkB,CAAA;;IAEpG,OAAO,MAAM,CAAC,MAAc,EAAA;AAC1B,QAAA,OAAO,SAAS,CAAC,MAAM,CAAC;IAC1B;AAMA,IAAA,IAAI,aAAa,GAAA;QACf,OAAO,WAAW,CAAC,CAAA,CAAA,EAAI,IAAI,CAAC,OAAO,CAAA,QAAA,CAAU,CAAC;IAChD;AAEA,IAAA,SAAS,GAAG,IAAI,OAAO,EAAiC;AAE/C,IAAA,OAAO,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,GAAA;QAC9C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAEhF,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI;AACxC,YAAA,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACjB,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;;;;"}
|
package/regex-parser.cjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
2
3
|
// Copied from `https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js`
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.RegexParser = void 0;
|
|
5
4
|
/**
|
|
6
5
|
* RegexParser
|
|
7
6
|
* Parses a string input.
|
|
@@ -26,4 +25,6 @@ const RegexParser = function (input) {
|
|
|
26
25
|
// Create the regular expression
|
|
27
26
|
return new RegExp(m[2], m[3]);
|
|
28
27
|
};
|
|
28
|
+
|
|
29
29
|
exports.RegexParser = RegexParser;
|
|
30
|
+
//# sourceMappingURL=regex-parser.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regex-parser.cjs","sources":["../../../libs/reactive-storage/src/regex-parser.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;AAEA;;;;;;;;;AASG;AACI,MAAM,WAAW,GAAG,UAAS,KAAa,EAAA;;AAG/C,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC3B,QAAA,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC;IAC5D;;IAGA,MAAM,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,sBAAsB,CAAE;;AAG9C,IAAA,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE;AACtD,QAAA,OAAO,MAAM,CAAC,KAAK,CAAC;IACxB;;AAGA,IAAA,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RegexParser
|
|
3
|
+
* Parses a string input.
|
|
4
|
+
*
|
|
5
|
+
* @name RegexParser
|
|
6
|
+
* @function
|
|
7
|
+
* @param {String} input The string input that should be parsed as regular
|
|
8
|
+
* expression.
|
|
9
|
+
* @return {RegExp} The parsed regular expression.
|
|
10
|
+
*/
|
|
11
|
+
declare const RegexParser: (input: string) => RegExp;
|
|
12
|
+
|
|
13
|
+
export { RegexParser };
|
package/regex-parser.d.ts
CHANGED
package/regex-parser.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* expression.
|
|
10
10
|
* @return {RegExp} The parsed regular expression.
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
const RegexParser = function (input) {
|
|
13
13
|
// Validate input
|
|
14
14
|
if (typeof input !== "string") {
|
|
15
15
|
throw new Error("Invalid input. Input must be a string");
|
|
@@ -24,4 +24,5 @@ export const RegexParser = function (input) {
|
|
|
24
24
|
return new RegExp(m[2], m[3]);
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
export { RegexParser };
|
|
27
28
|
//# sourceMappingURL=regex-parser.js.map
|
package/regex-parser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["
|
|
1
|
+
{"version":3,"file":"regex-parser.js","sources":["../../../libs/reactive-storage/src/regex-parser.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;AAEA;;;;;;;;;AASG;AACI,MAAM,WAAW,GAAG,UAAS,KAAa,EAAA;;AAG/C,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC3B,QAAA,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC;IAC5D;;IAGA,MAAM,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,sBAAsB,CAAE;;AAG9C,IAAA,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE;AACtD,QAAA,OAAO,MAAM,CAAC,KAAK,CAAC;IACxB;;AAGA,IAAA,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B;;;;"}
|
|
@@ -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
|
-
}
|