dharma-core 0.15.0 → 0.16.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.
@@ -28,39 +28,32 @@ const createStore = (config) => {
28
28
  };
29
29
  const reset = () => set(initialState);
30
30
  const listeners = new Set();
31
- const actions = config.actions?.({
32
- set,
31
+ const ctx = {
33
32
  get,
34
- reset,
35
- initialState,
36
- });
37
- // TODO: Replace with a static object once `state` is removed.
38
- const getContext = () => ({
39
- get,
40
- state,
41
33
  set,
42
34
  reset,
43
35
  initialState,
44
- });
36
+ };
37
+ const actions = config.actions?.(ctx);
45
38
  const storageAdapter = createStorageAdapter.createStorageAdapter(config, get, set);
46
39
  const subscribe = (listener) => {
47
40
  if (listeners.size === 0) {
48
41
  storageAdapter?.onAttach();
49
- onAttach?.(getContext());
42
+ onAttach?.(ctx);
50
43
  }
51
44
  listener(state);
52
45
  listeners.add(listener);
53
46
  return () => {
54
47
  if (listeners.delete(listener) && listeners.size === 0) {
55
48
  storageAdapter?.onDetach();
56
- onDetach?.(getContext());
49
+ onDetach?.(ctx);
57
50
  }
58
51
  };
59
52
  };
60
53
  const dispatch = () => {
61
54
  isSilent = true;
62
55
  try {
63
- onChange?.(getContext());
56
+ onChange?.(ctx);
64
57
  }
65
58
  finally {
66
59
  isSilent = false;
@@ -69,7 +62,7 @@ const createStore = (config) => {
69
62
  listeners.forEach((listener) => listener(state));
70
63
  };
71
64
  storageAdapter?.onLoad();
72
- onLoad?.(getContext());
65
+ onLoad?.(ctx);
73
66
  return {
74
67
  get,
75
68
  subscribe,
@@ -1 +1 @@
1
- {"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport {\n Listener,\n StateModifier,\n Store,\n StoreConfig,\n StoreEventContext,\n} 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 // Set while onChange runs, so its synchronous sets join the current dispatch\n let isSilent = false;\n\n const get = () => state;\n\n const set = (stateModifier: StateModifier<TState>) => {\n const prevState = state;\n state = merge(state, stateModifier);\n\n if (!Object.is(state, prevState) && !isSilent) {\n dispatch();\n }\n\n return state;\n };\n\n const reset = () => set(initialState);\n\n const listeners = new Set<Listener<TState>>();\n\n const actions = config.actions?.({\n set,\n get,\n reset,\n initialState,\n }) as TActions;\n\n // TODO: Replace with a static object once `state` is removed.\n const getContext = (): StoreEventContext<TState> => ({\n get,\n state,\n set,\n reset,\n initialState,\n });\n\n const storageAdapter = createStorageAdapter(config, get, set);\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\n storageAdapter?.onAttach();\n onAttach?.(getContext());\n }\n\n listener(state);\n listeners.add(listener);\n\n return () => {\n if (listeners.delete(listener) && listeners.size === 0) {\n storageAdapter?.onDetach();\n onDetach?.(getContext());\n }\n };\n };\n\n const dispatch = () => {\n isSilent = true;\n try {\n onChange?.(getContext());\n } finally {\n isSilent = false;\n }\n storageAdapter?.onChange(state);\n listeners.forEach((listener) => listener(state));\n };\n\n storageAdapter?.onLoad();\n onLoad?.(getContext());\n\n return {\n get,\n subscribe,\n actions,\n set,\n reset,\n initialState,\n };\n};\n"],"names":["merge","createStorageAdapter"],"mappings":";;;;;AAUA;;;;;;;;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;;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,QAAQ,GAAG,UAAU,EAAE,CAAC;YAC1B;AACF,QAAA,CAAC;AACH,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;QACpB,QAAQ,GAAG,IAAI;AACf,QAAA,IAAI;AACF,YAAA,QAAQ,GAAG,UAAU,EAAE,CAAC;QAC1B;gBAAU;YACR,QAAQ,GAAG,KAAK;QAClB;AACA,QAAA,cAAc,EAAE,QAAQ,CAAC,KAAK,CAAC;AAC/B,QAAA,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClD,IAAA,CAAC;IAED,cAAc,EAAE,MAAM,EAAE;AACxB,IAAA,MAAM,GAAG,UAAU,EAAE,CAAC;IAEtB,OAAO;QACL,GAAG;QACH,SAAS;QACT,OAAO;QACP,GAAG;QACH,KAAK;QACL,YAAY;KACb;AACH;;;;"}
1
+ {"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport {\n Listener,\n StateContext,\n StateModifier,\n Store,\n StoreConfig,\n} 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 // Set while onChange runs, so its synchronous sets join the current dispatch\n let isSilent = false;\n\n const get = () => state;\n\n const set = (stateModifier: StateModifier<TState>) => {\n const prevState = state;\n state = merge(state, stateModifier);\n\n if (!Object.is(state, prevState) && !isSilent) {\n dispatch();\n }\n\n return state;\n };\n\n const reset = () => set(initialState);\n\n const listeners = new Set<Listener<TState>>();\n\n const ctx: StateContext<TState> = {\n get,\n set,\n reset,\n initialState,\n };\n\n const actions = config.actions?.(ctx) as TActions;\n\n const storageAdapter = createStorageAdapter(config, get, set);\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\n storageAdapter?.onAttach();\n onAttach?.(ctx);\n }\n\n listener(state);\n listeners.add(listener);\n\n return () => {\n if (listeners.delete(listener) && listeners.size === 0) {\n storageAdapter?.onDetach();\n onDetach?.(ctx);\n }\n };\n };\n\n const dispatch = () => {\n isSilent = true;\n try {\n onChange?.(ctx);\n } finally {\n isSilent = false;\n }\n storageAdapter?.onChange(state);\n listeners.forEach((listener) => listener(state));\n };\n\n storageAdapter?.onLoad();\n onLoad?.(ctx);\n\n return {\n get,\n subscribe,\n actions,\n set,\n reset,\n initialState,\n };\n};\n"],"names":["merge","createStorageAdapter"],"mappings":";;;;;AAUA;;;;;;;;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;;IAGxB,IAAI,QAAQ,GAAG,KAAK;AAEpB,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK;AAEvB,IAAA,MAAM,GAAG,GAAG,CAAC,aAAoC,KAAI;QACnD,MAAM,SAAS,GAAG,KAAK;AACvB,QAAA,KAAK,GAAGA,WAAK,CAAC,KAAK,EAAE,aAAa,CAAC;AAEnC,QAAA,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE;AAC7C,YAAA,QAAQ,EAAE;QACZ;AAEA,QAAA,OAAO,KAAK;AACd,IAAA,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC;AAErC,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAoB;AAE7C,IAAA,MAAM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
32
  };
33
- export type StoreEventContext<TState> = {
34
- /** Returns the current state of the store. */
35
- get: () => TState;
36
- /**
37
- * A snapshot of the state.
38
- * @deprecated Use `get()` instead.
39
- */
40
- state: TState;
41
- /** The initial state of the store. */
42
- initialState: TState;
43
- /** Sets the state of the store. */
44
- set: SetState<TState>;
45
- /** Resets the state of the store to its initial value. */
46
- reset: () => TState;
47
- };
48
- export type StoreEventListener<TState> = (context: StoreEventContext<TState>) => void;
33
+ export type StoreEventListener<TState> = (context: StateContext<TState>) => void;
49
34
  export type StateModifier<TState> = Partial<TState> | ((state: TState) => Partial<TState>);
50
- export type ActionContext<TState> = {
35
+ export type StateContext<TState> = Readonly<{
51
36
  /** Returns the current state of the store. */
52
37
  get: () => TState;
53
38
  /** Sets the state of the store. */
@@ -56,8 +41,8 @@ export type ActionContext<TState> = {
56
41
  reset: () => TState;
57
42
  /** The initial state of the store. */
58
43
  initialState: TState;
59
- };
60
- export type StoreActions<TState, TActions> = (context: ActionContext<TState>) => TActions;
44
+ }>;
45
+ export type StoreActions<TState, TActions> = (context: StateContext<TState>) => TActions;
61
46
  export type BaseConfig<TState, TActions> = {
62
47
  /** The initial state of the store. */
63
48
  initialState: TState;
@@ -104,3 +89,7 @@ export type PersistConfig = {
104
89
  onStorageError?: (error: unknown) => void;
105
90
  };
106
91
  export type StoreConfig<TState, TActions> = BaseConfig<TState, TActions> & (PersistConfig | NoPersistConfig);
92
+ /** @deprecated Use `StateContext` instead. */
93
+ export type StoreEventContext<TState> = StateContext<TState>;
94
+ /** @deprecated Use `StateContext` instead. */
95
+ export type ActionContext<TState> = StateContext<TState>;
@@ -26,39 +26,32 @@ const createStore = (config) => {
26
26
  };
27
27
  const reset = () => set(initialState);
28
28
  const listeners = new Set();
29
- const actions = config.actions?.({
30
- set,
29
+ const ctx = {
31
30
  get,
32
- reset,
33
- initialState,
34
- });
35
- // TODO: Replace with a static object once `state` is removed.
36
- const getContext = () => ({
37
- get,
38
- state,
39
31
  set,
40
32
  reset,
41
33
  initialState,
42
- });
34
+ };
35
+ const actions = config.actions?.(ctx);
43
36
  const storageAdapter = createStorageAdapter(config, get, set);
44
37
  const subscribe = (listener) => {
45
38
  if (listeners.size === 0) {
46
39
  storageAdapter?.onAttach();
47
- onAttach?.(getContext());
40
+ onAttach?.(ctx);
48
41
  }
49
42
  listener(state);
50
43
  listeners.add(listener);
51
44
  return () => {
52
45
  if (listeners.delete(listener) && listeners.size === 0) {
53
46
  storageAdapter?.onDetach();
54
- onDetach?.(getContext());
47
+ onDetach?.(ctx);
55
48
  }
56
49
  };
57
50
  };
58
51
  const dispatch = () => {
59
52
  isSilent = true;
60
53
  try {
61
- onChange?.(getContext());
54
+ onChange?.(ctx);
62
55
  }
63
56
  finally {
64
57
  isSilent = false;
@@ -67,7 +60,7 @@ const createStore = (config) => {
67
60
  listeners.forEach((listener) => listener(state));
68
61
  };
69
62
  storageAdapter?.onLoad();
70
- onLoad?.(getContext());
63
+ onLoad?.(ctx);
71
64
  return {
72
65
  get,
73
66
  subscribe,
@@ -1 +1 @@
1
- {"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport {\n Listener,\n StateModifier,\n Store,\n StoreConfig,\n StoreEventContext,\n} 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 // Set while onChange runs, so its synchronous sets join the current dispatch\n let isSilent = false;\n\n const get = () => state;\n\n const set = (stateModifier: StateModifier<TState>) => {\n const prevState = state;\n state = merge(state, stateModifier);\n\n if (!Object.is(state, prevState) && !isSilent) {\n dispatch();\n }\n\n return state;\n };\n\n const reset = () => set(initialState);\n\n const listeners = new Set<Listener<TState>>();\n\n const actions = config.actions?.({\n set,\n get,\n reset,\n initialState,\n }) as TActions;\n\n // TODO: Replace with a static object once `state` is removed.\n const getContext = (): StoreEventContext<TState> => ({\n get,\n state,\n set,\n reset,\n initialState,\n });\n\n const storageAdapter = createStorageAdapter(config, get, set);\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\n storageAdapter?.onAttach();\n onAttach?.(getContext());\n }\n\n listener(state);\n listeners.add(listener);\n\n return () => {\n if (listeners.delete(listener) && listeners.size === 0) {\n storageAdapter?.onDetach();\n onDetach?.(getContext());\n }\n };\n };\n\n const dispatch = () => {\n isSilent = true;\n try {\n onChange?.(getContext());\n } finally {\n isSilent = false;\n }\n storageAdapter?.onChange(state);\n listeners.forEach((listener) => listener(state));\n };\n\n storageAdapter?.onLoad();\n onLoad?.(getContext());\n\n return {\n get,\n subscribe,\n actions,\n set,\n reset,\n initialState,\n };\n};\n"],"names":[],"mappings":";;;AAUA;;;;;;;;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;;IAGxB,IAAI,QAAQ,GAAG,KAAK;AAEpB,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK;AAEvB,IAAA,MAAM,GAAG,GAAG,CAAC,aAAoC,KAAI;QACnD,MAAM,SAAS,GAAG,KAAK;AACvB,QAAA,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,aAAa,CAAC;AAEnC,QAAA,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE;AAC7C,YAAA,QAAQ,EAAE;QACZ;AAEA,QAAA,OAAO,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AAClD,IAAA,CAAC;IAED,cAAc,EAAE,MAAM,EAAE;AACxB,IAAA,MAAM,GAAG,UAAU,EAAE,CAAC;IAEtB,OAAO;QACL,GAAG;QACH,SAAS;QACT,OAAO;QACP,GAAG;QACH,KAAK;QACL,YAAY;KACb;AACH;;;;"}
1
+ {"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { createStorageAdapter } from \"../internal/createStorageAdapter\";\nimport {\n Listener,\n StateContext,\n StateModifier,\n Store,\n StoreConfig,\n} 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 // Set while onChange runs, so its synchronous sets join the current dispatch\n let isSilent = false;\n\n const get = () => state;\n\n const set = (stateModifier: StateModifier<TState>) => {\n const prevState = state;\n state = merge(state, stateModifier);\n\n if (!Object.is(state, prevState) && !isSilent) {\n dispatch();\n }\n\n return state;\n };\n\n const reset = () => set(initialState);\n\n const listeners = new Set<Listener<TState>>();\n\n const ctx: StateContext<TState> = {\n get,\n set,\n reset,\n initialState,\n };\n\n const actions = config.actions?.(ctx) as TActions;\n\n const storageAdapter = createStorageAdapter(config, get, set);\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\n storageAdapter?.onAttach();\n onAttach?.(ctx);\n }\n\n listener(state);\n listeners.add(listener);\n\n return () => {\n if (listeners.delete(listener) && listeners.size === 0) {\n storageAdapter?.onDetach();\n onDetach?.(ctx);\n }\n };\n };\n\n const dispatch = () => {\n isSilent = true;\n try {\n onChange?.(ctx);\n } finally {\n isSilent = false;\n }\n storageAdapter?.onChange(state);\n listeners.forEach((listener) => listener(state));\n };\n\n storageAdapter?.onLoad();\n onLoad?.(ctx);\n\n return {\n get,\n subscribe,\n actions,\n set,\n reset,\n initialState,\n };\n};\n"],"names":[],"mappings":";;;AAUA;;;;;;;;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;;IAGxB,IAAI,QAAQ,GAAG,KAAK;AAEpB,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK;AAEvB,IAAA,MAAM,GAAG,GAAG,CAAC,aAAoC,KAAI;QACnD,MAAM,SAAS,GAAG,KAAK;AACvB,QAAA,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,aAAa,CAAC;AAEnC,QAAA,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE;AAC7C,YAAA,QAAQ,EAAE;QACZ;AAEA,QAAA,OAAO,KAAK;AACd,IAAA,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,YAAY,CAAC;AAErC,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAoB;AAE7C,IAAA,MAAM,GAAG,GAAyB;QAChC,GAAG;QACH,GAAG;QACH,KAAK;QACL,YAAY;KACb;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,GAAG,GAAG,CAAa;IAEjD,MAAM,cAAc,GAAG,oBAAoB,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC;AAE7D,IAAA,MAAM,SAAS,GAAG,CAAC,QAA0B,KAAI;AAC/C,QAAA,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;YACxB,cAAc,EAAE,QAAQ,EAAE;AAC1B,YAAA,QAAQ,GAAG,GAAG,CAAC;QACjB;QAEA,QAAQ,CAAC,KAAK,CAAC;AACf,QAAA,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC;AAEvB,QAAA,OAAO,MAAK;AACV,YAAA,IAAI,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;gBACtD,cAAc,EAAE,QAAQ,EAAE;AAC1B,gBAAA,QAAQ,GAAG,GAAG,CAAC;YACjB;AACF,QAAA,CAAC;AACH,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;QACpB,QAAQ,GAAG,IAAI;AACf,QAAA,IAAI;AACF,YAAA,QAAQ,GAAG,GAAG,CAAC;QACjB;gBAAU;YACR,QAAQ,GAAG,KAAK;QAClB;AACA,QAAA,cAAc,EAAE,QAAQ,CAAC,KAAK,CAAC;AAC/B,QAAA,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClD,IAAA,CAAC;IAED,cAAc,EAAE,MAAM,EAAE;AACxB,IAAA,MAAM,GAAG,GAAG,CAAC;IAEb,OAAO;QACL,GAAG;QACH,SAAS;QACT,OAAO;QACP,GAAG;QACH,KAAK;QACL,YAAY;KACb;AACH;;;;"}
@@ -30,24 +30,9 @@ export type Effect = {
30
30
  /** Stop listening to changes in the original store. */
31
31
  unmount: () => void;
32
32
  };
33
- export type StoreEventContext<TState> = {
34
- /** Returns the current state of the store. */
35
- get: () => TState;
36
- /**
37
- * A snapshot of the state.
38
- * @deprecated Use `get()` instead.
39
- */
40
- state: TState;
41
- /** The initial state of the store. */
42
- initialState: TState;
43
- /** Sets the state of the store. */
44
- set: SetState<TState>;
45
- /** Resets the state of the store to its initial value. */
46
- reset: () => TState;
47
- };
48
- export type StoreEventListener<TState> = (context: StoreEventContext<TState>) => void;
33
+ export type StoreEventListener<TState> = (context: StateContext<TState>) => void;
49
34
  export type StateModifier<TState> = Partial<TState> | ((state: TState) => Partial<TState>);
50
- export type ActionContext<TState> = {
35
+ export type StateContext<TState> = Readonly<{
51
36
  /** Returns the current state of the store. */
52
37
  get: () => TState;
53
38
  /** Sets the state of the store. */
@@ -56,8 +41,8 @@ export type ActionContext<TState> = {
56
41
  reset: () => TState;
57
42
  /** The initial state of the store. */
58
43
  initialState: TState;
59
- };
60
- export type StoreActions<TState, TActions> = (context: ActionContext<TState>) => TActions;
44
+ }>;
45
+ export type StoreActions<TState, TActions> = (context: StateContext<TState>) => TActions;
61
46
  export type BaseConfig<TState, TActions> = {
62
47
  /** The initial state of the store. */
63
48
  initialState: TState;
@@ -104,3 +89,7 @@ export type PersistConfig = {
104
89
  onStorageError?: (error: unknown) => void;
105
90
  };
106
91
  export type StoreConfig<TState, TActions> = BaseConfig<TState, TActions> & (PersistConfig | NoPersistConfig);
92
+ /** @deprecated Use `StateContext` instead. */
93
+ export type StoreEventContext<TState> = StateContext<TState>;
94
+ /** @deprecated Use `StateContext` instead. */
95
+ export type ActionContext<TState> = StateContext<TState>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dharma-core",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
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.",