@yaasl/core 0.13.0-alpha.5 → 0.13.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.
@@ -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");
@@ -20,7 +20,6 @@ const createSync = (storeKey, onTabSync) => {
20
20
  const channel = new BroadcastChannel(key);
21
21
  let changeTrigger = null;
22
22
  channel.onmessage = () => {
23
- console.log(changeTrigger);
24
23
  if (changeTrigger === "self") {
25
24
  changeTrigger = null;
26
25
  return;
@@ -30,7 +29,6 @@ const createSync = (storeKey, onTabSync) => {
30
29
  };
31
30
  return {
32
31
  pushSync: () => {
33
- console.log(changeTrigger);
34
32
  if (changeTrigger === "sync") {
35
33
  changeTrigger = null;
36
34
  return;
@@ -77,9 +75,12 @@ exports.indexedDb = (0, create_effect_1.createEffect)(({ atom, options }) => {
77
75
  yield atomDb.set(key, atom.defaultValue);
78
76
  }
79
77
  }),
80
- set: ({ value }) => {
81
- // don't wait to set the atom value,directly pass it into the atom
82
- void (atomDb === null || atomDb === void 0 ? void 0 : atomDb.set(key, value).then(pushSync));
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));
83
84
  },
84
85
  };
85
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
+ };
@@ -1,4 +1,5 @@
1
1
  export { createEffect } from "./create-effect";
2
+ export { autoSort } from "./auto-sort";
2
3
  export { localStorage } from "./local-storage";
3
4
  export { sessionStorage } from "./session-storage";
4
5
  export { indexedDb } from "./indexed-db";
@@ -8,7 +8,6 @@ const createSync = (storeKey, onTabSync) => {
8
8
  const channel = new BroadcastChannel(key);
9
9
  let changeTrigger = null;
10
10
  channel.onmessage = () => {
11
- console.log(changeTrigger);
12
11
  if (changeTrigger === "self") {
13
12
  changeTrigger = null;
14
13
  return;
@@ -18,7 +17,6 @@ const createSync = (storeKey, onTabSync) => {
18
17
  };
19
18
  return {
20
19
  pushSync: () => {
21
- console.log(changeTrigger);
22
20
  if (changeTrigger === "sync") {
23
21
  changeTrigger = null;
24
22
  return;
@@ -63,9 +61,12 @@ export const indexedDb = createEffect(({ atom, options }) => {
63
61
  await atomDb.set(key, atom.defaultValue);
64
62
  }
65
63
  },
66
- set: ({ value }) => {
67
- // don't wait to set the atom value,directly pass it into the atom
68
- void atomDb?.set(key, value).then(pushSync);
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);
69
70
  },
70
71
  };
71
72
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaasl/core",
3
- "version": "0.13.0-alpha.5",
3
+ "version": "0.13.1",
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.12.1"
43
+ "@yaasl/utils": "^0.13.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@yaasl/utils": "^0.12.1"
46
+ "@yaasl/utils": "^0.13.1"
47
47
  },
48
48
  "lint-staged": {
49
49
  "*.{ts,tsx}": [