@yaasl/core 0.11.0-alpha.2 → 0.11.0-alpha.3
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/base/createActions.d.ts +1 -4
- package/dist/@types/utils/Store.d.ts +2 -2
- package/dist/cjs/base/createDerived.js +0 -2
- package/dist/cjs/effects/expiration.js +18 -9
- package/dist/cjs/effects/migration.js +5 -3
- package/dist/cjs/utils/Expiration.js +7 -1
- package/dist/cjs/utils/LocalStorage.js +14 -6
- package/dist/cjs/utils/Store.js +9 -7
- package/dist/esm/base/createDerived.js +0 -2
- package/dist/esm/effects/expiration.js +7 -2
- package/dist/esm/effects/migration.js +4 -4
- package/dist/esm/utils/Expiration.js +6 -1
- package/dist/esm/utils/LocalStorage.js +7 -3
- package/dist/esm/utils/Store.js +8 -7
- package/package.json +2 -7
- package/tsconfig.json +0 -3
- package/tsconfig.node.json +0 -9
|
@@ -2,10 +2,7 @@ import type { Atom } from "./createAtom";
|
|
|
2
2
|
import type { SettableDerive } from "./createDerived";
|
|
3
3
|
type Reducer<State> = (state: State, ...payloadArgs: any[]) => State;
|
|
4
4
|
export type Reducers<State> = Record<string, Reducer<State>>;
|
|
5
|
-
type Payload<R extends Reducer<any>> = Parameters<R> extends [
|
|
6
|
-
any,
|
|
7
|
-
...infer PayloadArgs
|
|
8
|
-
] ? PayloadArgs : [];
|
|
5
|
+
type Payload<R extends Reducer<any>> = Parameters<R> extends [any, ...infer PayloadArgs] ? PayloadArgs : [];
|
|
9
6
|
export type Actions<State, R extends Reducers<State>> = {
|
|
10
7
|
[K in keyof R]: (...payloadArgs: Payload<R[K]>) => void;
|
|
11
8
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export declare class Store<T> {
|
|
2
2
|
private name;
|
|
3
|
-
private database
|
|
3
|
+
private database?;
|
|
4
4
|
constructor(name: string);
|
|
5
5
|
private getStore;
|
|
6
6
|
get(key: string): Promise<T | undefined>;
|
|
7
|
-
set(key: string, value: T): Promise<
|
|
7
|
+
set(key: string, value: T): Promise<IDBValidKey | undefined>;
|
|
8
8
|
delete(key: string): Promise<undefined>;
|
|
9
9
|
}
|
|
@@ -14,7 +14,6 @@ class Derive extends Stateful_1.Stateful {
|
|
|
14
14
|
constructor(getter) {
|
|
15
15
|
super(undefined);
|
|
16
16
|
this.getter = getter;
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
17
|
this.getterDependencies = new Set();
|
|
19
18
|
this.value = getter({ get: dep => this.addGetDependency(dep) });
|
|
20
19
|
this.setDidInit(allDidInit(Array.from(this.getterDependencies)));
|
|
@@ -35,7 +34,6 @@ class SettableDerive extends Derive {
|
|
|
35
34
|
constructor(getter, setter) {
|
|
36
35
|
super(getter);
|
|
37
36
|
this.setter = setter;
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
37
|
this.setterDependencies = new Set();
|
|
40
38
|
setter({
|
|
41
39
|
value: this.get(),
|
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var _a, _b;
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.expiration = void 0;
|
|
5
|
+
const utils_1 = require("@yaasl/utils");
|
|
4
6
|
const createEffect_1 = require("./createEffect");
|
|
5
7
|
const base_1 = require("../base");
|
|
6
8
|
const Expiration_1 = require("../utils/Expiration");
|
|
7
|
-
const STORAGE =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
const STORAGE = (_b = (_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.localStorage) !== null && _b !== void 0 ? _b : {
|
|
10
|
+
getItem: () => null,
|
|
11
|
+
setItem: utils_1.toVoid,
|
|
12
|
+
removeItem: utils_1.toVoid,
|
|
13
|
+
};
|
|
14
|
+
const syncOverBrowserTabs = (observingKey, onChange) => {
|
|
15
|
+
var _a;
|
|
16
|
+
return (_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.addEventListener("storage", ({ key, newValue }) => {
|
|
17
|
+
if (observingKey !== key)
|
|
18
|
+
return;
|
|
19
|
+
const currentValue = STORAGE.getItem(observingKey);
|
|
20
|
+
if (currentValue === newValue)
|
|
21
|
+
return;
|
|
22
|
+
onChange(newValue);
|
|
23
|
+
});
|
|
24
|
+
};
|
|
16
25
|
/** Effect to make an atom value expirable and reset to its defaulValue.
|
|
17
26
|
*
|
|
18
27
|
* __Note:__ When using `expiresAt`, a function returning the date should be prefered since using a static date might end in an infinite loop.
|
|
@@ -20,12 +20,14 @@ const sortMigrations = (migrations) => {
|
|
|
20
20
|
}, [first]);
|
|
21
21
|
};
|
|
22
22
|
const getVersion = (atom) => {
|
|
23
|
+
var _a, _b;
|
|
23
24
|
const key = base_1.CONFIG.name ? `${base_1.CONFIG.name}/${atom.name}` : atom.name;
|
|
24
|
-
return localStorage.getItem(`${key}-version`);
|
|
25
|
+
return (_b = (_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.localStorage.getItem(`${key}-version`)) !== null && _b !== void 0 ? _b : null;
|
|
25
26
|
};
|
|
26
27
|
const setVersion = (atom, version) => {
|
|
28
|
+
var _a;
|
|
27
29
|
const key = base_1.CONFIG.name ? `${base_1.CONFIG.name}/${atom.name}` : atom.name;
|
|
28
|
-
localStorage.setItem(`${key}-version`, version);
|
|
30
|
+
(_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.localStorage.setItem(`${key}-version`, version);
|
|
29
31
|
};
|
|
30
32
|
const migrateVersion = (atom, data, migration) => {
|
|
31
33
|
if (migration.validate && !migration.validate(data)) {
|
|
@@ -74,7 +76,7 @@ exports.migration = (0, createEffect_1.createEffect)({
|
|
|
74
76
|
const steps = sortMigrations(options.steps);
|
|
75
77
|
const currentVersion = getVersion(atom);
|
|
76
78
|
const isLatestVersion = currentVersion === ((_a = steps.at(-1)) === null || _a === void 0 ? void 0 : _a.version);
|
|
77
|
-
if (steps.length
|
|
79
|
+
if (steps.length === 0 || isLatestVersion) {
|
|
78
80
|
return;
|
|
79
81
|
}
|
|
80
82
|
if (atom.get() === atom.defaultValue) {
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var _a, _b;
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.Expiration = void 0;
|
|
4
|
-
const
|
|
5
|
+
const utils_1 = require("@yaasl/utils");
|
|
6
|
+
const STORAGE = (_b = (_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.localStorage) !== null && _b !== void 0 ? _b : {
|
|
7
|
+
getItem: () => null,
|
|
8
|
+
setItem: utils_1.toVoid,
|
|
9
|
+
removeItem: utils_1.toVoid,
|
|
10
|
+
};
|
|
5
11
|
class Expiration {
|
|
6
12
|
constructor({ key, expiresAt, expiresIn }) {
|
|
7
13
|
this.timeout = null;
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var _a, _b;
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.LocalStorage = void 0;
|
|
4
5
|
const utils_1 = require("@yaasl/utils");
|
|
5
|
-
const STORAGE =
|
|
6
|
+
const STORAGE = (_b = (_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.localStorage) !== null && _b !== void 0 ? _b : {
|
|
7
|
+
getItem: () => null,
|
|
8
|
+
setItem: utils_1.toVoid,
|
|
9
|
+
removeItem: utils_1.toVoid,
|
|
10
|
+
};
|
|
6
11
|
const defaultParser = {
|
|
7
12
|
parse: JSON.parse,
|
|
8
13
|
stringify: JSON.stringify,
|
|
9
14
|
};
|
|
10
|
-
const syncOverBrowserTabs = (observingKey, onTabSync) =>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
const syncOverBrowserTabs = (observingKey, onTabSync) => {
|
|
16
|
+
var _a;
|
|
17
|
+
return (_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.addEventListener("storage", ({ key, newValue }) => {
|
|
18
|
+
if (observingKey !== key)
|
|
19
|
+
return;
|
|
20
|
+
onTabSync(newValue);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
15
23
|
class LocalStorage {
|
|
16
24
|
constructor(key, options = {}) {
|
|
17
25
|
var _a;
|
package/dist/cjs/utils/Store.js
CHANGED
|
@@ -10,14 +10,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Store = void 0;
|
|
13
|
+
const utils_1 = require("@yaasl/utils");
|
|
13
14
|
const promisifyRequest = (request) => new Promise((resolve, reject) => {
|
|
14
15
|
request.onsuccess = () => resolve(request.result);
|
|
15
16
|
request.onerror = () => reject(request.error);
|
|
16
17
|
});
|
|
17
18
|
class Store {
|
|
18
19
|
constructor(name) {
|
|
20
|
+
var _a;
|
|
19
21
|
this.name = name;
|
|
20
|
-
const openRequest = indexedDB.open(`${name}-database`);
|
|
22
|
+
const openRequest = (_a = (0, utils_1.getWindow)()) === null || _a === void 0 ? void 0 : _a.indexedDB.open(`${name}-database`);
|
|
23
|
+
if (!openRequest)
|
|
24
|
+
return;
|
|
21
25
|
this.database = promisifyRequest(openRequest);
|
|
22
26
|
openRequest.onupgradeneeded = () => {
|
|
23
27
|
const database = openRequest.result;
|
|
@@ -30,21 +34,19 @@ class Store {
|
|
|
30
34
|
getStore(mode) {
|
|
31
35
|
return __awaiter(this, void 0, void 0, function* () {
|
|
32
36
|
const database = yield this.database;
|
|
33
|
-
return database.transaction(this.name, mode).objectStore(this.name);
|
|
37
|
+
return database === null || database === void 0 ? void 0 : database.transaction(this.name, mode).objectStore(this.name);
|
|
34
38
|
});
|
|
35
39
|
}
|
|
36
40
|
get(key) {
|
|
37
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
return this.getStore("readonly").then(store => promisifyRequest(store.get(key)));
|
|
42
|
+
return this.getStore("readonly").then(store => !store ? undefined : promisifyRequest(store.get(key)));
|
|
39
43
|
});
|
|
40
44
|
}
|
|
41
45
|
set(key, value) {
|
|
42
|
-
return this.getStore("readwrite").then((store) => __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
yield promisifyRequest(store.put(value, key));
|
|
44
|
-
}));
|
|
46
|
+
return this.getStore("readwrite").then((store) => __awaiter(this, void 0, void 0, function* () { return !store ? undefined : promisifyRequest(store.put(value, key)); }));
|
|
45
47
|
}
|
|
46
48
|
delete(key) {
|
|
47
|
-
return this.getStore("readwrite").then(store => promisifyRequest(store.delete(key)));
|
|
49
|
+
return this.getStore("readwrite").then(store => !store ? undefined : promisifyRequest(store.delete(key)));
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
52
|
exports.Store = Store;
|
|
@@ -8,7 +8,6 @@ const allDidInit = (atoms) => {
|
|
|
8
8
|
};
|
|
9
9
|
export class Derive extends Stateful {
|
|
10
10
|
getter;
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
11
|
getterDependencies = new Set();
|
|
13
12
|
constructor(getter) {
|
|
14
13
|
super(undefined);
|
|
@@ -29,7 +28,6 @@ export class Derive extends Stateful {
|
|
|
29
28
|
}
|
|
30
29
|
export class SettableDerive extends Derive {
|
|
31
30
|
setter;
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
31
|
setterDependencies = new Set();
|
|
34
32
|
constructor(getter, setter) {
|
|
35
33
|
super(getter);
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { toVoid, getWindow } from "@yaasl/utils";
|
|
1
2
|
import { createEffect } from "./createEffect";
|
|
2
3
|
import { CONFIG } from "../base";
|
|
3
4
|
import { Expiration } from "../utils/Expiration";
|
|
4
|
-
const STORAGE =
|
|
5
|
-
|
|
5
|
+
const STORAGE = getWindow()?.localStorage ?? {
|
|
6
|
+
getItem: () => null,
|
|
7
|
+
setItem: toVoid,
|
|
8
|
+
removeItem: toVoid,
|
|
9
|
+
};
|
|
10
|
+
const syncOverBrowserTabs = (observingKey, onChange) => getWindow()?.addEventListener("storage", ({ key, newValue }) => {
|
|
6
11
|
if (observingKey !== key)
|
|
7
12
|
return;
|
|
8
13
|
const currentValue = STORAGE.getItem(observingKey);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { log } from "@yaasl/utils";
|
|
1
|
+
import { getWindow, log } from "@yaasl/utils";
|
|
2
2
|
import { createEffect } from "./createEffect";
|
|
3
3
|
import { CONFIG } from "../base";
|
|
4
4
|
const sortMigrations = (migrations) => {
|
|
@@ -18,11 +18,11 @@ const sortMigrations = (migrations) => {
|
|
|
18
18
|
};
|
|
19
19
|
const getVersion = (atom) => {
|
|
20
20
|
const key = CONFIG.name ? `${CONFIG.name}/${atom.name}` : atom.name;
|
|
21
|
-
return localStorage.getItem(`${key}-version`);
|
|
21
|
+
return getWindow()?.localStorage.getItem(`${key}-version`) ?? null;
|
|
22
22
|
};
|
|
23
23
|
const setVersion = (atom, version) => {
|
|
24
24
|
const key = CONFIG.name ? `${CONFIG.name}/${atom.name}` : atom.name;
|
|
25
|
-
localStorage.setItem(`${key}-version`, version);
|
|
25
|
+
getWindow()?.localStorage.setItem(`${key}-version`, version);
|
|
26
26
|
};
|
|
27
27
|
const migrateVersion = (atom, data, migration) => {
|
|
28
28
|
if (migration.validate && !migration.validate(data)) {
|
|
@@ -70,7 +70,7 @@ export const migration = createEffect({
|
|
|
70
70
|
const steps = sortMigrations(options.steps);
|
|
71
71
|
const currentVersion = getVersion(atom);
|
|
72
72
|
const isLatestVersion = currentVersion === steps.at(-1)?.version;
|
|
73
|
-
if (steps.length
|
|
73
|
+
if (steps.length === 0 || isLatestVersion) {
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
76
|
if (atom.get() === atom.defaultValue) {
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { consoleMessage, log } from "@yaasl/utils";
|
|
2
|
-
const STORAGE =
|
|
1
|
+
import { consoleMessage, log, toVoid, getWindow } from "@yaasl/utils";
|
|
2
|
+
const STORAGE = getWindow()?.localStorage ?? {
|
|
3
|
+
getItem: () => null,
|
|
4
|
+
setItem: toVoid,
|
|
5
|
+
removeItem: toVoid,
|
|
6
|
+
};
|
|
3
7
|
const defaultParser = {
|
|
4
8
|
parse: JSON.parse,
|
|
5
9
|
stringify: JSON.stringify,
|
|
6
10
|
};
|
|
7
|
-
const syncOverBrowserTabs = (observingKey, onTabSync) =>
|
|
11
|
+
const syncOverBrowserTabs = (observingKey, onTabSync) => getWindow()?.addEventListener("storage", ({ key, newValue }) => {
|
|
8
12
|
if (observingKey !== key)
|
|
9
13
|
return;
|
|
10
14
|
onTabSync(newValue);
|
package/dist/esm/utils/Store.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getWindow } from "@yaasl/utils";
|
|
1
2
|
const promisifyRequest = (request) => new Promise((resolve, reject) => {
|
|
2
3
|
request.onsuccess = () => resolve(request.result);
|
|
3
4
|
request.onerror = () => reject(request.error);
|
|
@@ -7,7 +8,9 @@ export class Store {
|
|
|
7
8
|
database;
|
|
8
9
|
constructor(name) {
|
|
9
10
|
this.name = name;
|
|
10
|
-
const openRequest = indexedDB.open(`${name}-database`);
|
|
11
|
+
const openRequest = getWindow()?.indexedDB.open(`${name}-database`);
|
|
12
|
+
if (!openRequest)
|
|
13
|
+
return;
|
|
11
14
|
this.database = promisifyRequest(openRequest);
|
|
12
15
|
openRequest.onupgradeneeded = () => {
|
|
13
16
|
const database = openRequest.result;
|
|
@@ -19,17 +22,15 @@ export class Store {
|
|
|
19
22
|
}
|
|
20
23
|
async getStore(mode) {
|
|
21
24
|
const database = await this.database;
|
|
22
|
-
return database
|
|
25
|
+
return database?.transaction(this.name, mode).objectStore(this.name);
|
|
23
26
|
}
|
|
24
27
|
async get(key) {
|
|
25
|
-
return this.getStore("readonly").then(store => promisifyRequest(store.get(key)));
|
|
28
|
+
return this.getStore("readonly").then(store => !store ? undefined : promisifyRequest(store.get(key)));
|
|
26
29
|
}
|
|
27
30
|
set(key, value) {
|
|
28
|
-
return this.getStore("readwrite").then(async (store) =>
|
|
29
|
-
await promisifyRequest(store.put(value, key));
|
|
30
|
-
});
|
|
31
|
+
return this.getStore("readwrite").then(async (store) => !store ? undefined : promisifyRequest(store.put(value, key)));
|
|
31
32
|
}
|
|
32
33
|
delete(key) {
|
|
33
|
-
return this.getStore("readwrite").then(store => promisifyRequest(store.delete(key)));
|
|
34
|
+
return this.getStore("readwrite").then(store => !store ? undefined : promisifyRequest(store.delete(key)));
|
|
34
35
|
}
|
|
35
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaasl/core",
|
|
3
|
-
"version": "0.11.0-alpha.
|
|
3
|
+
"version": "0.11.0-alpha.3",
|
|
4
4
|
"description": "yet another atomic store library (vanilla-js)",
|
|
5
5
|
"author": "PrettyCoffee",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,12 +40,7 @@
|
|
|
40
40
|
"validate": "run-s lint test build"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@yaasl/utils": "0.11.0-alpha.
|
|
44
|
-
},
|
|
45
|
-
"eslintConfig": {
|
|
46
|
-
"extends": [
|
|
47
|
-
"../../.eslintrc"
|
|
48
|
-
]
|
|
43
|
+
"@yaasl/utils": "0.11.0-alpha.3"
|
|
49
44
|
},
|
|
50
45
|
"lint-staged": {
|
|
51
46
|
"*.{ts,tsx}": [
|
package/tsconfig.json
DELETED