@yaasl/core 0.14.0-alpha.0 → 0.14.0-alpha.2
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/dist/@types/effects/expiration.d.ts +1 -3
- package/dist/@types/effects/indexed-db.d.ts +6 -1
- package/dist/@types/utils/idb-store.d.ts +2 -1
- package/dist/cjs/effects/expiration.js +1 -3
- package/dist/cjs/effects/indexed-db.js +14 -5
- package/dist/cjs/utils/idb-store.js +5 -0
- package/dist/esm/effects/expiration.js +1 -3
- package/dist/esm/effects/indexed-db.js +13 -4
- package/dist/esm/utils/idb-store.js +5 -0
- package/package.json +1 -1
|
@@ -5,11 +5,9 @@ export interface ExpirationOptions {
|
|
|
5
5
|
expiresIn?: number | (() => number);
|
|
6
6
|
}
|
|
7
7
|
/** Effect to make an atom value expirable and reset to its defaulValue.
|
|
8
|
-
*
|
|
9
|
-
* __Note:__ When using `expiresAt`, a function returning the date should be prefered since using a static date might end in an infinite loop.
|
|
10
8
|
*
|
|
11
9
|
* @param {ExpirationOptions | undefined} options
|
|
12
|
-
* @param options.expiresAt Date at which the value expires
|
|
10
|
+
* @param options.expiresAt Date at which the value expires. Using a function returning the date should be prefered here, since using a static date might end in an infinite loop.
|
|
13
11
|
* @param options.expiresIn Milliseconds in which the value expires. Will be ignored if expiresAt is set.
|
|
14
12
|
*
|
|
15
13
|
* @returns The effect to be used on atoms.
|
|
@@ -14,4 +14,9 @@ export interface IndexedDbOptions {
|
|
|
14
14
|
*
|
|
15
15
|
* @returns The effect to be used on atoms.
|
|
16
16
|
**/
|
|
17
|
-
export declare const indexedDb: (...[optionsArg]: [] | [undefined] | [IndexedDbOptions]) => import("./create-effect").EffectAtomCallback<IndexedDbOptions | undefined, unknown
|
|
17
|
+
export declare const indexedDb: ((...[optionsArg]: [] | [undefined] | [IndexedDbOptions]) => import("./create-effect").EffectAtomCallback<IndexedDbOptions | undefined, unknown>) & {
|
|
18
|
+
get: (key: string) => Promise<unknown>;
|
|
19
|
+
getAllKeys: () => Promise<string[]>;
|
|
20
|
+
set: (key: string, value: unknown) => Promise<IDBValidKey | undefined>;
|
|
21
|
+
delete: (key: string) => Promise<undefined>;
|
|
22
|
+
};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export declare class IdbStore<T> {
|
|
1
|
+
export declare class IdbStore<T = unknown> {
|
|
2
2
|
private name;
|
|
3
3
|
private database?;
|
|
4
4
|
constructor(name: string);
|
|
5
5
|
private getStore;
|
|
6
|
+
getAllKeys(): Promise<string[]>;
|
|
6
7
|
get(key: string): Promise<T | undefined>;
|
|
7
8
|
set(key: string, value: T): Promise<IDBValidKey | undefined>;
|
|
8
9
|
delete(key: string): Promise<undefined>;
|
|
@@ -23,11 +23,9 @@ const syncOverBrowserTabs = (observingKey, onChange) => {
|
|
|
23
23
|
});
|
|
24
24
|
};
|
|
25
25
|
/** Effect to make an atom value expirable and reset to its defaulValue.
|
|
26
|
-
*
|
|
27
|
-
* __Note:__ When using `expiresAt`, a function returning the date should be prefered since using a static date might end in an infinite loop.
|
|
28
26
|
*
|
|
29
27
|
* @param {ExpirationOptions | undefined} options
|
|
30
|
-
* @param options.expiresAt Date at which the value expires
|
|
28
|
+
* @param options.expiresAt Date at which the value expires. Using a function returning the date should be prefered here, since using a static date might end in an infinite loop.
|
|
31
29
|
* @param options.expiresIn Milliseconds in which the value expires. Will be ignored if expiresAt is set.
|
|
32
30
|
*
|
|
33
31
|
* @returns The effect to be used on atoms.
|
|
@@ -14,6 +14,13 @@ const create_effect_1 = require("./create-effect");
|
|
|
14
14
|
const base_1 = require("../base");
|
|
15
15
|
const idb_store_1 = require("../utils/idb-store");
|
|
16
16
|
let atomDb = null;
|
|
17
|
+
const getAtomDb = () => {
|
|
18
|
+
var _a;
|
|
19
|
+
if (!atomDb) {
|
|
20
|
+
atomDb = new idb_store_1.IdbStore((_a = base_1.CONFIG.name) !== null && _a !== void 0 ? _a : "yaasl");
|
|
21
|
+
}
|
|
22
|
+
return atomDb;
|
|
23
|
+
};
|
|
17
24
|
/** Middleware to save and load atom values to an indexedDb.
|
|
18
25
|
*
|
|
19
26
|
* Will use one database and store for all atoms with your `CONFIG.name`
|
|
@@ -26,16 +33,13 @@ let atomDb = null;
|
|
|
26
33
|
*
|
|
27
34
|
* @returns The effect to be used on atoms.
|
|
28
35
|
**/
|
|
29
|
-
exports.indexedDb = (0, create_effect_1.createEffect)(({ atom, options }) => {
|
|
36
|
+
exports.indexedDb = Object.assign((0, create_effect_1.createEffect)(({ atom, options }) => {
|
|
30
37
|
var _a;
|
|
31
38
|
const key = (_a = options === null || options === void 0 ? void 0 : options.key) !== null && _a !== void 0 ? _a : atom.name;
|
|
32
39
|
return {
|
|
33
40
|
sort: "pre",
|
|
34
41
|
init: (_a) => __awaiter(void 0, [_a], void 0, function* ({ atom, set }) {
|
|
35
|
-
|
|
36
|
-
if (!atomDb) {
|
|
37
|
-
atomDb = new idb_store_1.IdbStore((_b = base_1.CONFIG.name) !== null && _b !== void 0 ? _b : "yaasl");
|
|
38
|
-
}
|
|
42
|
+
const atomDb = getAtomDb();
|
|
39
43
|
const existing = yield atomDb.get(key);
|
|
40
44
|
if (existing != null) {
|
|
41
45
|
set(existing);
|
|
@@ -54,4 +58,9 @@ exports.indexedDb = (0, create_effect_1.createEffect)(({ atom, options }) => {
|
|
|
54
58
|
}
|
|
55
59
|
},
|
|
56
60
|
};
|
|
61
|
+
}), {
|
|
62
|
+
get: (key) => getAtomDb().get(key),
|
|
63
|
+
getAllKeys: () => getAtomDb().getAllKeys(),
|
|
64
|
+
set: (key, value) => getAtomDb().set(key, value),
|
|
65
|
+
delete: (key) => getAtomDb().delete(key),
|
|
57
66
|
});
|
|
@@ -37,6 +37,11 @@ class IdbStore {
|
|
|
37
37
|
return database === null || database === void 0 ? void 0 : database.transaction(this.name, mode).objectStore(this.name);
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
+
getAllKeys() {
|
|
41
|
+
return this.getStore("readonly").then(store => !store
|
|
42
|
+
? []
|
|
43
|
+
: promisifyRequest(store.getAllKeys()).then(keys => keys.map(String)));
|
|
44
|
+
}
|
|
40
45
|
get(key) {
|
|
41
46
|
return __awaiter(this, void 0, void 0, function* () {
|
|
42
47
|
return this.getStore("readonly").then(store => !store ? undefined : promisifyRequest(store.get(key)));
|
|
@@ -16,11 +16,9 @@ const syncOverBrowserTabs = (observingKey, onChange) => getWindow()?.addEventLis
|
|
|
16
16
|
onChange(newValue);
|
|
17
17
|
});
|
|
18
18
|
/** Effect to make an atom value expirable and reset to its defaulValue.
|
|
19
|
-
*
|
|
20
|
-
* __Note:__ When using `expiresAt`, a function returning the date should be prefered since using a static date might end in an infinite loop.
|
|
21
19
|
*
|
|
22
20
|
* @param {ExpirationOptions | undefined} options
|
|
23
|
-
* @param options.expiresAt Date at which the value expires
|
|
21
|
+
* @param options.expiresAt Date at which the value expires. Using a function returning the date should be prefered here, since using a static date might end in an infinite loop.
|
|
24
22
|
* @param options.expiresIn Milliseconds in which the value expires. Will be ignored if expiresAt is set.
|
|
25
23
|
*
|
|
26
24
|
* @returns The effect to be used on atoms.
|
|
@@ -2,6 +2,12 @@ import { createEffect } from "./create-effect";
|
|
|
2
2
|
import { CONFIG } from "../base";
|
|
3
3
|
import { IdbStore } from "../utils/idb-store";
|
|
4
4
|
let atomDb = null;
|
|
5
|
+
const getAtomDb = () => {
|
|
6
|
+
if (!atomDb) {
|
|
7
|
+
atomDb = new IdbStore(CONFIG.name ?? "yaasl");
|
|
8
|
+
}
|
|
9
|
+
return atomDb;
|
|
10
|
+
};
|
|
5
11
|
/** Middleware to save and load atom values to an indexedDb.
|
|
6
12
|
*
|
|
7
13
|
* Will use one database and store for all atoms with your `CONFIG.name`
|
|
@@ -14,14 +20,12 @@ let atomDb = null;
|
|
|
14
20
|
*
|
|
15
21
|
* @returns The effect to be used on atoms.
|
|
16
22
|
**/
|
|
17
|
-
export const indexedDb = createEffect(({ atom, options }) => {
|
|
23
|
+
export const indexedDb = Object.assign(createEffect(({ atom, options }) => {
|
|
18
24
|
const key = options?.key ?? atom.name;
|
|
19
25
|
return {
|
|
20
26
|
sort: "pre",
|
|
21
27
|
init: async ({ atom, set }) => {
|
|
22
|
-
|
|
23
|
-
atomDb = new IdbStore(CONFIG.name ?? "yaasl");
|
|
24
|
-
}
|
|
28
|
+
const atomDb = getAtomDb();
|
|
25
29
|
const existing = await atomDb.get(key);
|
|
26
30
|
if (existing != null) {
|
|
27
31
|
set(existing);
|
|
@@ -40,4 +44,9 @@ export const indexedDb = createEffect(({ atom, options }) => {
|
|
|
40
44
|
}
|
|
41
45
|
},
|
|
42
46
|
};
|
|
47
|
+
}), {
|
|
48
|
+
get: (key) => getAtomDb().get(key),
|
|
49
|
+
getAllKeys: () => getAtomDb().getAllKeys(),
|
|
50
|
+
set: (key, value) => getAtomDb().set(key, value),
|
|
51
|
+
delete: (key) => getAtomDb().delete(key),
|
|
43
52
|
});
|
|
@@ -24,6 +24,11 @@ export class IdbStore {
|
|
|
24
24
|
const database = await this.database;
|
|
25
25
|
return database?.transaction(this.name, mode).objectStore(this.name);
|
|
26
26
|
}
|
|
27
|
+
getAllKeys() {
|
|
28
|
+
return this.getStore("readonly").then(store => !store
|
|
29
|
+
? []
|
|
30
|
+
: promisifyRequest(store.getAllKeys()).then(keys => keys.map(String)));
|
|
31
|
+
}
|
|
27
32
|
async get(key) {
|
|
28
33
|
return this.getStore("readonly").then(store => !store ? undefined : promisifyRequest(store.get(key)));
|
|
29
34
|
}
|