dharma-core 0.2.1 → 0.3.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.
|
@@ -6,9 +6,9 @@ export type Store<TState extends object, TActions extends object> = {
|
|
|
6
6
|
/** Actions that can modify the state of the store. */
|
|
7
7
|
actions: TActions;
|
|
8
8
|
/** Subscribes to changes in the state of the store. Returns an unsubscribe function. */
|
|
9
|
-
subscribe: (listener: Listener) =>
|
|
9
|
+
subscribe: (listener: Listener<TState>) => () => void;
|
|
10
10
|
};
|
|
11
|
-
type Listener = () => void;
|
|
11
|
+
type Listener<TState extends object> = (state: TState) => void;
|
|
12
12
|
export type StoreEventHandler<TState extends object> = (state: TState, set: SetState<TState>) => void;
|
|
13
13
|
export type SetState<TState extends object> = (stateModifier: StateModifier<TState>) => TState;
|
|
14
14
|
export type StateModifier<TState extends object> = Partial<TState> | ((state: TState) => Partial<TState>);
|
|
@@ -33,7 +33,7 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
|
|
|
33
33
|
};
|
|
34
34
|
const dispatch = () => {
|
|
35
35
|
onChange?.(state, setSilently);
|
|
36
|
-
listeners.forEach((listener) => listener());
|
|
36
|
+
listeners.forEach((listener) => listener(state));
|
|
37
37
|
};
|
|
38
38
|
const set = (stateModifier) => {
|
|
39
39
|
setSilently(stateModifier);
|
|
@@ -41,6 +41,7 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
|
|
|
41
41
|
return state;
|
|
42
42
|
};
|
|
43
43
|
const subscribe = (listener) => {
|
|
44
|
+
listener(state);
|
|
44
45
|
if (listeners.size === 0) {
|
|
45
46
|
onAttach?.(state, set);
|
|
46
47
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { merge } from \"./merge\";\n\nexport type Store<TState extends object, TActions extends object> = {\n /** Returns the current state of the store. */\n get: () => TState;\n /** Sets the state of the store. */\n set: (stateModifier: StateModifier<TState>) => TState;\n /** Actions that can modify the state of the store. */\n actions: TActions;\n /** Subscribes to changes in the state of the store. Returns an unsubscribe function. */\n subscribe: (listener: Listener) =>
|
|
1
|
+
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { merge } from \"./merge\";\n\nexport type Store<TState extends object, TActions extends object> = {\n /** Returns the current state of the store. */\n get: () => TState;\n /** Sets the state of the store. */\n set: (stateModifier: StateModifier<TState>) => TState;\n /** Actions that can modify the state of the store. */\n actions: TActions;\n /** Subscribes to changes in the state of the store. Returns an unsubscribe function. */\n subscribe: (listener: Listener<TState>) => () => void;\n};\n\ntype Listener<TState extends object> = (state: TState) => void;\n\nexport type StoreEventHandler<TState extends object> = (\n state: TState,\n set: SetState<TState>,\n) => void;\n\nexport type SetState<TState extends object> = (\n stateModifier: StateModifier<TState>,\n) => TState;\n\nexport type StateModifier<TState extends object> =\n | Partial<TState>\n | ((state: TState) => Partial<TState>);\n\nexport type DefineActions<TState extends object, TActions> = (\n set: (stateModifier: StateModifier<TState>) => TState,\n get: () => TState,\n) => TActions;\n\nexport type StoreOptions<TState extends object> = {\n /** Invoked when the store is created. */\n onLoad?: StoreEventHandler<TState>;\n /** Invoked when the store is subscribed to. */\n onAttach?: StoreEventHandler<TState>;\n /** Invoked when the store is unsubscribed from. */\n onDetach?: StoreEventHandler<TState>;\n /** Invoked whenever the state changes. */\n onChange?: StoreEventHandler<TState>;\n};\n\n/**\n * Creates a store with an initial state and actions that can modify the state.\n *\n * @param {TState} initialState - The initial state of the store.\n * @param {DefineActions<TState, TActions>} [defineActions] - A function that defines actions that can modify the state.\n * @param {StoreOptions<TState>} [options] - Additional options for the store.\n *\n * @returns {Store<TState, TActions>} The created store with state management methods.\n *\n * @example\n * ```ts\n * import { createStore } from \"dharma-core\";\n *\n * const store = createStore({ count: 0 }, (set) => ({\n * increment: () => set((state) => ({ count: state.count + 1 })),\n * decrement: () => set((state) => ({ count: state.count - 1 })),\n * reset: () => set({ count: 0 }),\n * }));\n * ```\n * @group Core\n */\nexport const createStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n initialState: TState,\n defineActions: DefineActions<TState, TActions>,\n { onLoad, onAttach, onDetach, onChange }: StoreOptions<TState> = {},\n): Store<TState, TActions> => {\n let state = initialState;\n const listeners = new Set<Listener<TState>>();\n\n const get = () => state;\n\n const setSilently = (stateModifier: StateModifier<TState>) => {\n state = merge(state, stateModifier);\n return state;\n };\n\n const dispatch = () => {\n onChange?.(state, setSilently);\n listeners.forEach((listener) => listener(state));\n };\n\n const set = (stateModifier: StateModifier<TState>) => {\n setSilently(stateModifier);\n dispatch();\n return state;\n };\n\n const subscribe = (listener: Listener<TState>) => {\n listener(state);\n if (listeners.size === 0) {\n onAttach?.(state, set);\n }\n\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n onDetach?.(state, set);\n }\n };\n };\n\n const actions = defineActions ? defineActions(set, get) : ({} as TActions);\n\n onLoad?.(state, set);\n\n return {\n get,\n set,\n subscribe,\n actions,\n };\n};\n"],"names":["merge"],"mappings":";;;;AA4CA;;;;;;;;;;;;;;;;;;;;AAoBG;MACU,WAAW,GAAG,CAIzB,YAAoB,EACpB,aAA8C,EAC9C,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAA,GAA2B,EAAE,KACxC;IAC3B,IAAI,KAAK,GAAG,YAAY;AACxB,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAoB;AAE7C,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK;AAEvB,IAAA,MAAM,WAAW,GAAG,CAAC,aAAoC,KAAI;AAC3D,QAAA,KAAK,GAAGA,WAAK,CAAC,KAAK,EAAE,aAAa,CAAC;AACnC,QAAA,OAAO,KAAK;AACd,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;AACpB,QAAA,QAAQ,GAAG,KAAK,EAAE,WAAW,CAAC;AAC9B,QAAA,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClD,IAAA,CAAC;AAED,IAAA,MAAM,GAAG,GAAG,CAAC,aAAoC,KAAI;QACnD,WAAW,CAAC,aAAa,CAAC;AAC1B,QAAA,QAAQ,EAAE;AACV,QAAA,OAAO,KAAK;AACd,IAAA,CAAC;AAED,IAAA,MAAM,SAAS,GAAG,CAAC,QAA0B,KAAI;QAC/C,QAAQ,CAAC,KAAK,CAAC;AACf,QAAA,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;AACxB,YAAA,QAAQ,GAAG,KAAK,EAAE,GAAG,CAAC;QACxB;AAEA,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,QAAQ,GAAG,KAAK,EAAE,GAAG,CAAC;YACxB;AACF,QAAA,CAAC;AACH,IAAA,CAAC;AAED,IAAA,MAAM,OAAO,GAAG,aAAa,GAAG,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,GAAI,EAAe;AAE1E,IAAA,MAAM,GAAG,KAAK,EAAE,GAAG,CAAC;IAEpB,OAAO;QACL,GAAG;QACH,GAAG;QACH,SAAS;QACT,OAAO;KACR;AACH;;;;"}
|
|
@@ -6,9 +6,9 @@ export type Store<TState extends object, TActions extends object> = {
|
|
|
6
6
|
/** Actions that can modify the state of the store. */
|
|
7
7
|
actions: TActions;
|
|
8
8
|
/** Subscribes to changes in the state of the store. Returns an unsubscribe function. */
|
|
9
|
-
subscribe: (listener: Listener) =>
|
|
9
|
+
subscribe: (listener: Listener<TState>) => () => void;
|
|
10
10
|
};
|
|
11
|
-
type Listener = () => void;
|
|
11
|
+
type Listener<TState extends object> = (state: TState) => void;
|
|
12
12
|
export type StoreEventHandler<TState extends object> = (state: TState, set: SetState<TState>) => void;
|
|
13
13
|
export type SetState<TState extends object> = (stateModifier: StateModifier<TState>) => TState;
|
|
14
14
|
export type StateModifier<TState extends object> = Partial<TState> | ((state: TState) => Partial<TState>);
|
|
@@ -31,7 +31,7 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
|
|
|
31
31
|
};
|
|
32
32
|
const dispatch = () => {
|
|
33
33
|
onChange?.(state, setSilently);
|
|
34
|
-
listeners.forEach((listener) => listener());
|
|
34
|
+
listeners.forEach((listener) => listener(state));
|
|
35
35
|
};
|
|
36
36
|
const set = (stateModifier) => {
|
|
37
37
|
setSilently(stateModifier);
|
|
@@ -39,6 +39,7 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
|
|
|
39
39
|
return state;
|
|
40
40
|
};
|
|
41
41
|
const subscribe = (listener) => {
|
|
42
|
+
listener(state);
|
|
42
43
|
if (listeners.size === 0) {
|
|
43
44
|
onAttach?.(state, set);
|
|
44
45
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { merge } from \"./merge\";\n\nexport type Store<TState extends object, TActions extends object> = {\n /** Returns the current state of the store. */\n get: () => TState;\n /** Sets the state of the store. */\n set: (stateModifier: StateModifier<TState>) => TState;\n /** Actions that can modify the state of the store. */\n actions: TActions;\n /** Subscribes to changes in the state of the store. Returns an unsubscribe function. */\n subscribe: (listener: Listener) =>
|
|
1
|
+
{"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { merge } from \"./merge\";\n\nexport type Store<TState extends object, TActions extends object> = {\n /** Returns the current state of the store. */\n get: () => TState;\n /** Sets the state of the store. */\n set: (stateModifier: StateModifier<TState>) => TState;\n /** Actions that can modify the state of the store. */\n actions: TActions;\n /** Subscribes to changes in the state of the store. Returns an unsubscribe function. */\n subscribe: (listener: Listener<TState>) => () => void;\n};\n\ntype Listener<TState extends object> = (state: TState) => void;\n\nexport type StoreEventHandler<TState extends object> = (\n state: TState,\n set: SetState<TState>,\n) => void;\n\nexport type SetState<TState extends object> = (\n stateModifier: StateModifier<TState>,\n) => TState;\n\nexport type StateModifier<TState extends object> =\n | Partial<TState>\n | ((state: TState) => Partial<TState>);\n\nexport type DefineActions<TState extends object, TActions> = (\n set: (stateModifier: StateModifier<TState>) => TState,\n get: () => TState,\n) => TActions;\n\nexport type StoreOptions<TState extends object> = {\n /** Invoked when the store is created. */\n onLoad?: StoreEventHandler<TState>;\n /** Invoked when the store is subscribed to. */\n onAttach?: StoreEventHandler<TState>;\n /** Invoked when the store is unsubscribed from. */\n onDetach?: StoreEventHandler<TState>;\n /** Invoked whenever the state changes. */\n onChange?: StoreEventHandler<TState>;\n};\n\n/**\n * Creates a store with an initial state and actions that can modify the state.\n *\n * @param {TState} initialState - The initial state of the store.\n * @param {DefineActions<TState, TActions>} [defineActions] - A function that defines actions that can modify the state.\n * @param {StoreOptions<TState>} [options] - Additional options for the store.\n *\n * @returns {Store<TState, TActions>} The created store with state management methods.\n *\n * @example\n * ```ts\n * import { createStore } from \"dharma-core\";\n *\n * const store = createStore({ count: 0 }, (set) => ({\n * increment: () => set((state) => ({ count: state.count + 1 })),\n * decrement: () => set((state) => ({ count: state.count - 1 })),\n * reset: () => set({ count: 0 }),\n * }));\n * ```\n * @group Core\n */\nexport const createStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n initialState: TState,\n defineActions: DefineActions<TState, TActions>,\n { onLoad, onAttach, onDetach, onChange }: StoreOptions<TState> = {},\n): Store<TState, TActions> => {\n let state = initialState;\n const listeners = new Set<Listener<TState>>();\n\n const get = () => state;\n\n const setSilently = (stateModifier: StateModifier<TState>) => {\n state = merge(state, stateModifier);\n return state;\n };\n\n const dispatch = () => {\n onChange?.(state, setSilently);\n listeners.forEach((listener) => listener(state));\n };\n\n const set = (stateModifier: StateModifier<TState>) => {\n setSilently(stateModifier);\n dispatch();\n return state;\n };\n\n const subscribe = (listener: Listener<TState>) => {\n listener(state);\n if (listeners.size === 0) {\n onAttach?.(state, set);\n }\n\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n\n if (listeners.size === 0) {\n onDetach?.(state, set);\n }\n };\n };\n\n const actions = defineActions ? defineActions(set, get) : ({} as TActions);\n\n onLoad?.(state, set);\n\n return {\n get,\n set,\n subscribe,\n actions,\n };\n};\n"],"names":[],"mappings":";;AA4CA;;;;;;;;;;;;;;;;;;;;AAoBG;MACU,WAAW,GAAG,CAIzB,YAAoB,EACpB,aAA8C,EAC9C,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAA,GAA2B,EAAE,KACxC;IAC3B,IAAI,KAAK,GAAG,YAAY;AACxB,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAoB;AAE7C,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK;AAEvB,IAAA,MAAM,WAAW,GAAG,CAAC,aAAoC,KAAI;AAC3D,QAAA,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,aAAa,CAAC;AACnC,QAAA,OAAO,KAAK;AACd,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;AACpB,QAAA,QAAQ,GAAG,KAAK,EAAE,WAAW,CAAC;AAC9B,QAAA,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClD,IAAA,CAAC;AAED,IAAA,MAAM,GAAG,GAAG,CAAC,aAAoC,KAAI;QACnD,WAAW,CAAC,aAAa,CAAC;AAC1B,QAAA,QAAQ,EAAE;AACV,QAAA,OAAO,KAAK;AACd,IAAA,CAAC;AAED,IAAA,MAAM,SAAS,GAAG,CAAC,QAA0B,KAAI;QAC/C,QAAQ,CAAC,KAAK,CAAC;AACf,QAAA,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;AACxB,YAAA,QAAQ,GAAG,KAAK,EAAE,GAAG,CAAC;QACxB;AAEA,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,QAAQ,GAAG,KAAK,EAAE,GAAG,CAAC;YACxB;AACF,QAAA,CAAC;AACH,IAAA,CAAC;AAED,IAAA,MAAM,OAAO,GAAG,aAAa,GAAG,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,GAAI,EAAe;AAE1E,IAAA,MAAM,GAAG,KAAK,EAAE,GAAG,CAAC;IAEpB,OAAO;QACL,GAAG;QACH,GAAG;QACH,SAAS;QACT,OAAO;KACR;AACH;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dharma-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Frans Ekman <fransedvinekman@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "My package description",
|
|
@@ -18,10 +18,27 @@
|
|
|
18
18
|
],
|
|
19
19
|
"main": "dist/cjs/index.js",
|
|
20
20
|
"module": "dist/esm/index.js",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
21
22
|
"exports": {
|
|
22
23
|
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
23
25
|
"import": "./dist/esm/index.js",
|
|
24
26
|
"require": "./dist/cjs/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./createStore": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/esm/lib/createStore.js",
|
|
31
|
+
"require": "./dist/cjs/lib/createStore.js"
|
|
32
|
+
},
|
|
33
|
+
"./createPersistentStore": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/esm/lib/createPersistentStore.js",
|
|
36
|
+
"require": "./dist/cjs/lib/createPersistentStore.js"
|
|
37
|
+
},
|
|
38
|
+
"./merge": {
|
|
39
|
+
"types": "./dist/index.d.ts",
|
|
40
|
+
"import": "./dist/esm/lib/merge.js",
|
|
41
|
+
"require": "./dist/cjs/lib/merge.js"
|
|
25
42
|
}
|
|
26
43
|
},
|
|
27
44
|
"sideEffects": false,
|
|
@@ -29,7 +46,7 @@
|
|
|
29
46
|
"provenance": true
|
|
30
47
|
},
|
|
31
48
|
"scripts": {
|
|
32
|
-
"build": "rollup -c",
|
|
49
|
+
"build": "rm -rf dist && rollup -c",
|
|
33
50
|
"dev": "rollup -c -w",
|
|
34
51
|
"test": "vitest --run",
|
|
35
52
|
"test:watch": "vitest",
|