dharma-core 0.9.1 → 0.10.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.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright 2025 Frans Ekman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,3 +1,4 @@
1
+ export * from "./lib/createEffect";
1
2
  export * from "./lib/createStore";
2
3
  export * from "./lib/derive";
3
4
  export * from "./lib/merge";
package/dist/cjs/index.js CHANGED
@@ -1,11 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ var createEffect = require('./lib/createEffect.js');
3
4
  var createStore = require('./lib/createStore.js');
4
5
  var derive = require('./lib/derive.js');
5
6
  var merge = require('./lib/merge.js');
6
7
 
7
8
 
8
9
 
10
+ exports.createEffect = createEffect.createEffect;
9
11
  exports.createStore = createStore.createStore;
10
12
  exports.derive = derive.derive;
11
13
  exports.merge = merge.merge;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
@@ -0,0 +1,13 @@
1
+ import { Effect, Store } from "../types/types";
2
+ /**
3
+ * Creates an effect that runs when the store's state changes.
4
+ *
5
+ * @param store - The store instance to subscribe to
6
+ * @param effectFn - The function to run when dependencies change. Receives the current state as argument.
7
+ * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the effect will only run when these dependencies change.
8
+ *
9
+ * @returns An Effect object with mount and unmount methods
10
+ *
11
+ * @see {@link https://dharma.fransek.dev/core/createeffect/}
12
+ */
13
+ export declare const createEffect: <TState, TActions, TDerived>(store: Store<TState, TActions>, effectFn: (state: TState) => TDerived, dependencyFn?: (state: TState) => unknown[]) => Effect;
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ var deeplyEquals = require('./deeplyEquals.js');
4
+
5
+ /**
6
+ * Creates an effect that runs when the store's state changes.
7
+ *
8
+ * @param store - The store instance to subscribe to
9
+ * @param effectFn - The function to run when dependencies change. Receives the current state as argument.
10
+ * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the effect will only run when these dependencies change.
11
+ *
12
+ * @returns An Effect object with mount and unmount methods
13
+ *
14
+ * @see {@link https://dharma.fransek.dev/core/createeffect/}
15
+ */
16
+ const createEffect = (store, effectFn, dependencyFn) => {
17
+ let prev;
18
+ let unsubscribe;
19
+ const execute = () => effectFn(store.get());
20
+ const effect = () => {
21
+ const next = dependencyFn?.(store.get());
22
+ if (!prev || !deeplyEquals.deeplyEquals(prev, next)) {
23
+ prev = next;
24
+ execute();
25
+ }
26
+ };
27
+ const mount = () => {
28
+ if (!unsubscribe) {
29
+ unsubscribe = store.subscribe(effect);
30
+ }
31
+ return unmount;
32
+ };
33
+ const unmount = () => {
34
+ unsubscribe?.();
35
+ unsubscribe = undefined;
36
+ };
37
+ return {
38
+ mount,
39
+ unmount,
40
+ };
41
+ };
42
+
43
+ exports.createEffect = createEffect;
44
+ //# sourceMappingURL=createEffect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEffect.js","sources":["../../../src/lib/createEffect.ts"],"sourcesContent":["import { Effect, Store } from \"../types/types\";\nimport { deeplyEquals } from \"./deeplyEquals\";\n\n/**\n * Creates an effect that runs when the store's state changes.\n *\n * @param store - The store instance to subscribe to\n * @param effectFn - The function to run when dependencies change. Receives the current state as argument.\n * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the effect will only run when these dependencies change.\n *\n * @returns An Effect object with mount and unmount methods\n *\n * @see {@link https://dharma.fransek.dev/core/createeffect/}\n */\nexport const createEffect = <TState, TActions, TDerived>(\n store: Store<TState, TActions>,\n effectFn: (state: TState) => TDerived,\n dependencyFn?: (state: TState) => unknown[],\n): Effect => {\n let prev: unknown[] | undefined;\n let unsubscribe: (() => void) | undefined;\n\n const execute = () => effectFn(store.get());\n\n const effect = () => {\n const next = dependencyFn?.(store.get());\n\n if (!prev || !deeplyEquals(prev, next)) {\n prev = next;\n execute();\n }\n };\n\n const mount = () => {\n if (!unsubscribe) {\n unsubscribe = store.subscribe(effect);\n }\n return unmount;\n };\n\n const unmount = () => {\n unsubscribe?.();\n unsubscribe = undefined;\n };\n\n return {\n mount,\n unmount,\n };\n};\n"],"names":["deeplyEquals"],"mappings":";;;;AAGA;;;;;;;;;;AAUG;AACI,MAAM,YAAY,GAAG,CAC1B,KAA8B,EAC9B,QAAqC,EACrC,YAA2C,KACjC;AACV,IAAA,IAAI,IAA2B;AAC/B,IAAA,IAAI,WAAqC;AAEzC,IAAA,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IAE3C,MAAM,MAAM,GAAG,MAAK;QAClB,MAAM,IAAI,GAAG,YAAY,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC;QAExC,IAAI,CAAC,IAAI,IAAI,CAACA,yBAAY,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE;YACtC,IAAI,GAAG,IAAI;AACX,YAAA,OAAO,EAAE;QACX;AACF,IAAA,CAAC;IAED,MAAM,KAAK,GAAG,MAAK;QACjB,IAAI,CAAC,WAAW,EAAE;AAChB,YAAA,WAAW,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC;QACvC;AACA,QAAA,OAAO,OAAO;AAChB,IAAA,CAAC;IAED,MAAM,OAAO,GAAG,MAAK;QACnB,WAAW,IAAI;QACf,WAAW,GAAG,SAAS;AACzB,IAAA,CAAC;IAED,OAAO;QACL,KAAK;QACL,OAAO;KACR;AACH;;;;"}
@@ -28,13 +28,7 @@ const derive = (store, deriveFn, dependencyFn) => {
28
28
  return memo;
29
29
  };
30
30
  const get = () => {
31
- if (!dependencyFn) {
32
- if (isStale) {
33
- return compute();
34
- }
35
- return memo;
36
- }
37
- const next = dependencyFn(store.get());
31
+ const next = dependencyFn?.(store.get());
38
32
  isStale &&= !prev || !deeplyEquals.deeplyEquals(prev, next);
39
33
  if (isStale) {
40
34
  prev = next;
@@ -55,13 +49,15 @@ const derive = (store, deriveFn, dependencyFn) => {
55
49
  }
56
50
  };
57
51
  };
52
+ const listener = () => {
53
+ isStale = true;
54
+ listeners.forEach((listener) => listener(get()));
55
+ };
58
56
  const mount = () => {
59
57
  if (!unsubscribe) {
60
- unsubscribe = store.subscribe(() => {
61
- isStale = true;
62
- listeners.forEach((listener) => listener(get()));
63
- });
58
+ unsubscribe = store.subscribe(listener);
64
59
  }
60
+ return unmount;
65
61
  };
66
62
  const unmount = () => {
67
63
  unsubscribe?.();
@@ -1 +1 @@
1
- {"version":3,"file":"derive.js","sources":["../../../src/lib/derive.ts"],"sourcesContent":["import { DerivedStore, Listener, Store } from \"../types/types\";\nimport { deeplyEquals } from \"./deeplyEquals\";\n\n/**\n * Creates a derived store from an existing store.\n *\n * A derived store computes its state based on the original store's state using a derive function.\n * The derived store's value is memoized and only recomputed when requested.\n *\n * @param store - The original store to derive from\n * @param deriveFn - A function that computes the derived state from the original state\n * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the derived state will only be recomputed when these dependencies change\n *\n * @returns A new store containing the derived state\n *\n * @see {@link https://dharma.fransek.dev/core/derive/}\n */\nexport const derive = <TState, TActions, TDerived>(\n store: Store<TState, TActions>,\n deriveFn: (state: TState) => TDerived,\n dependencyFn?: (state: TState) => unknown[],\n): DerivedStore<TDerived> => {\n let memo: TDerived | undefined;\n let prev: unknown[] | undefined;\n let isStale = true;\n let unsubscribe: (() => void) | undefined;\n const listeners = new Set<Listener<TDerived>>();\n\n const compute = () => {\n memo = deriveFn(store.get());\n isStale = false;\n return memo;\n };\n\n const get = () => {\n if (!dependencyFn) {\n if (isStale) {\n return compute();\n }\n\n return memo as TDerived;\n }\n\n const next = dependencyFn(store.get());\n isStale &&= !prev || !deeplyEquals(prev, next);\n\n if (isStale) {\n prev = next;\n return compute();\n }\n\n return memo as TDerived;\n };\n\n const subscribe = (listener: Listener<TDerived>) => {\n if (listeners.size === 0) {\n mount();\n }\n\n listener(get());\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n unmount();\n }\n };\n };\n\n const mount = () => {\n if (!unsubscribe) {\n unsubscribe = store.subscribe(() => {\n isStale = true;\n listeners.forEach((listener) => listener(get()));\n });\n }\n };\n\n const unmount = () => {\n unsubscribe?.();\n unsubscribe = undefined;\n };\n\n return {\n get,\n subscribe,\n actions: {\n mount,\n unmount,\n },\n };\n};\n"],"names":["deeplyEquals"],"mappings":";;;;AAGA;;;;;;;;;;;;;AAaG;AACI,MAAM,MAAM,GAAG,CACpB,KAA8B,EAC9B,QAAqC,EACrC,YAA2C,KACjB;AAC1B,IAAA,IAAI,IAA0B;AAC9B,IAAA,IAAI,IAA2B;IAC/B,IAAI,OAAO,GAAG,IAAI;AAClB,IAAA,IAAI,WAAqC;AACzC,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG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,CAAC,CAAC;QACJ;AACF,IAAA,CAAC;IAED,MAAM,OAAO,GAAG,MAAK;QACnB,WAAW,IAAI;QACf,WAAW,GAAG,SAAS;AACzB,IAAA,CAAC;IAED,OAAO;QACL,GAAG;QACH,SAAS;AACT,QAAA,OAAO,EAAE;YACP,KAAK;YACL,OAAO;AACR,SAAA;KACF;AACH;;;;"}
1
+ {"version":3,"file":"derive.js","sources":["../../../src/lib/derive.ts"],"sourcesContent":["import { DerivedStore, Listener, Store } from \"../types/types\";\nimport { deeplyEquals } from \"./deeplyEquals\";\n\n/**\n * Creates a derived store from an existing store.\n *\n * A derived store computes its state based on the original store's state using a derive function.\n * The derived store's value is memoized and only recomputed when requested.\n *\n * @param store - The original store to derive from\n * @param deriveFn - A function that computes the derived state from the original state\n * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the derived state will only be recomputed when these dependencies change\n *\n * @returns A new store containing the derived state\n *\n * @see {@link https://dharma.fransek.dev/core/derive/}\n */\nexport const derive = <TState, TActions, TDerived>(\n store: Store<TState, TActions>,\n deriveFn: (state: TState) => TDerived,\n dependencyFn?: (state: TState) => unknown[],\n): DerivedStore<TDerived> => {\n let memo: TDerived | undefined;\n let prev: unknown[] | undefined;\n let isStale = true;\n let unsubscribe: (() => void) | undefined;\n const listeners = new Set<Listener<TDerived>>();\n\n const compute = () => {\n memo = deriveFn(store.get());\n isStale = false;\n return memo;\n };\n\n const get = () => {\n const next = dependencyFn?.(store.get());\n isStale &&= !prev || !deeplyEquals(prev, next);\n\n if (isStale) {\n prev = next;\n return compute();\n }\n\n return memo as TDerived;\n };\n\n const subscribe = (listener: Listener<TDerived>) => {\n if (listeners.size === 0) {\n mount();\n }\n\n listener(get());\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n unmount();\n }\n };\n };\n\n const listener = () => {\n isStale = true;\n listeners.forEach((listener) => listener(get()));\n };\n\n const mount = () => {\n if (!unsubscribe) {\n unsubscribe = store.subscribe(listener);\n }\n return unmount;\n };\n\n const unmount = () => {\n unsubscribe?.();\n unsubscribe = undefined;\n };\n\n return {\n get,\n subscribe,\n actions: {\n mount,\n unmount,\n },\n };\n};\n"],"names":["deeplyEquals"],"mappings":";;;;AAGA;;;;;;;;;;;;;AAaG;AACI,MAAM,MAAM,GAAG,CACpB,KAA8B,EAC9B,QAAqC,EACrC,YAA2C,KACjB;AAC1B,IAAA,IAAI,IAA0B;AAC9B,IAAA,IAAI,IAA2B;IAC/B,IAAI,OAAO,GAAG,IAAI;AAClB,IAAA,IAAI,WAAqC;AACzC,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAsB;IAE/C,MAAM,OAAO,GAAG,MAAK;QACnB,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;QAC5B,OAAO,GAAG,KAAK;AACf,QAAA,OAAO,IAAI;AACb,IAAA,CAAC;IAED,MAAM,GAAG,GAAG,MAAK;QACf,MAAM,IAAI,GAAG,YAAY,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC;QACxC,OAAO,KAAK,CAAC,IAAI,IAAI,CAACA,yBAAY,CAAC,IAAI,EAAE,IAAI,CAAC;QAE9C,IAAI,OAAO,EAAE;YACX,IAAI,GAAG,IAAI;YACX,OAAO,OAAO,EAAE;QAClB;AAEA,QAAA,OAAO,IAAgB;AACzB,IAAA,CAAC;AAED,IAAA,MAAM,SAAS,GAAG,CAAC,QAA4B,KAAI;AACjD,QAAA,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;AACxB,YAAA,KAAK,EAAE;QACT;AAEA,QAAA,QAAQ,CAAC,GAAG,EAAE,CAAC;AACf,QAAA,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;AAEvB,QAAA,OAAO,MAAK;AACV,YAAA,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC;AAE1B,YAAA,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;AACxB,gBAAA,OAAO,EAAE;YACX;AACF,QAAA,CAAC;AACH,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;QACpB,OAAO,GAAG,IAAI;AACd,QAAA,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC;AAClD,IAAA,CAAC;IAED,MAAM,KAAK,GAAG,MAAK;QACjB,IAAI,CAAC,WAAW,EAAE;AAChB,YAAA,WAAW,GAAG,KAAK,CAAC,SAAS,CAAC,QAAQ,CAAC;QACzC;AACA,QAAA,OAAO,OAAO;AAChB,IAAA,CAAC;IAED,MAAM,OAAO,GAAG,MAAK;QACnB,WAAW,IAAI;QACf,WAAW,GAAG,SAAS;AACzB,IAAA,CAAC;IAED,OAAO;QACL,GAAG;QACH,SAAS;AACT,QAAA,OAAO,EAAE;YACP,KAAK;YACL,OAAO;AACR,SAAA;KACF;AACH;;;;"}
@@ -10,11 +10,17 @@ export type Store<TState, TActions> = {
10
10
  };
11
11
  export type DerivedStoreActions = {
12
12
  /** Start listening to changes in the original store. Called automatically on the first subscription. */
13
- mount: () => void;
13
+ mount: () => () => void;
14
14
  /** Stop listening to changes in the original store. Called automatically when there are no more subscribers. */
15
15
  unmount: () => void;
16
16
  };
17
17
  export type DerivedStore<TState> = Store<TState, DerivedStoreActions>;
18
+ export type Effect = {
19
+ /** Start listening to changes in the original store. */
20
+ mount: () => () => void;
21
+ /** Stop listening to changes in the original store. */
22
+ unmount: () => void;
23
+ };
18
24
  export type StoreEventContext<TState> = {
19
25
  /** The current state of the store. */
20
26
  state: TState;
@@ -1,3 +1,4 @@
1
+ export * from "./lib/createEffect";
1
2
  export * from "./lib/createStore";
2
3
  export * from "./lib/derive";
3
4
  export * from "./lib/merge";
package/dist/esm/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export { createEffect } from './lib/createEffect.js';
1
2
  export { createStore } from './lib/createStore.js';
2
3
  export { derive } from './lib/derive.js';
3
4
  export { merge } from './lib/merge.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
@@ -0,0 +1,13 @@
1
+ import { Effect, Store } from "../types/types";
2
+ /**
3
+ * Creates an effect that runs when the store's state changes.
4
+ *
5
+ * @param store - The store instance to subscribe to
6
+ * @param effectFn - The function to run when dependencies change. Receives the current state as argument.
7
+ * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the effect will only run when these dependencies change.
8
+ *
9
+ * @returns An Effect object with mount and unmount methods
10
+ *
11
+ * @see {@link https://dharma.fransek.dev/core/createeffect/}
12
+ */
13
+ export declare const createEffect: <TState, TActions, TDerived>(store: Store<TState, TActions>, effectFn: (state: TState) => TDerived, dependencyFn?: (state: TState) => unknown[]) => Effect;
@@ -0,0 +1,42 @@
1
+ import { deeplyEquals } from './deeplyEquals.js';
2
+
3
+ /**
4
+ * Creates an effect that runs when the store's state changes.
5
+ *
6
+ * @param store - The store instance to subscribe to
7
+ * @param effectFn - The function to run when dependencies change. Receives the current state as argument.
8
+ * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the effect will only run when these dependencies change.
9
+ *
10
+ * @returns An Effect object with mount and unmount methods
11
+ *
12
+ * @see {@link https://dharma.fransek.dev/core/createeffect/}
13
+ */
14
+ const createEffect = (store, effectFn, dependencyFn) => {
15
+ let prev;
16
+ let unsubscribe;
17
+ const execute = () => effectFn(store.get());
18
+ const effect = () => {
19
+ const next = dependencyFn?.(store.get());
20
+ if (!prev || !deeplyEquals(prev, next)) {
21
+ prev = next;
22
+ execute();
23
+ }
24
+ };
25
+ const mount = () => {
26
+ if (!unsubscribe) {
27
+ unsubscribe = store.subscribe(effect);
28
+ }
29
+ return unmount;
30
+ };
31
+ const unmount = () => {
32
+ unsubscribe?.();
33
+ unsubscribe = undefined;
34
+ };
35
+ return {
36
+ mount,
37
+ unmount,
38
+ };
39
+ };
40
+
41
+ export { createEffect };
42
+ //# sourceMappingURL=createEffect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEffect.js","sources":["../../../src/lib/createEffect.ts"],"sourcesContent":["import { Effect, Store } from \"../types/types\";\nimport { deeplyEquals } from \"./deeplyEquals\";\n\n/**\n * Creates an effect that runs when the store's state changes.\n *\n * @param store - The store instance to subscribe to\n * @param effectFn - The function to run when dependencies change. Receives the current state as argument.\n * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the effect will only run when these dependencies change.\n *\n * @returns An Effect object with mount and unmount methods\n *\n * @see {@link https://dharma.fransek.dev/core/createeffect/}\n */\nexport const createEffect = <TState, TActions, TDerived>(\n store: Store<TState, TActions>,\n effectFn: (state: TState) => TDerived,\n dependencyFn?: (state: TState) => unknown[],\n): Effect => {\n let prev: unknown[] | undefined;\n let unsubscribe: (() => void) | undefined;\n\n const execute = () => effectFn(store.get());\n\n const effect = () => {\n const next = dependencyFn?.(store.get());\n\n if (!prev || !deeplyEquals(prev, next)) {\n prev = next;\n execute();\n }\n };\n\n const mount = () => {\n if (!unsubscribe) {\n unsubscribe = store.subscribe(effect);\n }\n return unmount;\n };\n\n const unmount = () => {\n unsubscribe?.();\n unsubscribe = undefined;\n };\n\n return {\n mount,\n unmount,\n };\n};\n"],"names":[],"mappings":";;AAGA;;;;;;;;;;AAUG;AACI,MAAM,YAAY,GAAG,CAC1B,KAA8B,EAC9B,QAAqC,EACrC,YAA2C,KACjC;AACV,IAAA,IAAI,IAA2B;AAC/B,IAAA,IAAI,WAAqC;AAEzC,IAAA,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IAE3C,MAAM,MAAM,GAAG,MAAK;QAClB,MAAM,IAAI,GAAG,YAAY,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC;QAExC,IAAI,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE;YACtC,IAAI,GAAG,IAAI;AACX,YAAA,OAAO,EAAE;QACX;AACF,IAAA,CAAC;IAED,MAAM,KAAK,GAAG,MAAK;QACjB,IAAI,CAAC,WAAW,EAAE;AAChB,YAAA,WAAW,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC;QACvC;AACA,QAAA,OAAO,OAAO;AAChB,IAAA,CAAC;IAED,MAAM,OAAO,GAAG,MAAK;QACnB,WAAW,IAAI;QACf,WAAW,GAAG,SAAS;AACzB,IAAA,CAAC;IAED,OAAO;QACL,KAAK;QACL,OAAO;KACR;AACH;;;;"}
@@ -26,13 +26,7 @@ const derive = (store, deriveFn, dependencyFn) => {
26
26
  return memo;
27
27
  };
28
28
  const get = () => {
29
- if (!dependencyFn) {
30
- if (isStale) {
31
- return compute();
32
- }
33
- return memo;
34
- }
35
- const next = dependencyFn(store.get());
29
+ const next = dependencyFn?.(store.get());
36
30
  isStale &&= !prev || !deeplyEquals(prev, next);
37
31
  if (isStale) {
38
32
  prev = next;
@@ -53,13 +47,15 @@ const derive = (store, deriveFn, dependencyFn) => {
53
47
  }
54
48
  };
55
49
  };
50
+ const listener = () => {
51
+ isStale = true;
52
+ listeners.forEach((listener) => listener(get()));
53
+ };
56
54
  const mount = () => {
57
55
  if (!unsubscribe) {
58
- unsubscribe = store.subscribe(() => {
59
- isStale = true;
60
- listeners.forEach((listener) => listener(get()));
61
- });
56
+ unsubscribe = store.subscribe(listener);
62
57
  }
58
+ return unmount;
63
59
  };
64
60
  const unmount = () => {
65
61
  unsubscribe?.();
@@ -1 +1 @@
1
- {"version":3,"file":"derive.js","sources":["../../../src/lib/derive.ts"],"sourcesContent":["import { DerivedStore, Listener, Store } from \"../types/types\";\nimport { deeplyEquals } from \"./deeplyEquals\";\n\n/**\n * Creates a derived store from an existing store.\n *\n * A derived store computes its state based on the original store's state using a derive function.\n * The derived store's value is memoized and only recomputed when requested.\n *\n * @param store - The original store to derive from\n * @param deriveFn - A function that computes the derived state from the original state\n * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the derived state will only be recomputed when these dependencies change\n *\n * @returns A new store containing the derived state\n *\n * @see {@link https://dharma.fransek.dev/core/derive/}\n */\nexport const derive = <TState, TActions, TDerived>(\n store: Store<TState, TActions>,\n deriveFn: (state: TState) => TDerived,\n dependencyFn?: (state: TState) => unknown[],\n): DerivedStore<TDerived> => {\n let memo: TDerived | undefined;\n let prev: unknown[] | undefined;\n let isStale = true;\n let unsubscribe: (() => void) | undefined;\n const listeners = new Set<Listener<TDerived>>();\n\n const compute = () => {\n memo = deriveFn(store.get());\n isStale = false;\n return memo;\n };\n\n const get = () => {\n if (!dependencyFn) {\n if (isStale) {\n return compute();\n }\n\n return memo as TDerived;\n }\n\n const next = dependencyFn(store.get());\n isStale &&= !prev || !deeplyEquals(prev, next);\n\n if (isStale) {\n prev = next;\n return compute();\n }\n\n return memo as TDerived;\n };\n\n const subscribe = (listener: Listener<TDerived>) => {\n if (listeners.size === 0) {\n mount();\n }\n\n listener(get());\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n unmount();\n }\n };\n };\n\n const mount = () => {\n if (!unsubscribe) {\n unsubscribe = store.subscribe(() => {\n isStale = true;\n listeners.forEach((listener) => listener(get()));\n });\n }\n };\n\n const unmount = () => {\n unsubscribe?.();\n unsubscribe = undefined;\n };\n\n return {\n get,\n subscribe,\n actions: {\n mount,\n unmount,\n },\n };\n};\n"],"names":[],"mappings":";;AAGA;;;;;;;;;;;;;AAaG;AACI,MAAM,MAAM,GAAG,CACpB,KAA8B,EAC9B,QAAqC,EACrC,YAA2C,KACjB;AAC1B,IAAA,IAAI,IAA0B;AAC9B,IAAA,IAAI,IAA2B;IAC/B,IAAI,OAAO,GAAG,IAAI;AAClB,IAAA,IAAI,WAAqC;AACzC,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAsB;IAE/C,MAAM,OAAO,GAAG,MAAK;QACnB,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;QAC5B,OAA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AAA,CAAC;IAED,OAAO;QACL,GAAG;QACH,SAAS;AACT,QAAA,OAAO,EAAE;YACP,KAAK;YACL,OAAO;AACR,SAAA;KACF;AACH;;;;"}
1
+ {"version":3,"file":"derive.js","sources":["../../../src/lib/derive.ts"],"sourcesContent":["import { DerivedStore, Listener, Store } from \"../types/types\";\nimport { deeplyEquals } from \"./deeplyEquals\";\n\n/**\n * Creates a derived store from an existing store.\n *\n * A derived store computes its state based on the original store's state using a derive function.\n * The derived store's value is memoized and only recomputed when requested.\n *\n * @param store - The original store to derive from\n * @param deriveFn - A function that computes the derived state from the original state\n * @param dependencyFn - Optional function that returns an array of dependencies. If provided, the derived state will only be recomputed when these dependencies change\n *\n * @returns A new store containing the derived state\n *\n * @see {@link https://dharma.fransek.dev/core/derive/}\n */\nexport const derive = <TState, TActions, TDerived>(\n store: Store<TState, TActions>,\n deriveFn: (state: TState) => TDerived,\n dependencyFn?: (state: TState) => unknown[],\n): DerivedStore<TDerived> => {\n let memo: TDerived | undefined;\n let prev: unknown[] | undefined;\n let isStale = true;\n let unsubscribe: (() => void) | undefined;\n const listeners = new Set<Listener<TDerived>>();\n\n const compute = () => {\n memo = deriveFn(store.get());\n isStale = false;\n return memo;\n };\n\n const get = () => {\n const next = dependencyFn?.(store.get());\n isStale &&= !prev || !deeplyEquals(prev, next);\n\n if (isStale) {\n prev = next;\n return compute();\n }\n\n return memo as TDerived;\n };\n\n const subscribe = (listener: Listener<TDerived>) => {\n if (listeners.size === 0) {\n mount();\n }\n\n listener(get());\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n unmount();\n }\n };\n };\n\n const listener = () => {\n isStale = true;\n listeners.forEach((listener) => listener(get()));\n };\n\n const mount = () => {\n if (!unsubscribe) {\n unsubscribe = store.subscribe(listener);\n }\n return unmount;\n };\n\n const unmount = () => {\n unsubscribe?.();\n unsubscribe = undefined;\n };\n\n return {\n get,\n subscribe,\n actions: {\n mount,\n unmount,\n },\n };\n};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C,QAAQ,KAAK,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC;AAClD,IAAA,CAAC;IAED,MAAM,KAAK,GAAG,MAAK;QACjB,IAAI,CAAC,WAAW,EAAE;AAChB,YAAA,WAAW,GAAG,KAAK,CAAC,SAAS,CAAC,QAAQ,CAAC;QACzC;AACA,QAAA,OAAO,OAAO;AAChB,IAAA,CAAC;IAED,MAAM,OAAO,GAAG,MAAK;QACnB,WAAW,IAAI;QACf,WAAW,GAAG,SAAS;AACzB,IAAA,CAAC;IAED,OAAO;QACL,GAAG;QACH,SAAS;AACT,QAAA,OAAO,EAAE;YACP,KAAK;YACL,OAAO;AACR,SAAA;KACF;AACH;;;;"}
@@ -10,11 +10,17 @@ export type Store<TState, TActions> = {
10
10
  };
11
11
  export type DerivedStoreActions = {
12
12
  /** Start listening to changes in the original store. Called automatically on the first subscription. */
13
- mount: () => void;
13
+ mount: () => () => void;
14
14
  /** Stop listening to changes in the original store. Called automatically when there are no more subscribers. */
15
15
  unmount: () => void;
16
16
  };
17
17
  export type DerivedStore<TState> = Store<TState, DerivedStoreActions>;
18
+ export type Effect = {
19
+ /** Start listening to changes in the original store. */
20
+ mount: () => () => void;
21
+ /** Stop listening to changes in the original store. */
22
+ unmount: () => void;
23
+ };
18
24
  export type StoreEventContext<TState> = {
19
25
  /** The current state of the store. */
20
26
  state: TState;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "dharma-core",
3
- "version": "0.9.1",
3
+ "version": "0.10.1",
4
4
  "author": "Frans Ekman <fransedvinekman@gmail.com>",
5
5
  "license": "MIT",
6
- "description": "My package description",
6
+ "description": "A simple and lightweight state management solution for JavaScript and TypeScript applications.",
7
7
  "keywords": [],
8
8
  "homepage": "https://github.com/fransek/dharma/tree/main/packages/dharma-core#readme",
9
9
  "repository": {