@yaasl/core 0.13.0-alpha.4 → 0.13.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/dist/@types/effects/auto-sort.d.ts +13 -0
- package/dist/@types/effects/index.d.ts +2 -0
- package/dist/cjs/effects/auto-sort.js +37 -0
- package/dist/cjs/effects/index.js +3 -1
- package/dist/cjs/effects/indexed-db.js +23 -5
- package/dist/esm/effects/auto-sort.js +33 -0
- package/dist/esm/effects/index.js +1 -0
- package/dist/esm/effects/indexed-db.js +23 -5
- package/package.json +3 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface AutoSortOptions<TData> {
|
|
2
|
+
/** Function to sort the atom value. */
|
|
3
|
+
sortFn?: (a: TData, b: TData) => number;
|
|
4
|
+
}
|
|
5
|
+
/** Middleware to automatically sort the atom value.
|
|
6
|
+
* Only supports arrays and nullish values.
|
|
7
|
+
*
|
|
8
|
+
* @param {AutoSortOptions | undefined} options
|
|
9
|
+
* @param options.sortFn Function to sort the atom value.
|
|
10
|
+
*
|
|
11
|
+
* @returns The effect to be used on atoms.
|
|
12
|
+
**/
|
|
13
|
+
export declare const autoSort: <TData>({ sortFn }: AutoSortOptions<TData>) => import("./create-effect").EffectAtomCallback<AutoSortOptions<TData>, TData[] | null | undefined>;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { createEffect } from "./create-effect";
|
|
2
2
|
export type { Effect, EffectPayload, EffectAtomCallback } from "./create-effect";
|
|
3
|
+
export { autoSort } from "./auto-sort";
|
|
4
|
+
export type { AutoSortOptions } from "./auto-sort";
|
|
3
5
|
export { localStorage } from "./local-storage";
|
|
4
6
|
export type { LocalStorageOptions } from "./local-storage";
|
|
5
7
|
export { sessionStorage } from "./session-storage";
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.autoSort = void 0;
|
|
4
|
+
const utils_1 = require("@yaasl/utils");
|
|
5
|
+
const create_effect_1 = require("./create-effect");
|
|
6
|
+
const forceArray = (value, atom) => {
|
|
7
|
+
if (value == null)
|
|
8
|
+
return [];
|
|
9
|
+
if (Array.isArray(value))
|
|
10
|
+
return value;
|
|
11
|
+
const message = (0, utils_1.consoleMessage)(`Value type is not compatile with the autoSort effect. Value must be an array or nullish.`, { scope: atom.name });
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
13
|
+
throw new Error(message + "\nValue: " + String(value));
|
|
14
|
+
};
|
|
15
|
+
/** Middleware to automatically sort the atom value.
|
|
16
|
+
* Only supports arrays and nullish values.
|
|
17
|
+
*
|
|
18
|
+
* @param {AutoSortOptions | undefined} options
|
|
19
|
+
* @param options.sortFn Function to sort the atom value.
|
|
20
|
+
*
|
|
21
|
+
* @returns The effect to be used on atoms.
|
|
22
|
+
**/
|
|
23
|
+
const autoSort = ({ sortFn }) => {
|
|
24
|
+
const sortEffect = (0, create_effect_1.createEffect)({
|
|
25
|
+
sort: "pre",
|
|
26
|
+
init: ({ atom, value, options, set }) => {
|
|
27
|
+
const array = forceArray(value, atom);
|
|
28
|
+
set(array.toSorted(options.sortFn));
|
|
29
|
+
},
|
|
30
|
+
set: ({ atom, value, options, set }) => {
|
|
31
|
+
const array = forceArray(value, atom);
|
|
32
|
+
set(array.toSorted(options.sortFn));
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
return sortEffect({ sortFn });
|
|
36
|
+
};
|
|
37
|
+
exports.autoSort = autoSort;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createMigrationStep = exports.migration = exports.expiration = exports.indexedDb = exports.sessionStorage = exports.localStorage = exports.createEffect = void 0;
|
|
3
|
+
exports.createMigrationStep = exports.migration = exports.expiration = exports.indexedDb = exports.sessionStorage = exports.localStorage = exports.autoSort = exports.createEffect = void 0;
|
|
4
4
|
var create_effect_1 = require("./create-effect");
|
|
5
5
|
Object.defineProperty(exports, "createEffect", { enumerable: true, get: function () { return create_effect_1.createEffect; } });
|
|
6
|
+
var auto_sort_1 = require("./auto-sort");
|
|
7
|
+
Object.defineProperty(exports, "autoSort", { enumerable: true, get: function () { return auto_sort_1.autoSort; } });
|
|
6
8
|
var local_storage_1 = require("./local-storage");
|
|
7
9
|
Object.defineProperty(exports, "localStorage", { enumerable: true, get: function () { return local_storage_1.localStorage; } });
|
|
8
10
|
var session_storage_1 = require("./session-storage");
|
|
@@ -18,9 +18,24 @@ let atomDb = null;
|
|
|
18
18
|
const createSync = (storeKey, onTabSync) => {
|
|
19
19
|
const key = (0, get_scoped_key_1.getScopedKey)(storeKey);
|
|
20
20
|
const channel = new BroadcastChannel(key);
|
|
21
|
-
|
|
21
|
+
let changeTrigger = null;
|
|
22
|
+
channel.onmessage = () => {
|
|
23
|
+
if (changeTrigger === "self") {
|
|
24
|
+
changeTrigger = null;
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
changeTrigger = "sync";
|
|
28
|
+
onTabSync();
|
|
29
|
+
};
|
|
22
30
|
return {
|
|
23
|
-
pushSync: () =>
|
|
31
|
+
pushSync: () => {
|
|
32
|
+
if (changeTrigger === "sync") {
|
|
33
|
+
changeTrigger = null;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
changeTrigger = "self";
|
|
37
|
+
channel.postMessage("sync");
|
|
38
|
+
},
|
|
24
39
|
};
|
|
25
40
|
};
|
|
26
41
|
/** Middleware to save and load atom values to an indexedDb.
|
|
@@ -60,9 +75,12 @@ exports.indexedDb = (0, create_effect_1.createEffect)(({ atom, options }) => {
|
|
|
60
75
|
yield atomDb.set(key, atom.defaultValue);
|
|
61
76
|
}
|
|
62
77
|
}),
|
|
63
|
-
set: ({ value }) => {
|
|
64
|
-
|
|
65
|
-
|
|
78
|
+
set: ({ value, atom }) => {
|
|
79
|
+
const action = value === atom.defaultValue
|
|
80
|
+
? atomDb === null || atomDb === void 0 ? void 0 : atomDb.delete(key)
|
|
81
|
+
: atomDb === null || atomDb === void 0 ? void 0 : atomDb.set(key, value);
|
|
82
|
+
// don't wait to set the atom value, directly pass it into the atom
|
|
83
|
+
void (action === null || action === void 0 ? void 0 : action.then(pushSync));
|
|
66
84
|
},
|
|
67
85
|
};
|
|
68
86
|
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { consoleMessage } from "@yaasl/utils";
|
|
2
|
+
import { createEffect } from "./create-effect";
|
|
3
|
+
const forceArray = (value, atom) => {
|
|
4
|
+
if (value == null)
|
|
5
|
+
return [];
|
|
6
|
+
if (Array.isArray(value))
|
|
7
|
+
return value;
|
|
8
|
+
const message = consoleMessage(`Value type is not compatile with the autoSort effect. Value must be an array or nullish.`, { scope: atom.name });
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
10
|
+
throw new Error(message + "\nValue: " + String(value));
|
|
11
|
+
};
|
|
12
|
+
/** Middleware to automatically sort the atom value.
|
|
13
|
+
* Only supports arrays and nullish values.
|
|
14
|
+
*
|
|
15
|
+
* @param {AutoSortOptions | undefined} options
|
|
16
|
+
* @param options.sortFn Function to sort the atom value.
|
|
17
|
+
*
|
|
18
|
+
* @returns The effect to be used on atoms.
|
|
19
|
+
**/
|
|
20
|
+
export const autoSort = ({ sortFn }) => {
|
|
21
|
+
const sortEffect = createEffect({
|
|
22
|
+
sort: "pre",
|
|
23
|
+
init: ({ atom, value, options, set }) => {
|
|
24
|
+
const array = forceArray(value, atom);
|
|
25
|
+
set(array.toSorted(options.sortFn));
|
|
26
|
+
},
|
|
27
|
+
set: ({ atom, value, options, set }) => {
|
|
28
|
+
const array = forceArray(value, atom);
|
|
29
|
+
set(array.toSorted(options.sortFn));
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
return sortEffect({ sortFn });
|
|
33
|
+
};
|
|
@@ -6,9 +6,24 @@ let atomDb = null;
|
|
|
6
6
|
const createSync = (storeKey, onTabSync) => {
|
|
7
7
|
const key = getScopedKey(storeKey);
|
|
8
8
|
const channel = new BroadcastChannel(key);
|
|
9
|
-
|
|
9
|
+
let changeTrigger = null;
|
|
10
|
+
channel.onmessage = () => {
|
|
11
|
+
if (changeTrigger === "self") {
|
|
12
|
+
changeTrigger = null;
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
changeTrigger = "sync";
|
|
16
|
+
onTabSync();
|
|
17
|
+
};
|
|
10
18
|
return {
|
|
11
|
-
pushSync: () =>
|
|
19
|
+
pushSync: () => {
|
|
20
|
+
if (changeTrigger === "sync") {
|
|
21
|
+
changeTrigger = null;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
changeTrigger = "self";
|
|
25
|
+
channel.postMessage("sync");
|
|
26
|
+
},
|
|
12
27
|
};
|
|
13
28
|
};
|
|
14
29
|
/** Middleware to save and load atom values to an indexedDb.
|
|
@@ -46,9 +61,12 @@ export const indexedDb = createEffect(({ atom, options }) => {
|
|
|
46
61
|
await atomDb.set(key, atom.defaultValue);
|
|
47
62
|
}
|
|
48
63
|
},
|
|
49
|
-
set: ({ value }) => {
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
set: ({ value, atom }) => {
|
|
65
|
+
const action = value === atom.defaultValue
|
|
66
|
+
? atomDb?.delete(key)
|
|
67
|
+
: atomDb?.set(key, value);
|
|
68
|
+
// don't wait to set the atom value, directly pass it into the atom
|
|
69
|
+
void action?.then(pushSync);
|
|
52
70
|
},
|
|
53
71
|
};
|
|
54
72
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaasl/core",
|
|
3
|
-
"version": "0.13.0
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "yet another atomic store library (vanilla-js)",
|
|
5
5
|
"author": "PrettyCoffee",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,10 +40,10 @@
|
|
|
40
40
|
"validate": "run-s lint test build"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@yaasl/utils": "^0.
|
|
43
|
+
"@yaasl/utils": "^0.13.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@yaasl/utils": "^0.
|
|
46
|
+
"@yaasl/utils": "^0.13.0"
|
|
47
47
|
},
|
|
48
48
|
"lint-staged": {
|
|
49
49
|
"*.{ts,tsx}": [
|