@yaasl/core 0.11.0-alpha.1 → 0.11.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.
@@ -1,9 +1,10 @@
1
- import type { ActionType, EffectAtomCallback } from "./createEffect";
1
+ import type { ActionType, EffectAtomCallback, EffectMeta } from "./createEffect";
2
2
  import type { Atom } from "../base/createAtom";
3
3
  export declare class EffectActions<Value> {
4
4
  private readonly atom;
5
- private options;
6
- private actions;
5
+ readonly meta?: EffectMeta;
6
+ private readonly options;
7
+ private readonly actions;
7
8
  constructor(atom: Atom<Value>, effectCreator: EffectAtomCallback<unknown, Value>);
8
9
  createAction(action: ActionType): ((prev: Value) => Value | PromiseLike<Value>) | undefined;
9
10
  }
@@ -23,9 +23,14 @@ interface EffectSetupProps<Options, AtomValue> {
23
23
  atom: Atom<AtomValue>;
24
24
  options: Options;
25
25
  }
26
+ export interface EffectMeta {
27
+ /** Enforce a position of the effect in the execution order */
28
+ sort?: "pre" | "post";
29
+ }
26
30
  export type EffectAtomCallback<Options = unknown, AtomValue = unknown> = (atom: Atom<AtomValue>) => Effect<Options, AtomValue>;
27
- type EffectSetup<Options, AtomValue> = EffectActions<Options, AtomValue> | ((props: EffectSetupProps<Options, AtomValue>) => EffectActions<Options, AtomValue>);
31
+ type EffectSetup<Options, AtomValue> = (EffectMeta & EffectActions<Options, AtomValue>) | ((props: EffectSetupProps<Options, AtomValue>) => EffectMeta & EffectActions<Options, AtomValue>);
28
32
  export interface Effect<Options = unknown, AtomValue = unknown> {
33
+ meta: EffectMeta;
29
34
  options: Options;
30
35
  actions: EffectActions<Options, AtomValue>;
31
36
  }
@@ -41,6 +46,8 @@ export interface Effect<Options = unknown, AtomValue = unknown> {
41
46
  *
42
47
  * @param setup Effect actions or function to create effect actions.
43
48
  * Effect actions are fired in the atom lifecycle, alongside to the subscriptions.
49
+ * Additionally, you can pass a `sort` option, which will enforce a position of the
50
+ * effect in the execution order.
44
51
  *
45
52
  * @returns The effect to be used on atoms.
46
53
  **/
@@ -7,7 +7,8 @@ const isTruthy = (value) => !!value;
7
7
  class EffectActions {
8
8
  constructor(atom, effectCreator) {
9
9
  this.atom = atom;
10
- const { actions, options } = effectCreator(atom);
10
+ const { meta, actions, options } = effectCreator(atom);
11
+ this.meta = meta;
11
12
  this.options = options;
12
13
  this.actions = actions;
13
14
  }
@@ -35,7 +36,16 @@ class EffectDispatcher {
35
36
  constructor(atom, effects) {
36
37
  this.effects = [];
37
38
  this.queue = new Queue_1.Queue();
38
- this.effects = effects.map(create => new EffectActions(atom, create));
39
+ this.effects = effects
40
+ .map(create => new EffectActions(atom, create))
41
+ .sort((a, b) => {
42
+ var _a, _b, _c, _d;
43
+ return ((_a = a.meta) === null || _a === void 0 ? void 0 : _a.sort) === "pre" || ((_b = b.meta) === null || _b === void 0 ? void 0 : _b.sort) === "post"
44
+ ? -1
45
+ : ((_c = a.meta) === null || _c === void 0 ? void 0 : _c.sort) === "post" || ((_d = b.meta) === null || _d === void 0 ? void 0 : _d.sort) === "pre"
46
+ ? 1
47
+ : 0;
48
+ });
39
49
  }
40
50
  dispatch(action, startValue) {
41
51
  const tasks = this.effects
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
14
  exports.createEffect = void 0;
4
15
  /** Create effects to be used in combination with atoms.
@@ -13,13 +24,17 @@ exports.createEffect = void 0;
13
24
  *
14
25
  * @param setup Effect actions or function to create effect actions.
15
26
  * Effect actions are fired in the atom lifecycle, alongside to the subscriptions.
27
+ * Additionally, you can pass a `sort` option, which will enforce a position of the
28
+ * effect in the execution order.
16
29
  *
17
30
  * @returns The effect to be used on atoms.
18
31
  **/
19
32
  const createEffect = (setup) => (...[optionsArg]) => (atom) => {
20
33
  const options = optionsArg;
21
- const actions = setup instanceof Function ? setup({ options, atom }) : setup;
34
+ const _a = setup instanceof Function ? setup({ options, atom }) : setup, { sort } = _a, actions = __rest(_a, ["sort"]);
35
+ const meta = { sort };
22
36
  return {
37
+ meta,
23
38
  options,
24
39
  actions,
25
40
  };
@@ -28,6 +28,7 @@ exports.indexedDb = (0, createEffect_1.createEffect)(({ atom, options }) => {
28
28
  var _a;
29
29
  const key = (_a = options === null || options === void 0 ? void 0 : options.key) !== null && _a !== void 0 ? _a : atom.name;
30
30
  return {
31
+ sort: "pre",
31
32
  init: (_a) => __awaiter(void 0, [_a], void 0, function* ({ atom, set }) {
32
33
  var _b;
33
34
  if (!atomDb) {
@@ -22,6 +22,7 @@ exports.localStorage = (0, createEffect_1.createEffect)(({ atom, options = {} })
22
22
  onTabSync: noTabSync ? undefined : value => atom.set(value),
23
23
  });
24
24
  return {
25
+ sort: "pre",
25
26
  init: ({ set }) => {
26
27
  const existing = storage.get();
27
28
  if (existing != null) {
@@ -3,11 +3,13 @@ import { Queue } from "../utils/Queue";
3
3
  const isTruthy = (value) => !!value;
4
4
  export class EffectActions {
5
5
  atom;
6
+ meta;
6
7
  options;
7
8
  actions;
8
9
  constructor(atom, effectCreator) {
9
10
  this.atom = atom;
10
- const { actions, options } = effectCreator(atom);
11
+ const { meta, actions, options } = effectCreator(atom);
12
+ this.meta = meta;
11
13
  this.options = options;
12
14
  this.actions = actions;
13
15
  }
@@ -34,7 +36,13 @@ export class EffectDispatcher {
34
36
  effects = [];
35
37
  queue = new Queue();
36
38
  constructor(atom, effects) {
37
- this.effects = effects.map(create => new EffectActions(atom, create));
39
+ this.effects = effects
40
+ .map(create => new EffectActions(atom, create))
41
+ .sort((a, b) => a.meta?.sort === "pre" || b.meta?.sort === "post"
42
+ ? -1
43
+ : a.meta?.sort === "post" || b.meta?.sort === "pre"
44
+ ? 1
45
+ : 0);
38
46
  }
39
47
  dispatch(action, startValue) {
40
48
  const tasks = this.effects
@@ -10,13 +10,17 @@
10
10
  *
11
11
  * @param setup Effect actions or function to create effect actions.
12
12
  * Effect actions are fired in the atom lifecycle, alongside to the subscriptions.
13
+ * Additionally, you can pass a `sort` option, which will enforce a position of the
14
+ * effect in the execution order.
13
15
  *
14
16
  * @returns The effect to be used on atoms.
15
17
  **/
16
18
  export const createEffect = (setup) => (...[optionsArg]) => (atom) => {
17
19
  const options = optionsArg;
18
- const actions = setup instanceof Function ? setup({ options, atom }) : setup;
20
+ const { sort, ...actions } = setup instanceof Function ? setup({ options, atom }) : setup;
21
+ const meta = { sort };
19
22
  return {
23
+ meta,
20
24
  options,
21
25
  actions,
22
26
  };
@@ -15,6 +15,7 @@ let atomDb = null;
15
15
  export const indexedDb = createEffect(({ atom, options }) => {
16
16
  const key = options?.key ?? atom.name;
17
17
  return {
18
+ sort: "pre",
18
19
  init: async ({ atom, set }) => {
19
20
  if (!atomDb) {
20
21
  atomDb = new Store(CONFIG.name ?? "yaasl");
@@ -19,6 +19,7 @@ export const localStorage = createEffect(({ atom, options = {} }) => {
19
19
  onTabSync: noTabSync ? undefined : value => atom.set(value),
20
20
  });
21
21
  return {
22
+ sort: "pre",
22
23
  init: ({ set }) => {
23
24
  const existing = storage.get();
24
25
  if (existing != null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaasl/core",
3
- "version": "0.11.0-alpha.1",
3
+ "version": "0.11.0-alpha.2",
4
4
  "description": "yet another atomic store library (vanilla-js)",
5
5
  "author": "PrettyCoffee",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  "validate": "run-s lint test build"
41
41
  },
42
42
  "dependencies": {
43
- "@yaasl/utils": "0.11.0-alpha.1"
43
+ "@yaasl/utils": "0.11.0-alpha.2"
44
44
  },
45
45
  "eslintConfig": {
46
46
  "extends": [