dharma-core 0.10.2 → 0.11.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/README.md +1 -1
- package/dist/cjs/lib/createStore.js +6 -6
- package/dist/cjs/lib/createStore.js.map +1 -1
- package/dist/cjs/types/types.d.ts +2 -2
- package/dist/esm/lib/createStore.js +6 -6
- package/dist/esm/lib/createStore.js.map +1 -1
- package/dist/esm/types/types.d.ts +2 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ import { createStore } from "dharma-core";
|
|
|
13
13
|
|
|
14
14
|
const store = createStore({
|
|
15
15
|
initialState: { count: 0 },
|
|
16
|
-
|
|
16
|
+
actions: ({ set }) => ({
|
|
17
17
|
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
18
18
|
decrement: () => set((state) => ({ count: state.count - 1 })),
|
|
19
19
|
}),
|
|
@@ -13,7 +13,7 @@ var merge = require('./merge.js');
|
|
|
13
13
|
* @see {@link https://dharma.fransek.dev/core/createstore/}
|
|
14
14
|
*/
|
|
15
15
|
const createStore = (config) => {
|
|
16
|
-
const { initialState,
|
|
16
|
+
const { initialState, onLoad, onAttach, onDetach, onChange } = config;
|
|
17
17
|
let state = initialState;
|
|
18
18
|
const get = () => state;
|
|
19
19
|
const setSilently = (stateModifier) => {
|
|
@@ -32,34 +32,34 @@ const createStore = (config) => {
|
|
|
32
32
|
return state;
|
|
33
33
|
};
|
|
34
34
|
const listeners = new Set();
|
|
35
|
-
const actions =
|
|
35
|
+
const actions = config.actions?.({ set, get, reset });
|
|
36
36
|
const storageAdapter = createStorageAdapter.createStorageAdapter(config, get, set);
|
|
37
37
|
const subscribe = (listener) => {
|
|
38
38
|
if (listeners.size === 0) {
|
|
39
|
-
onAttach?.({ state, set, reset });
|
|
40
39
|
storageAdapter?.onAttach();
|
|
40
|
+
onAttach?.({ state, set, reset });
|
|
41
41
|
}
|
|
42
42
|
listener(state);
|
|
43
43
|
listeners.add(listener);
|
|
44
44
|
return () => {
|
|
45
45
|
listeners.delete(listener);
|
|
46
46
|
if (listeners.size === 0) {
|
|
47
|
-
onDetach?.({ state, set, reset });
|
|
48
47
|
storageAdapter?.onDetach();
|
|
48
|
+
onDetach?.({ state, set, reset });
|
|
49
49
|
}
|
|
50
50
|
};
|
|
51
51
|
};
|
|
52
52
|
const dispatch = () => {
|
|
53
|
+
storageAdapter?.onChange(state);
|
|
53
54
|
onChange?.({
|
|
54
55
|
state,
|
|
55
56
|
set: setSilently,
|
|
56
57
|
reset: resetSilently,
|
|
57
58
|
});
|
|
58
|
-
storageAdapter?.onChange(state);
|
|
59
59
|
listeners.forEach((listener) => listener(state));
|
|
60
60
|
};
|
|
61
|
-
onLoad?.({ state, set, reset });
|
|
62
61
|
storageAdapter?.onLoad();
|
|
62
|
+
onLoad?.({ state, set, reset });
|
|
63
63
|
return {
|
|
64
64
|
get,
|
|
65
65
|
subscribe,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport { Listener, StateModifier, Store, StoreConfig } from \"../types/types\";\nimport { merge } from \"./merge\";\n\n/**\n * Creates a store with an initial state and actions that can modify the state.\n *\n * @param {StoreConfig<TState, TActions>} [config] - The configuration for the store.\n *\n * @returns {Store<TState, TActions>} The created store with state management methods.\n *\n * @see {@link https://dharma.fransek.dev/core/createstore/}\n */\nexport const createStore = <TState, TActions = undefined>(\n config: StoreConfig<TState, TActions>,\n): Store<TState, TActions> => {\n const { initialState,
|
|
1
|
+
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport { Listener, StateModifier, Store, StoreConfig } from \"../types/types\";\nimport { merge } from \"./merge\";\n\n/**\n * Creates a store with an initial state and actions that can modify the state.\n *\n * @param {StoreConfig<TState, TActions>} [config] - The configuration for the store.\n *\n * @returns {Store<TState, TActions>} The created store with state management methods.\n *\n * @see {@link https://dharma.fransek.dev/core/createstore/}\n */\nexport const createStore = <TState, TActions = undefined>(\n config: StoreConfig<TState, TActions>,\n): Store<TState, TActions> => {\n const { initialState, onLoad, onAttach, onDetach, onChange } = config;\n\n let state = initialState;\n\n const get = () => state;\n\n const setSilently = (stateModifier: StateModifier<TState>) => {\n state = merge(state, stateModifier);\n return state;\n };\n\n const set = (stateModifier: StateModifier<TState>) => {\n setSilently(stateModifier);\n dispatch();\n return state;\n };\n\n const resetSilently = () => setSilently(initialState);\n\n const reset = () => {\n resetSilently();\n dispatch();\n return state;\n };\n\n const listeners = new Set<Listener<TState>>();\n const actions = config.actions?.({ set, get, reset }) as TActions;\n const storageAdapter = createStorageAdapter(config, get, set);\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\n storageAdapter?.onAttach();\n onAttach?.({ state, set, reset });\n }\n\n listener(state);\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n storageAdapter?.onDetach();\n onDetach?.({ state, set, reset });\n }\n };\n };\n\n const dispatch = () => {\n storageAdapter?.onChange(state);\n onChange?.({\n state,\n set: setSilently,\n reset: resetSilently,\n });\n listeners.forEach((listener) => listener(state));\n };\n\n storageAdapter?.onLoad();\n onLoad?.({ state, set, reset });\n\n return {\n get,\n subscribe,\n actions,\n };\n};\n"],"names":["merge","createStorageAdapter"],"mappings":";;;;;AAIA;;;;;;;;AAQG;AACI,MAAM,WAAW,GAAG,CACzB,MAAqC,KACV;AAC3B,IAAA,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,MAAM;IAErE,IAAI,KAAK,GAAG,YAAY;AAExB,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK;AAEvB,IAAA,MAAM,WAAW,GAAG,CAAC,aAAoC,KAAI;AAC3D,QAAA,KAAK,GAAGA,W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E,QAAQ,CAAC,KAAK,CAAC;AAC/B,QAAA,QAAQ,GAAG;YACT,KAAK;AACL,YAAA,GAAG,EAAE,WAAW;AAChB,YAAA,KAAK,EAAE,aAAa;AACrB,SAAA,CAAC;AACF,QAAA,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClD,IAAA,CAAC;IAED,cAAc,EAAE,MAAM,EAAE;IACxB,MAAM,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IAE/B,OAAO;QACL,GAAG;QACH,SAAS;QACT,OAAO;KACR;AACH;;;;"}
|
|
@@ -39,12 +39,12 @@ export type ActionContext<TState> = {
|
|
|
39
39
|
/** Resets the state of the store to its initial value. */
|
|
40
40
|
reset: () => TState;
|
|
41
41
|
};
|
|
42
|
-
export type
|
|
42
|
+
export type StoreActions<TState, TActions> = (context: ActionContext<TState>) => TActions;
|
|
43
43
|
export type BaseConfig<TState, TActions> = {
|
|
44
44
|
/** The initial state of the store. */
|
|
45
45
|
initialState: TState;
|
|
46
46
|
/** A function that defines actions that can modify the state. */
|
|
47
|
-
|
|
47
|
+
actions?: StoreActions<TState, TActions>;
|
|
48
48
|
/** Invoked when the store is created. */
|
|
49
49
|
onLoad?: StoreEventListener<TState>;
|
|
50
50
|
/** Invoked when the store is subscribed to. */
|
|
@@ -11,7 +11,7 @@ import { merge } from './merge.js';
|
|
|
11
11
|
* @see {@link https://dharma.fransek.dev/core/createstore/}
|
|
12
12
|
*/
|
|
13
13
|
const createStore = (config) => {
|
|
14
|
-
const { initialState,
|
|
14
|
+
const { initialState, onLoad, onAttach, onDetach, onChange } = config;
|
|
15
15
|
let state = initialState;
|
|
16
16
|
const get = () => state;
|
|
17
17
|
const setSilently = (stateModifier) => {
|
|
@@ -30,34 +30,34 @@ const createStore = (config) => {
|
|
|
30
30
|
return state;
|
|
31
31
|
};
|
|
32
32
|
const listeners = new Set();
|
|
33
|
-
const actions =
|
|
33
|
+
const actions = config.actions?.({ set, get, reset });
|
|
34
34
|
const storageAdapter = createStorageAdapter(config, get, set);
|
|
35
35
|
const subscribe = (listener) => {
|
|
36
36
|
if (listeners.size === 0) {
|
|
37
|
-
onAttach?.({ state, set, reset });
|
|
38
37
|
storageAdapter?.onAttach();
|
|
38
|
+
onAttach?.({ state, set, reset });
|
|
39
39
|
}
|
|
40
40
|
listener(state);
|
|
41
41
|
listeners.add(listener);
|
|
42
42
|
return () => {
|
|
43
43
|
listeners.delete(listener);
|
|
44
44
|
if (listeners.size === 0) {
|
|
45
|
-
onDetach?.({ state, set, reset });
|
|
46
45
|
storageAdapter?.onDetach();
|
|
46
|
+
onDetach?.({ state, set, reset });
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
};
|
|
50
50
|
const dispatch = () => {
|
|
51
|
+
storageAdapter?.onChange(state);
|
|
51
52
|
onChange?.({
|
|
52
53
|
state,
|
|
53
54
|
set: setSilently,
|
|
54
55
|
reset: resetSilently,
|
|
55
56
|
});
|
|
56
|
-
storageAdapter?.onChange(state);
|
|
57
57
|
listeners.forEach((listener) => listener(state));
|
|
58
58
|
};
|
|
59
|
-
onLoad?.({ state, set, reset });
|
|
60
59
|
storageAdapter?.onLoad();
|
|
60
|
+
onLoad?.({ state, set, reset });
|
|
61
61
|
return {
|
|
62
62
|
get,
|
|
63
63
|
subscribe,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport { Listener, StateModifier, Store, StoreConfig } from \"../types/types\";\nimport { merge } from \"./merge\";\n\n/**\n * Creates a store with an initial state and actions that can modify the state.\n *\n * @param {StoreConfig<TState, TActions>} [config] - The configuration for the store.\n *\n * @returns {Store<TState, TActions>} The created store with state management methods.\n *\n * @see {@link https://dharma.fransek.dev/core/createstore/}\n */\nexport const createStore = <TState, TActions = undefined>(\n config: StoreConfig<TState, TActions>,\n): Store<TState, TActions> => {\n const { initialState,
|
|
1
|
+
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport { Listener, StateModifier, Store, StoreConfig } from \"../types/types\";\nimport { merge } from \"./merge\";\n\n/**\n * Creates a store with an initial state and actions that can modify the state.\n *\n * @param {StoreConfig<TState, TActions>} [config] - The configuration for the store.\n *\n * @returns {Store<TState, TActions>} The created store with state management methods.\n *\n * @see {@link https://dharma.fransek.dev/core/createstore/}\n */\nexport const createStore = <TState, TActions = undefined>(\n config: StoreConfig<TState, TActions>,\n): Store<TState, TActions> => {\n const { initialState, onLoad, onAttach, onDetach, onChange } = config;\n\n let state = initialState;\n\n const get = () => state;\n\n const setSilently = (stateModifier: StateModifier<TState>) => {\n state = merge(state, stateModifier);\n return state;\n };\n\n const set = (stateModifier: StateModifier<TState>) => {\n setSilently(stateModifier);\n dispatch();\n return state;\n };\n\n const resetSilently = () => setSilently(initialState);\n\n const reset = () => {\n resetSilently();\n dispatch();\n return state;\n };\n\n const listeners = new Set<Listener<TState>>();\n const actions = config.actions?.({ set, get, reset }) as TActions;\n const storageAdapter = createStorageAdapter(config, get, set);\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\n storageAdapter?.onAttach();\n onAttach?.({ state, set, reset });\n }\n\n listener(state);\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n storageAdapter?.onDetach();\n onDetach?.({ state, set, reset });\n }\n };\n };\n\n const dispatch = () => {\n storageAdapter?.onChange(state);\n onChange?.({\n state,\n set: setSilently,\n reset: resetSilently,\n });\n listeners.forEach((listener) => listener(state));\n };\n\n storageAdapter?.onLoad();\n onLoad?.({ state, set, reset });\n\n return {\n get,\n subscribe,\n actions,\n };\n};\n"],"names":[],"mappings":";;;AAIA;;;;;;;;AAQG;AACI,MAAM,WAAW,GAAG,CACzB,MAAqC,KACV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;AAE1B,YAAA,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;gBACxB,cAAc,EAAE,QAAQ,EAAE;gBAC1B,QAAQ,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;YACnC;AACF,QAAA,CAAC;AACH,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;AACpB,QAAA,cAAc,EAAE,QAAQ,CAAC,KAAK,CAAC;AAC/B,QAAA,QAAQ,GAAG;YACT,KAAK;AACL,YAAA,GAAG,EAAE,WAAW;AAChB,YAAA,KAAK,EAAE,aAAa;AACrB,SAAA,CAAC;AACF,QAAA,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClD,IAAA,CAAC;IAED,cAAc,EAAE,MAAM,EAAE;IACxB,MAAM,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IAE/B,OAAO;QACL,GAAG;QACH,SAAS;QACT,OAAO;KACR;AACH;;;;"}
|
|
@@ -39,12 +39,12 @@ export type ActionContext<TState> = {
|
|
|
39
39
|
/** Resets the state of the store to its initial value. */
|
|
40
40
|
reset: () => TState;
|
|
41
41
|
};
|
|
42
|
-
export type
|
|
42
|
+
export type StoreActions<TState, TActions> = (context: ActionContext<TState>) => TActions;
|
|
43
43
|
export type BaseConfig<TState, TActions> = {
|
|
44
44
|
/** The initial state of the store. */
|
|
45
45
|
initialState: TState;
|
|
46
46
|
/** A function that defines actions that can modify the state. */
|
|
47
|
-
|
|
47
|
+
actions?: StoreActions<TState, TActions>;
|
|
48
48
|
/** Invoked when the store is created. */
|
|
49
49
|
onLoad?: StoreEventListener<TState>;
|
|
50
50
|
/** Invoked when the store is subscribed to. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dharma-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"author": "Frans Ekman <fransedvinekman@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A simple and lightweight state management solution for JavaScript and TypeScript applications.",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"homepage": "https://github.com/fransek/dharma/tree/main/packages/dharma-core#readme",
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
|
-
"url": "git+https://github.com/fransek/dharma
|
|
16
|
+
"url": "git+https://github.com/fransek/dharma.git",
|
|
17
|
+
"directory": "packages/dharma-core"
|
|
17
18
|
},
|
|
18
19
|
"bugs": {
|
|
19
20
|
"url": "https://github.com/fransek/dharma/issues"
|