dharma-core 0.3.0 → 0.4.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.
@@ -1,4 +1,4 @@
1
- import { DefineActions, Store, StoreOptions } from "./createStore";
1
+ import { Store, StoreConfiguration } from "./createStore";
2
2
  export type StorageAPI = {
3
3
  getItem: (key: string) => string | null;
4
4
  setItem: (key: string, value: string) => void;
@@ -8,7 +8,8 @@ export type Serializer<T = any> = {
8
8
  stringify: (value: T) => string;
9
9
  parse: (value: string) => T;
10
10
  };
11
- export type PersistentStoreOptions<TState extends object> = StoreOptions<TState> & {
11
+ export type PersistentStoreConfiguration<TState extends object, TActions extends object> = StoreConfiguration<TState, TActions> & {
12
+ key: string;
12
13
  /** The storage to use for persisting the state. Defaults to local storage. */
13
14
  storage?: StorageAPI | "local" | "session";
14
15
  /** The serializer to use for storing the state. Defaults to JSON. */
@@ -24,7 +25,7 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
24
25
  * @param {string} key - A unique key to identify the store in storage.
25
26
  * @param {TState} initialState - The initial state of the store.
26
27
  * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.
27
- * @param {PersistentStoreOptions<TState>} [options] - Additional options for the persistent store.
28
+ * @param {PersistentStoreConfiguration<TState>} [options] - Additional options for the persistent store.
28
29
  *
29
30
  * @returns {Store<TState, TActions>} The created store.
30
31
  *
@@ -33,7 +34,7 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
33
34
  * ```ts
34
35
  * import { createPersistentStore } from "dharma-core";
35
36
  *
36
- * const store = createPersistentStore("count", { count: 0 }, (set) => ({
37
+ * const store = createPersistentStore("count", { count: 0 }, ({ set }) => ({
37
38
  * increment: () => set((state) => ({ count: state.count + 1 })),
38
39
  * decrement: () => set((state) => ({ count: state.count - 1 })),
39
40
  * reset: () => set({ count: 0 }),
@@ -48,7 +49,7 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
48
49
  * const store = createPersistentStore(
49
50
  * "count",
50
51
  * { count: 0 },
51
- * (set) => ({
52
+ * ({ set }) => ({
52
53
  * increment: () => set((state) => ({ count: state.count + 1 })),
53
54
  * decrement: () => set((state) => ({ count: state.count - 1 })),
54
55
  * reset: () => set({ count: 0 }),
@@ -59,6 +60,5 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
59
60
  * },
60
61
  * );
61
62
  * ```
62
- * @group Core
63
63
  */
64
- export declare const createPersistentStore: <TState extends object, TActions extends object = Record<never, never>>(key: string, initialState: TState, defineActions: DefineActions<TState, TActions>, { storage: _storage, serializer, onAttach, onDetach, onChange, ...options }?: PersistentStoreOptions<TState>) => Store<TState, TActions>;
64
+ export declare const createPersistentStore: <TState extends object, TActions extends object = Record<never, never>>(options: PersistentStoreConfiguration<TState, TActions>) => Store<TState, TActions>;
@@ -12,7 +12,7 @@ var createStore = require('./createStore.js');
12
12
  * @param {string} key - A unique key to identify the store in storage.
13
13
  * @param {TState} initialState - The initial state of the store.
14
14
  * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.
15
- * @param {PersistentStoreOptions<TState>} [options] - Additional options for the persistent store.
15
+ * @param {PersistentStoreConfiguration<TState>} [options] - Additional options for the persistent store.
16
16
  *
17
17
  * @returns {Store<TState, TActions>} The created store.
18
18
  *
@@ -21,7 +21,7 @@ var createStore = require('./createStore.js');
21
21
  * ```ts
22
22
  * import { createPersistentStore } from "dharma-core";
23
23
  *
24
- * const store = createPersistentStore("count", { count: 0 }, (set) => ({
24
+ * const store = createPersistentStore("count", { count: 0 }, ({ set }) => ({
25
25
  * increment: () => set((state) => ({ count: state.count + 1 })),
26
26
  * decrement: () => set((state) => ({ count: state.count - 1 })),
27
27
  * reset: () => set({ count: 0 }),
@@ -36,7 +36,7 @@ var createStore = require('./createStore.js');
36
36
  * const store = createPersistentStore(
37
37
  * "count",
38
38
  * { count: 0 },
39
- * (set) => ({
39
+ * ({ set }) => ({
40
40
  * increment: () => set((state) => ({ count: state.count + 1 })),
41
41
  * decrement: () => set((state) => ({ count: state.count - 1 })),
42
42
  * reset: () => set({ count: 0 }),
@@ -47,12 +47,12 @@ var createStore = require('./createStore.js');
47
47
  * },
48
48
  * );
49
49
  * ```
50
- * @group Core
51
50
  */
52
- const createPersistentStore = (key, initialState, defineActions, { storage: _storage = "local", serializer = JSON, onAttach, onDetach, onChange, ...options } = {}) => {
51
+ const createPersistentStore = (options) => {
53
52
  if (typeof window === "undefined") {
54
- return createStore.createStore(initialState, defineActions, options);
53
+ return createStore.createStore(options);
55
54
  }
55
+ const { key, initialState, storage: _storage = "local", serializer = JSON, onAttach, onDetach, onChange, ...rest } = options;
56
56
  const storage = getStorage(_storage);
57
57
  const stateKey = `store_${key}`;
58
58
  const initialStateKey = `init_${key}`;
@@ -76,21 +76,22 @@ const createPersistentStore = (key, initialState, defineActions, { storage: _sto
76
76
  store.set(serializer.parse(currentSnapshot));
77
77
  }
78
78
  };
79
- const store = createStore.createStore(initialState, defineActions, {
80
- onAttach: (...args) => {
81
- onAttach?.(...args);
79
+ const store = createStore.createStore({
80
+ initialState,
81
+ onAttach: (ctx) => {
82
+ onAttach?.(ctx);
82
83
  updateState();
83
84
  window.addEventListener("focus", updateState);
84
85
  },
85
- onDetach: (...args) => {
86
- onDetach?.(...args);
86
+ onDetach: (ctx) => {
87
+ onDetach?.(ctx);
87
88
  window.removeEventListener("focus", updateState);
88
89
  },
89
- onChange: (state, ...args) => {
90
- onChange?.(state, ...args);
90
+ onChange: ({ state, ...ctx }) => {
91
+ onChange?.({ state, ...ctx });
91
92
  updateSnapshot(state);
92
93
  },
93
- ...options,
94
+ ...rest,
94
95
  });
95
96
  return store;
96
97
  };
@@ -1 +1 @@
1
- {"version":3,"file":"createPersistentStore.js","sources":["../../../src/lib/createPersistentStore.ts"],"sourcesContent":["import { createStore, DefineActions, Store, StoreOptions } from \"./createStore\";\n\nexport type StorageAPI = {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Serializer<T = any> = {\n stringify: (value: T) => string;\n parse: (value: string) => T;\n};\n\nexport type PersistentStoreOptions<TState extends object> =\n StoreOptions<TState> & {\n /** The storage to use for persisting the state. Defaults to local storage. */\n storage?: StorageAPI | \"local\" | \"session\";\n /** The serializer to use for storing the state. Defaults to JSON. */\n serializer?: Serializer<TState>;\n };\n\n/**\n * Creates a store that persists its state in local or session storage.\n * Defaults to local storage but this can be changed in the options.\n *\n * **Note:** The state needs to be serializable by whatever serializer you use. (JSON by default)\n * If you need something more versatile I would recommend using a library like [superjson](https://github.com/flightcontrolhq/superjson).\n *\n * @param {string} key - A unique key to identify the store in storage.\n * @param {TState} initialState - The initial state of the store.\n * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.\n * @param {PersistentStoreOptions<TState>} [options] - Additional options for the persistent store.\n *\n * @returns {Store<TState, TActions>} The created store.\n *\n * @example\n * Basic usage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n *\n * const store = createPersistentStore(\"count\", { 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 * @example\n * With superjson serialization and session storage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n * import superjson from \"superjson\";\n *\n * const store = createPersistentStore(\n * \"count\",\n * { count: 0 },\n * (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 * serializer: superjson,\n * storage: \"session\",\n * },\n * );\n * ```\n * @group Core\n */\nexport const createPersistentStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n key: string,\n initialState: TState,\n defineActions: DefineActions<TState, TActions>,\n {\n storage: _storage = \"local\",\n serializer = JSON,\n onAttach,\n onDetach,\n onChange,\n ...options\n }: PersistentStoreOptions<TState> = {},\n): Store<TState, TActions> => {\n if (typeof window === \"undefined\") {\n return createStore(initialState, defineActions, options);\n }\n\n const storage = getStorage(_storage);\n const stateKey = `store_${key}`;\n const initialStateKey = `init_${key}`;\n const initialStateSnapshot = storage.getItem(initialStateKey);\n const initialStateString = serializer.stringify(initialState);\n\n if (initialStateSnapshot !== initialStateString) {\n storage.setItem(initialStateKey, initialStateString);\n storage.removeItem(stateKey);\n }\n\n const updateSnapshot = (newState: TState) => {\n const currentSnapshot = storage.getItem(stateKey);\n const newSnapshot = serializer.stringify(newState);\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(stateKey, newSnapshot);\n }\n };\n\n const updateState = () => {\n const currentSnapshot = storage.getItem(stateKey);\n\n if (\n currentSnapshot &&\n currentSnapshot !== serializer.stringify(store.get())\n ) {\n store.set(serializer.parse(currentSnapshot));\n }\n };\n\n const store = createStore(initialState, defineActions, {\n onAttach: (...args) => {\n onAttach?.(...args);\n updateState();\n window.addEventListener(\"focus\", updateState);\n },\n onDetach: (...args) => {\n onDetach?.(...args);\n window.removeEventListener(\"focus\", updateState);\n },\n onChange: (state, ...args) => {\n onChange?.(state, ...args);\n updateSnapshot(state);\n },\n ...options,\n });\n\n return store;\n};\n\nconst getStorage = (storage: StorageAPI | \"local\" | \"session\"): StorageAPI => {\n switch (storage) {\n case \"local\":\n return localStorage;\n case \"session\":\n return sessionStorage;\n default:\n return storage;\n }\n};\n"],"names":["createStore"],"mappings":";;;;AAsBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CG;AACI,MAAM,qBAAqB,GAAG,CAInC,GAAW,EACX,YAAoB,EACpB,aAA8C,EAC9C,EACE,OAAO,EAAE,QAAQ,GAAG,OAAO,EAC3B,UAAU,GAAG,IAAI,EACjB,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,GAAG,OAAO,EAAA,GACwB,EAAE,KACX;AAC3B,IAAA,IAAI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D,QAAA,QAAQ,EAAE,CAAC,GAAG,IAAI,KAAI;AACpB,YAAA,QAAQ,GAAG,GAAG,IAAI,CAAC;AACnB,YAAA,WAAW,EAAE;AACb,YAAA,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,WAAW,CAAC;QAC/C,CAAC;AACD,QAAA,QAAQ,EAAE,CAAC,GAAG,IAAI,KAAI;AACpB,YAAA,QAAQ,GAAG,GAAG,IAAI,CAAC;AACnB,YAAA,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,WAAW,CAAC;QAClD,CAAC;AACD,QAAA,QAAQ,EAAE,CAAC,KAAK,EAAE,GAAG,IAAI,KAAI;AAC3B,YAAA,QAAQ,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC;YAC1B,cAAc,CAAC,KAAK,CAAC;QACvB,CAAC;AACD,QAAA,GAAG,OAAO;AACX,KAAA,CAAC;AAEF,IAAA,OAAO,KAAK;AACd;AAEA,MAAM,UAAU,GAAG,CAAC,OAAyC,KAAgB;IAC3E,QAAQ,OAAO;AACb,QAAA,KAAK,OAAO;AACV,YAAA,OAAO,YAAY;AACrB,QAAA,KAAK,SAAS;AACZ,YAAA,OAAO,cAAc;AACvB,QAAA;AACE,YAAA,OAAO,OAAO;;AAEpB,CAAC;;;;"}
1
+ {"version":3,"file":"createPersistentStore.js","sources":["../../../src/lib/createPersistentStore.ts"],"sourcesContent":["import { createStore, Store, StoreConfiguration } from \"./createStore\";\n\nexport type StorageAPI = {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Serializer<T = any> = {\n stringify: (value: T) => string;\n parse: (value: string) => T;\n};\n\nexport type PersistentStoreConfiguration<\n TState extends object,\n TActions extends object,\n> = StoreConfiguration<TState, TActions> & {\n key: string;\n /** The storage to use for persisting the state. Defaults to local storage. */\n storage?: StorageAPI | \"local\" | \"session\";\n /** The serializer to use for storing the state. Defaults to JSON. */\n serializer?: Serializer<TState>;\n};\n\n/**\n * Creates a store that persists its state in local or session storage.\n * Defaults to local storage but this can be changed in the options.\n *\n * **Note:** The state needs to be serializable by whatever serializer you use. (JSON by default)\n * If you need something more versatile I would recommend using a library like [superjson](https://github.com/flightcontrolhq/superjson).\n *\n * @param {string} key - A unique key to identify the store in storage.\n * @param {TState} initialState - The initial state of the store.\n * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.\n * @param {PersistentStoreConfiguration<TState>} [options] - Additional options for the persistent store.\n *\n * @returns {Store<TState, TActions>} The created store.\n *\n * @example\n * Basic usage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n *\n * const store = createPersistentStore(\"count\", { 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 * @example\n * With superjson serialization and session storage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n * import superjson from \"superjson\";\n *\n * const store = createPersistentStore(\n * \"count\",\n * { count: 0 },\n * ({ 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 * serializer: superjson,\n * storage: \"session\",\n * },\n * );\n * ```\n */\nexport const createPersistentStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n options: PersistentStoreConfiguration<TState, TActions>,\n): Store<TState, TActions> => {\n if (typeof window === \"undefined\") {\n return createStore(options);\n }\n\n const {\n key,\n initialState,\n storage: _storage = \"local\",\n serializer = JSON,\n onAttach,\n onDetach,\n onChange,\n ...rest\n } = options;\n\n const storage = getStorage(_storage);\n const stateKey = `store_${key}`;\n const initialStateKey = `init_${key}`;\n const initialStateSnapshot = storage.getItem(initialStateKey);\n const initialStateString = serializer.stringify(initialState);\n\n if (initialStateSnapshot !== initialStateString) {\n storage.setItem(initialStateKey, initialStateString);\n storage.removeItem(stateKey);\n }\n\n const updateSnapshot = (newState: TState) => {\n const currentSnapshot = storage.getItem(stateKey);\n const newSnapshot = serializer.stringify(newState);\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(stateKey, newSnapshot);\n }\n };\n\n const updateState = () => {\n const currentSnapshot = storage.getItem(stateKey);\n\n if (\n currentSnapshot &&\n currentSnapshot !== serializer.stringify(store.get())\n ) {\n store.set(serializer.parse(currentSnapshot));\n }\n };\n\n const store = createStore({\n initialState,\n onAttach: (ctx) => {\n onAttach?.(ctx);\n updateState();\n window.addEventListener(\"focus\", updateState);\n },\n onDetach: (ctx) => {\n onDetach?.(ctx);\n window.removeEventListener(\"focus\", updateState);\n },\n onChange: ({ state, ...ctx }) => {\n onChange?.({ state, ...ctx });\n updateSnapshot(state);\n },\n ...rest,\n });\n\n return store;\n};\n\nconst getStorage = (storage: StorageAPI | \"local\" | \"session\"): StorageAPI => {\n switch (storage) {\n case \"local\":\n return localStorage;\n case \"session\":\n return sessionStorage;\n default:\n return storage;\n }\n};\n"],"names":["createStore"],"mappings":";;;;AAyBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CG;AACI,MAAM,qBAAqB,GAAG,CAInC,OAAuD,KAC5B;AAC3B,IAAA,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;AACjC,QAAA,OAAOA,uBAAW,CAAC,OAAO,CAAC;IAC7B;IAEA,MAAM,EACJ,GAAG,EACH,YAAY,EACZ,OAAO,EAAE,QAAQ,GAAG,OAAO,EAC3B,UAAU,GAAG,IAAI,EACjB,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,GAAG,IAAI,EACR,GAAG,OAAO;AAEX,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,QAAQ,CAAC;AACpC,IAAA,MAAM,QAAQ,GAAG,CAAA,MAAA,EAAS,GAAG,EAAE;AAC/B,IAAA,MAAM,eAAe,GAAG,CAAA,KAAA,EAAQ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CAAC,OAAO,EAAE,WAAW,CAAC;QAClD,CAAC;QACD,QAAQ,EAAE,CAAC,EAAE,KAAK,EAAE,GAAG,GAAG,EAAE,KAAI;YAC9B,QAAQ,GAAG,EAAE,KAAK,EAAE,GAAG,GAAG,EAAE,CAAC;YAC7B,cAAc,CAAC,KAAK,CAAC;QACvB,CAAC;AACD,QAAA,GAAG,IAAI;AACR,KAAA,CAAC;AAEF,IAAA,OAAO,KAAK;AACd;AAEA,MAAM,UAAU,GAAG,CAAC,OAAyC,KAAgB;IAC3E,QAAQ,OAAO;AACb,QAAA,KAAK,OAAO;AACV,YAAA,OAAO,YAAY;AACrB,QAAA,KAAK,SAAS;AACZ,YAAA,OAAO,cAAc;AACvB,QAAA;AACE,YAAA,OAAO,OAAO;;AAEpB,CAAC;;;;"}
@@ -1,19 +1,41 @@
1
+ type Listener<TState extends object> = (state: TState) => void;
2
+ export type SetState<TState extends object> = (stateModifier: StateModifier<TState>) => TState;
1
3
  export type Store<TState extends object, TActions extends object> = {
2
4
  /** Returns the current state of the store. */
3
5
  get: () => TState;
4
6
  /** Sets the state of the store. */
5
- set: (stateModifier: StateModifier<TState>) => TState;
7
+ set: SetState<TState>;
8
+ /** Resets the state of the store to its initial value. */
9
+ reset: () => TState;
6
10
  /** Actions that can modify the state of the store. */
7
11
  actions: TActions;
8
12
  /** Subscribes to changes in the state of the store. Returns an unsubscribe function. */
9
13
  subscribe: (listener: Listener<TState>) => () => void;
10
14
  };
11
- type Listener<TState extends object> = (state: TState) => void;
12
- export type StoreEventHandler<TState extends object> = (state: TState, set: SetState<TState>) => void;
13
- export type SetState<TState extends object> = (stateModifier: StateModifier<TState>) => TState;
15
+ export type EventHandlerContext<TState extends object> = {
16
+ /** The current state of the store. */
17
+ state: TState;
18
+ /** Sets the state of the store. */
19
+ set: SetState<TState>;
20
+ /** Resets the state of the store to its initial value. */
21
+ reset: () => TState;
22
+ };
23
+ export type StoreEventHandler<TState extends object> = (context: EventHandlerContext<TState>) => void;
14
24
  export type StateModifier<TState extends object> = Partial<TState> | ((state: TState) => Partial<TState>);
15
- export type DefineActions<TState extends object, TActions> = (set: (stateModifier: StateModifier<TState>) => TState, get: () => TState) => TActions;
16
- export type StoreOptions<TState extends object> = {
25
+ export type StateFunctions<TState extends object> = {
26
+ /** Returns the current state of the store. */
27
+ get: () => TState;
28
+ /** Sets the state of the store. */
29
+ set: SetState<TState>;
30
+ /** Resets the state of the store to its initial value. */
31
+ reset: () => TState;
32
+ };
33
+ export type DefineActions<TState extends object, TActions> = (stateFunctions: StateFunctions<TState>) => TActions;
34
+ export type StoreConfiguration<TState extends object, TActions extends object> = {
35
+ /** The initial state of the store. */
36
+ initialState: TState;
37
+ /** A function that defines actions that can modify the state. */
38
+ defineActions?: DefineActions<TState, TActions>;
17
39
  /** Invoked when the store is created. */
18
40
  onLoad?: StoreEventHandler<TState>;
19
41
  /** Invoked when the store is subscribed to. */
@@ -26,9 +48,7 @@ export type StoreOptions<TState extends object> = {
26
48
  /**
27
49
  * Creates a store with an initial state and actions that can modify the state.
28
50
  *
29
- * @param {TState} initialState - The initial state of the store.
30
- * @param {DefineActions<TState, TActions>} [defineActions] - A function that defines actions that can modify the state.
31
- * @param {StoreOptions<TState>} [options] - Additional options for the store.
51
+ * @param {StoreConfiguration<TState>} [config] - The configuration for the store.
32
52
  *
33
53
  * @returns {Store<TState, TActions>} The created store with state management methods.
34
54
  *
@@ -36,13 +56,14 @@ export type StoreOptions<TState extends object> = {
36
56
  * ```ts
37
57
  * import { createStore } from "dharma-core";
38
58
  *
39
- * const store = createStore({ count: 0 }, (set) => ({
40
- * increment: () => set((state) => ({ count: state.count + 1 })),
41
- * decrement: () => set((state) => ({ count: state.count - 1 })),
42
- * reset: () => set({ count: 0 }),
43
- * }));
59
+ * const store = createStore({
60
+ * initialState: { count: 0 },
61
+ * defineActions: ({ set }) => ({
62
+ * increment: () => set((state) => ({ count: state.count + 1 })),
63
+ * decrement: () => set((state) => ({ count: state.count - 1 })),
64
+ * }),
65
+ * });
44
66
  * ```
45
- * @group Core
46
67
  */
47
- export declare const createStore: <TState extends object, TActions extends object = Record<never, never>>(initialState: TState, defineActions: DefineActions<TState, TActions>, { onLoad, onAttach, onDetach, onChange }?: StoreOptions<TState>) => Store<TState, TActions>;
68
+ export declare const createStore: <TState extends object, TActions extends object = Record<never, never>>(config: StoreConfiguration<TState, TActions>) => Store<TState, TActions>;
48
69
  export {};
@@ -5,9 +5,7 @@ var merge = require('./merge.js');
5
5
  /**
6
6
  * Creates a store with an initial state and actions that can modify the state.
7
7
  *
8
- * @param {TState} initialState - The initial state of the store.
9
- * @param {DefineActions<TState, TActions>} [defineActions] - A function that defines actions that can modify the state.
10
- * @param {StoreOptions<TState>} [options] - Additional options for the store.
8
+ * @param {StoreConfiguration<TState>} [config] - The configuration for the store.
11
9
  *
12
10
  * @returns {Store<TState, TActions>} The created store with state management methods.
13
11
  *
@@ -15,15 +13,17 @@ var merge = require('./merge.js');
15
13
  * ```ts
16
14
  * import { createStore } from "dharma-core";
17
15
  *
18
- * const store = createStore({ count: 0 }, (set) => ({
19
- * increment: () => set((state) => ({ count: state.count + 1 })),
20
- * decrement: () => set((state) => ({ count: state.count - 1 })),
21
- * reset: () => set({ count: 0 }),
22
- * }));
16
+ * const store = createStore({
17
+ * initialState: { count: 0 },
18
+ * defineActions: ({ set }) => ({
19
+ * increment: () => set((state) => ({ count: state.count + 1 })),
20
+ * decrement: () => set((state) => ({ count: state.count - 1 })),
21
+ * }),
22
+ * });
23
23
  * ```
24
- * @group Core
25
24
  */
26
- const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach, onChange } = {}) => {
25
+ const createStore = (config) => {
26
+ const { initialState, defineActions, onLoad, onAttach, onDetach, onChange } = config;
27
27
  let state = initialState;
28
28
  const listeners = new Set();
29
29
  const get = () => state;
@@ -31,8 +31,9 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
31
31
  state = merge.merge(state, stateModifier);
32
32
  return state;
33
33
  };
34
+ const resetSilently = () => setSilently(initialState);
34
35
  const dispatch = () => {
35
- onChange?.(state, setSilently);
36
+ onChange?.({ state, set: setSilently, reset: resetSilently });
36
37
  listeners.forEach((listener) => listener(state));
37
38
  };
38
39
  const set = (stateModifier) => {
@@ -40,24 +41,32 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
40
41
  dispatch();
41
42
  return state;
42
43
  };
44
+ const reset = () => {
45
+ resetSilently();
46
+ dispatch();
47
+ return state;
48
+ };
43
49
  const subscribe = (listener) => {
44
- listener(state);
45
50
  if (listeners.size === 0) {
46
- onAttach?.(state, set);
51
+ onAttach?.({ state, set, reset });
47
52
  }
53
+ listener(state);
48
54
  listeners.add(listener);
49
55
  return () => {
50
56
  listeners.delete(listener);
51
57
  if (listeners.size === 0) {
52
- onDetach?.(state, set);
58
+ onDetach?.({ state, set, reset });
53
59
  }
54
60
  };
55
61
  };
56
- const actions = defineActions ? defineActions(set, get) : {};
57
- onLoad?.(state, set);
62
+ const actions = defineActions
63
+ ? defineActions({ set, get, reset })
64
+ : {};
65
+ onLoad?.({ state, set, reset });
58
66
  return {
59
67
  get,
60
68
  set,
69
+ reset,
61
70
  subscribe,
62
71
  actions,
63
72
  };
@@ -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<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 = def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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;;;;"}
1
+ {"version":3,"file":"createStore.js","sources":["../../../src/lib/createStore.ts"],"sourcesContent":["import { merge } from \"./merge\";\n\ntype Listener<TState extends object> = (state: TState) => void;\n\nexport type SetState<TState extends object> = (\n stateModifier: StateModifier<TState>,\n) => TState;\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: SetState<TState>;\n /** Resets the state of the store to its initial value. */\n reset: () => 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\nexport type EventHandlerContext<TState extends object> = {\n /** The current state of the store. */\n state: TState;\n /** Sets the state of the store. */\n set: SetState<TState>;\n /** Resets the state of the store to its initial value. */\n reset: () => TState;\n};\n\nexport type StoreEventHandler<TState extends object> = (\n context: EventHandlerContext<TState>,\n) => void;\n\nexport type StateModifier<TState extends object> =\n | Partial<TState>\n | ((state: TState) => Partial<TState>);\n\nexport type StateFunctions<TState extends object> = {\n /** Returns the current state of the store. */\n get: () => TState;\n /** Sets the state of the store. */\n set: SetState<TState>;\n /** Resets the state of the store to its initial value. */\n reset: () => TState;\n};\n\nexport type DefineActions<TState extends object, TActions> = (\n stateFunctions: StateFunctions<TState>,\n) => TActions;\n\nexport type StoreConfiguration<\n TState extends object,\n TActions extends object,\n> = {\n /** The initial state of the store. */\n initialState: TState;\n /** A function that defines actions that can modify the state. */\n defineActions?: DefineActions<TState, TActions>;\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 {StoreConfiguration<TState>} [config] - The configuration 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({\n * initialState: { count: 0 },\n * defineActions: ({ set }) => ({\n * increment: () => set((state) => ({ count: state.count + 1 })),\n * decrement: () => set((state) => ({ count: state.count - 1 })),\n * }),\n * });\n * ```\n */\nexport const createStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n config: StoreConfiguration<TState, TActions>,\n): Store<TState, TActions> => {\n const { initialState, defineActions, onLoad, onAttach, onDetach, onChange } =\n config;\n\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 resetSilently = () => setSilently(initialState);\n\n const dispatch = () => {\n onChange?.({ state, set: setSilently, reset: resetSilently });\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 reset = () => {\n resetSilently();\n dispatch();\n return state;\n };\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\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 onDetach?.({ state, set, reset });\n }\n };\n };\n\n const actions = defineActions\n ? defineActions({ set, get, reset })\n : ({} as TActions);\n\n onLoad?.({ state, set, reset });\n\n return {\n get,\n set,\n reset,\n subscribe,\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AEA,QAAQ,CAAC,KAAK,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;gBACxB,QAAQ,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;YACnC;AACF,QAAA,CAAC;AACH,IAAA,CAAC;IAED,MAAM,OAAO,GAAG;UACZ,aAAa,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE;UAChC,EAAe;IAEpB,MAAM,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IAE/B,OAAO;QACL,GAAG;QACH,GAAG;QACH,KAAK;QACL,SAAS;QACT,OAAO;KACR;AACH;;;;"}
@@ -11,14 +11,14 @@ import { StateModifier } from "./createStore";
11
11
  * ```ts
12
12
  * import { createStore, merge, StateModifier } from "dharma-core";
13
13
  *
14
- * const store = createStore(
15
- * {
14
+ * const store = createStore({
15
+ * initialState: {
16
16
  * counter: {
17
17
  * count: 0,
18
18
  * },
19
19
  * // ...
20
20
  * },
21
- * (set) => {
21
+ * defineActions: ({ set }) => {
22
22
  * const setCounter = (counter: StateModifier<{ count: number }>) =>
23
23
  * set((state) => ({
24
24
  * counter: merge(state.counter, counter),
@@ -31,8 +31,7 @@ import { StateModifier } from "./createStore";
31
31
  * // ...
32
32
  * };
33
33
  * },
34
- * );
34
+ * });
35
35
  * ```
36
- * @group Utilities
37
36
  */
38
37
  export declare const merge: <T extends object>(currentState: T, stateModifier: StateModifier<T>) => T;
@@ -12,14 +12,14 @@
12
12
  * ```ts
13
13
  * import { createStore, merge, StateModifier } from "dharma-core";
14
14
  *
15
- * const store = createStore(
16
- * {
15
+ * const store = createStore({
16
+ * initialState: {
17
17
  * counter: {
18
18
  * count: 0,
19
19
  * },
20
20
  * // ...
21
21
  * },
22
- * (set) => {
22
+ * defineActions: ({ set }) => {
23
23
  * const setCounter = (counter: StateModifier<{ count: number }>) =>
24
24
  * set((state) => ({
25
25
  * counter: merge(state.counter, counter),
@@ -32,9 +32,8 @@
32
32
  * // ...
33
33
  * };
34
34
  * },
35
- * );
35
+ * });
36
36
  * ```
37
- * @group Utilities
38
37
  */
39
38
  const merge = (currentState, stateModifier) => {
40
39
  const newState = typeof stateModifier === "function"
@@ -1 +1 @@
1
- {"version":3,"file":"merge.js","sources":["../../../src/lib/merge.ts"],"sourcesContent":["import { StateModifier } from \"./createStore\";\n\n/**\n * Merges the current state with a state modifier.\n * Useful for creating custom set functions.\n *\n * @param {T} stateModifier - A function or object that modifies the state.\n * @param {StateModifier<T>} stateModifier - The current state object.\n * @returns {T} The new state object, which is a combination of the current state and the state modifier.\n *\n * @example\n * ```ts\n * import { createStore, merge, StateModifier } from \"dharma-core\";\n *\n * const store = createStore(\n * {\n * counter: {\n * count: 0,\n * },\n * // ...\n * },\n * (set) => {\n * const setCounter = (counter: StateModifier<{ count: number }>) =>\n * set((state) => ({\n * counter: merge(state.counter, counter),\n * }));\n *\n * return {\n * increment: () => setCounter((state) => ({ count: state.count + 1 })),\n * decrement: () => setCounter((state) => ({ count: state.count - 1 })),\n * reset: () => setCounter({ count: 0 }),\n * // ...\n * };\n * },\n * );\n * ```\n * @group Utilities\n */\nexport const merge = <T extends object>(\n currentState: T,\n stateModifier: StateModifier<T>,\n): T => {\n const newState =\n typeof stateModifier === \"function\"\n ? stateModifier(currentState)\n : stateModifier;\n return { ...currentState, ...newState };\n};\n"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCG;MACU,KAAK,GAAG,CACnB,YAAe,EACf,aAA+B,KAC1B;AACL,IAAA,MAAM,QAAQ,GACZ,OAAO,aAAa,KAAK;AACvB,UAAE,aAAa,CAAC,YAAY;UAC1B,aAAa;AACnB,IAAA,OAAO,EAAE,GAAG,YAAY,EAAE,GAAG,QAAQ,EAAE;AACzC;;;;"}
1
+ {"version":3,"file":"merge.js","sources":["../../../src/lib/merge.ts"],"sourcesContent":["import { StateModifier } from \"./createStore\";\n\n/**\n * Merges the current state with a state modifier.\n * Useful for creating custom set functions.\n *\n * @param {T} stateModifier - A function or object that modifies the state.\n * @param {StateModifier<T>} stateModifier - The current state object.\n * @returns {T} The new state object, which is a combination of the current state and the state modifier.\n *\n * @example\n * ```ts\n * import { createStore, merge, StateModifier } from \"dharma-core\";\n *\n * const store = createStore({\n * initialState: {\n * counter: {\n * count: 0,\n * },\n * // ...\n * },\n * defineActions: ({ set }) => {\n * const setCounter = (counter: StateModifier<{ count: number }>) =>\n * set((state) => ({\n * counter: merge(state.counter, counter),\n * }));\n *\n * return {\n * increment: () => setCounter((state) => ({ count: state.count + 1 })),\n * decrement: () => setCounter((state) => ({ count: state.count - 1 })),\n * reset: () => setCounter({ count: 0 }),\n * // ...\n * };\n * },\n * });\n * ```\n */\nexport const merge = <T extends object>(\n currentState: T,\n stateModifier: StateModifier<T>,\n): T => {\n const newState =\n typeof stateModifier === \"function\"\n ? stateModifier(currentState)\n : stateModifier;\n return { ...currentState, ...newState };\n};\n"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCG;MACU,KAAK,GAAG,CACnB,YAAe,EACf,aAA+B,KAC1B;AACL,IAAA,MAAM,QAAQ,GACZ,OAAO,aAAa,KAAK;AACvB,UAAE,aAAa,CAAC,YAAY;UAC1B,aAAa;AACnB,IAAA,OAAO,EAAE,GAAG,YAAY,EAAE,GAAG,QAAQ,EAAE;AACzC;;;;"}
@@ -1,4 +1,4 @@
1
- import { DefineActions, Store, StoreOptions } from "./createStore";
1
+ import { Store, StoreConfiguration } from "./createStore";
2
2
  export type StorageAPI = {
3
3
  getItem: (key: string) => string | null;
4
4
  setItem: (key: string, value: string) => void;
@@ -8,7 +8,8 @@ export type Serializer<T = any> = {
8
8
  stringify: (value: T) => string;
9
9
  parse: (value: string) => T;
10
10
  };
11
- export type PersistentStoreOptions<TState extends object> = StoreOptions<TState> & {
11
+ export type PersistentStoreConfiguration<TState extends object, TActions extends object> = StoreConfiguration<TState, TActions> & {
12
+ key: string;
12
13
  /** The storage to use for persisting the state. Defaults to local storage. */
13
14
  storage?: StorageAPI | "local" | "session";
14
15
  /** The serializer to use for storing the state. Defaults to JSON. */
@@ -24,7 +25,7 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
24
25
  * @param {string} key - A unique key to identify the store in storage.
25
26
  * @param {TState} initialState - The initial state of the store.
26
27
  * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.
27
- * @param {PersistentStoreOptions<TState>} [options] - Additional options for the persistent store.
28
+ * @param {PersistentStoreConfiguration<TState>} [options] - Additional options for the persistent store.
28
29
  *
29
30
  * @returns {Store<TState, TActions>} The created store.
30
31
  *
@@ -33,7 +34,7 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
33
34
  * ```ts
34
35
  * import { createPersistentStore } from "dharma-core";
35
36
  *
36
- * const store = createPersistentStore("count", { count: 0 }, (set) => ({
37
+ * const store = createPersistentStore("count", { count: 0 }, ({ set }) => ({
37
38
  * increment: () => set((state) => ({ count: state.count + 1 })),
38
39
  * decrement: () => set((state) => ({ count: state.count - 1 })),
39
40
  * reset: () => set({ count: 0 }),
@@ -48,7 +49,7 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
48
49
  * const store = createPersistentStore(
49
50
  * "count",
50
51
  * { count: 0 },
51
- * (set) => ({
52
+ * ({ set }) => ({
52
53
  * increment: () => set((state) => ({ count: state.count + 1 })),
53
54
  * decrement: () => set((state) => ({ count: state.count - 1 })),
54
55
  * reset: () => set({ count: 0 }),
@@ -59,6 +60,5 @@ export type PersistentStoreOptions<TState extends object> = StoreOptions<TState>
59
60
  * },
60
61
  * );
61
62
  * ```
62
- * @group Core
63
63
  */
64
- export declare const createPersistentStore: <TState extends object, TActions extends object = Record<never, never>>(key: string, initialState: TState, defineActions: DefineActions<TState, TActions>, { storage: _storage, serializer, onAttach, onDetach, onChange, ...options }?: PersistentStoreOptions<TState>) => Store<TState, TActions>;
64
+ export declare const createPersistentStore: <TState extends object, TActions extends object = Record<never, never>>(options: PersistentStoreConfiguration<TState, TActions>) => Store<TState, TActions>;
@@ -10,7 +10,7 @@ import { createStore } from './createStore.js';
10
10
  * @param {string} key - A unique key to identify the store in storage.
11
11
  * @param {TState} initialState - The initial state of the store.
12
12
  * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.
13
- * @param {PersistentStoreOptions<TState>} [options] - Additional options for the persistent store.
13
+ * @param {PersistentStoreConfiguration<TState>} [options] - Additional options for the persistent store.
14
14
  *
15
15
  * @returns {Store<TState, TActions>} The created store.
16
16
  *
@@ -19,7 +19,7 @@ import { createStore } from './createStore.js';
19
19
  * ```ts
20
20
  * import { createPersistentStore } from "dharma-core";
21
21
  *
22
- * const store = createPersistentStore("count", { count: 0 }, (set) => ({
22
+ * const store = createPersistentStore("count", { count: 0 }, ({ set }) => ({
23
23
  * increment: () => set((state) => ({ count: state.count + 1 })),
24
24
  * decrement: () => set((state) => ({ count: state.count - 1 })),
25
25
  * reset: () => set({ count: 0 }),
@@ -34,7 +34,7 @@ import { createStore } from './createStore.js';
34
34
  * const store = createPersistentStore(
35
35
  * "count",
36
36
  * { count: 0 },
37
- * (set) => ({
37
+ * ({ set }) => ({
38
38
  * increment: () => set((state) => ({ count: state.count + 1 })),
39
39
  * decrement: () => set((state) => ({ count: state.count - 1 })),
40
40
  * reset: () => set({ count: 0 }),
@@ -45,12 +45,12 @@ import { createStore } from './createStore.js';
45
45
  * },
46
46
  * );
47
47
  * ```
48
- * @group Core
49
48
  */
50
- const createPersistentStore = (key, initialState, defineActions, { storage: _storage = "local", serializer = JSON, onAttach, onDetach, onChange, ...options } = {}) => {
49
+ const createPersistentStore = (options) => {
51
50
  if (typeof window === "undefined") {
52
- return createStore(initialState, defineActions, options);
51
+ return createStore(options);
53
52
  }
53
+ const { key, initialState, storage: _storage = "local", serializer = JSON, onAttach, onDetach, onChange, ...rest } = options;
54
54
  const storage = getStorage(_storage);
55
55
  const stateKey = `store_${key}`;
56
56
  const initialStateKey = `init_${key}`;
@@ -74,21 +74,22 @@ const createPersistentStore = (key, initialState, defineActions, { storage: _sto
74
74
  store.set(serializer.parse(currentSnapshot));
75
75
  }
76
76
  };
77
- const store = createStore(initialState, defineActions, {
78
- onAttach: (...args) => {
79
- onAttach?.(...args);
77
+ const store = createStore({
78
+ initialState,
79
+ onAttach: (ctx) => {
80
+ onAttach?.(ctx);
80
81
  updateState();
81
82
  window.addEventListener("focus", updateState);
82
83
  },
83
- onDetach: (...args) => {
84
- onDetach?.(...args);
84
+ onDetach: (ctx) => {
85
+ onDetach?.(ctx);
85
86
  window.removeEventListener("focus", updateState);
86
87
  },
87
- onChange: (state, ...args) => {
88
- onChange?.(state, ...args);
88
+ onChange: ({ state, ...ctx }) => {
89
+ onChange?.({ state, ...ctx });
89
90
  updateSnapshot(state);
90
91
  },
91
- ...options,
92
+ ...rest,
92
93
  });
93
94
  return store;
94
95
  };
@@ -1 +1 @@
1
- {"version":3,"file":"createPersistentStore.js","sources":["../../../src/lib/createPersistentStore.ts"],"sourcesContent":["import { createStore, DefineActions, Store, StoreOptions } from \"./createStore\";\n\nexport type StorageAPI = {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Serializer<T = any> = {\n stringify: (value: T) => string;\n parse: (value: string) => T;\n};\n\nexport type PersistentStoreOptions<TState extends object> =\n StoreOptions<TState> & {\n /** The storage to use for persisting the state. Defaults to local storage. */\n storage?: StorageAPI | \"local\" | \"session\";\n /** The serializer to use for storing the state. Defaults to JSON. */\n serializer?: Serializer<TState>;\n };\n\n/**\n * Creates a store that persists its state in local or session storage.\n * Defaults to local storage but this can be changed in the options.\n *\n * **Note:** The state needs to be serializable by whatever serializer you use. (JSON by default)\n * If you need something more versatile I would recommend using a library like [superjson](https://github.com/flightcontrolhq/superjson).\n *\n * @param {string} key - A unique key to identify the store in storage.\n * @param {TState} initialState - The initial state of the store.\n * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.\n * @param {PersistentStoreOptions<TState>} [options] - Additional options for the persistent store.\n *\n * @returns {Store<TState, TActions>} The created store.\n *\n * @example\n * Basic usage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n *\n * const store = createPersistentStore(\"count\", { 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 * @example\n * With superjson serialization and session storage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n * import superjson from \"superjson\";\n *\n * const store = createPersistentStore(\n * \"count\",\n * { count: 0 },\n * (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 * serializer: superjson,\n * storage: \"session\",\n * },\n * );\n * ```\n * @group Core\n */\nexport const createPersistentStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n key: string,\n initialState: TState,\n defineActions: DefineActions<TState, TActions>,\n {\n storage: _storage = \"local\",\n serializer = JSON,\n onAttach,\n onDetach,\n onChange,\n ...options\n }: PersistentStoreOptions<TState> = {},\n): Store<TState, TActions> => {\n if (typeof window === \"undefined\") {\n return createStore(initialState, defineActions, options);\n }\n\n const storage = getStorage(_storage);\n const stateKey = `store_${key}`;\n const initialStateKey = `init_${key}`;\n const initialStateSnapshot = storage.getItem(initialStateKey);\n const initialStateString = serializer.stringify(initialState);\n\n if (initialStateSnapshot !== initialStateString) {\n storage.setItem(initialStateKey, initialStateString);\n storage.removeItem(stateKey);\n }\n\n const updateSnapshot = (newState: TState) => {\n const currentSnapshot = storage.getItem(stateKey);\n const newSnapshot = serializer.stringify(newState);\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(stateKey, newSnapshot);\n }\n };\n\n const updateState = () => {\n const currentSnapshot = storage.getItem(stateKey);\n\n if (\n currentSnapshot &&\n currentSnapshot !== serializer.stringify(store.get())\n ) {\n store.set(serializer.parse(currentSnapshot));\n }\n };\n\n const store = createStore(initialState, defineActions, {\n onAttach: (...args) => {\n onAttach?.(...args);\n updateState();\n window.addEventListener(\"focus\", updateState);\n },\n onDetach: (...args) => {\n onDetach?.(...args);\n window.removeEventListener(\"focus\", updateState);\n },\n onChange: (state, ...args) => {\n onChange?.(state, ...args);\n updateSnapshot(state);\n },\n ...options,\n });\n\n return store;\n};\n\nconst getStorage = (storage: StorageAPI | \"local\" | \"session\"): StorageAPI => {\n switch (storage) {\n case \"local\":\n return localStorage;\n case \"session\":\n return sessionStorage;\n default:\n return storage;\n }\n};\n"],"names":[],"mappings":";;AAsBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CG;AACI,MAAM,qBAAqB,GAAG,CAInC,GAAW,EACX,YAAoB,EACpB,aAA8C,EAC9C,EACE,OAAO,EAAE,QAAQ,GAAG,OAAO,EAC3B,UAAU,GAAG,IAAI,EACjB,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,GAAG,OAAO,EAAA,GACwB,EAAE,KACX;AAC3B,IAAA,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,WAAW,CAAC,YAAY,EAAE,aAAa,EAAE,OAAO,CAAC;IAC1D;AAEA,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,QAAQ,CAAC;AACpC,IAAA,MAAM,QAAQ,GAAG,CAAA,MAAA,EAAS,GAAG,EAAE;AAC/B,IAAA,MAAM,eAAe,GAAG,CAAA,KAAA,EAAQ,GAAG,EAAE;IACrC,MAAM,oBAAoB,GAAG,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC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ClD,CAAC;AACD,QAAA,QAAQ,EAAE,CAAC,KAAK,EAAE,GAAG,IAAI,KAAI;AAC3B,YAAA,QAAQ,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC;YAC1B,cAAc,CAAC,KAAK,CAAC;QACvB,CAAC;AACD,QAAA,GAAG,OAAO;AACX,KAAA,CAAC;AAEF,IAAA,OAAO,KAAK;AACd;AAEA,MAAM,UAAU,GAAG,CAAC,OAAyC,KAAgB;IAC3E,QAAQ,OAAO;AACb,QAAA,KAAK,OAAO;AACV,YAAA,OAAO,YAAY;AACrB,QAAA,KAAK,SAAS;AACZ,YAAA,OAAO,cAAc;AACvB,QAAA;AACE,YAAA,OAAO,OAAO;;AAEpB,CAAC;;;;"}
1
+ {"version":3,"file":"createPersistentStore.js","sources":["../../../src/lib/createPersistentStore.ts"],"sourcesContent":["import { createStore, Store, StoreConfiguration } from \"./createStore\";\n\nexport type StorageAPI = {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type Serializer<T = any> = {\n stringify: (value: T) => string;\n parse: (value: string) => T;\n};\n\nexport type PersistentStoreConfiguration<\n TState extends object,\n TActions extends object,\n> = StoreConfiguration<TState, TActions> & {\n key: string;\n /** The storage to use for persisting the state. Defaults to local storage. */\n storage?: StorageAPI | \"local\" | \"session\";\n /** The serializer to use for storing the state. Defaults to JSON. */\n serializer?: Serializer<TState>;\n};\n\n/**\n * Creates a store that persists its state in local or session storage.\n * Defaults to local storage but this can be changed in the options.\n *\n * **Note:** The state needs to be serializable by whatever serializer you use. (JSON by default)\n * If you need something more versatile I would recommend using a library like [superjson](https://github.com/flightcontrolhq/superjson).\n *\n * @param {string} key - A unique key to identify the store in storage.\n * @param {TState} initialState - The initial state of the store.\n * @param {DefineActions<TState, TActions>} [defineActions] - A function to define actions for the store.\n * @param {PersistentStoreConfiguration<TState>} [options] - Additional options for the persistent store.\n *\n * @returns {Store<TState, TActions>} The created store.\n *\n * @example\n * Basic usage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n *\n * const store = createPersistentStore(\"count\", { 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 * @example\n * With superjson serialization and session storage:\n * ```ts\n * import { createPersistentStore } from \"dharma-core\";\n * import superjson from \"superjson\";\n *\n * const store = createPersistentStore(\n * \"count\",\n * { count: 0 },\n * ({ 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 * serializer: superjson,\n * storage: \"session\",\n * },\n * );\n * ```\n */\nexport const createPersistentStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n options: PersistentStoreConfiguration<TState, TActions>,\n): Store<TState, TActions> => {\n if (typeof window === \"undefined\") {\n return createStore(options);\n }\n\n const {\n key,\n initialState,\n storage: _storage = \"local\",\n serializer = JSON,\n onAttach,\n onDetach,\n onChange,\n ...rest\n } = options;\n\n const storage = getStorage(_storage);\n const stateKey = `store_${key}`;\n const initialStateKey = `init_${key}`;\n const initialStateSnapshot = storage.getItem(initialStateKey);\n const initialStateString = serializer.stringify(initialState);\n\n if (initialStateSnapshot !== initialStateString) {\n storage.setItem(initialStateKey, initialStateString);\n storage.removeItem(stateKey);\n }\n\n const updateSnapshot = (newState: TState) => {\n const currentSnapshot = storage.getItem(stateKey);\n const newSnapshot = serializer.stringify(newState);\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(stateKey, newSnapshot);\n }\n };\n\n const updateState = () => {\n const currentSnapshot = storage.getItem(stateKey);\n\n if (\n currentSnapshot &&\n currentSnapshot !== serializer.stringify(store.get())\n ) {\n store.set(serializer.parse(currentSnapshot));\n }\n };\n\n const store = createStore({\n initialState,\n onAttach: (ctx) => {\n onAttach?.(ctx);\n updateState();\n window.addEventListener(\"focus\", updateState);\n },\n onDetach: (ctx) => {\n onDetach?.(ctx);\n window.removeEventListener(\"focus\", updateState);\n },\n onChange: ({ state, ...ctx }) => {\n onChange?.({ state, ...ctx });\n updateSnapshot(state);\n },\n ...rest,\n });\n\n return store;\n};\n\nconst getStorage = (storage: StorageAPI | \"local\" | \"session\"): StorageAPI => {\n switch (storage) {\n case \"local\":\n return localStorage;\n case \"session\":\n return sessionStorage;\n default:\n return storage;\n }\n};\n"],"names":[],"mappings":";;AAyBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CG;AACI,MAAM,qBAAqB,GAAG,CAInC,OAAuD,KAC5B;AAC3B,IAAA,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;AACjC,QAAA,OAAO,WAAW,CAAC,OAAO,CAAC;IAC7B;IAEA,MAAM,EACJ,GAAG,EACH,YAAY,EACZ,OAAO,EAAE,QAAQ,GAAG,OAAO,EAC3B,UAAU,GAAG,IAAI,EACjB,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,GAAG,IAAI,EACR,GAAG,OAAO;AAEX,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,QAAQ,CAAC;AACpC,IAAA,MAAM,QAAQ,GAAG,CAAA,MAAA,EAAS,GAAG,EAAE;AAC/B,IAAA,MAAM,eAAe,GAAG,CAAA,KAAA,EAAQ,GAAG,EAAE;IACrC,MAAM,oBAAoB,GAAG,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC;IAC7D,MAAM,kBAAkB,GAAG,UAAU,CAAC,SAAS,CAAC,YAAY,CAAC;AAE7D,IAAA,IAAI,oBAAoB,KAAK,kBAAkB,EAAE;AAC/C,QAAA,OAAO,CAAC,OAAO,CAAC,eAAe,EAAE,kBAAkB,CAAC;AACpD,QAAA,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC;IAC9B;AAEA,IAAA,MAAM,cAAc,GAAG,CAAC,QAAgB,KA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A,KAAK,OAAO;AACV,YAAA,OAAO,YAAY;AACrB,QAAA,KAAK,SAAS;AACZ,YAAA,OAAO,cAAc;AACvB,QAAA;AACE,YAAA,OAAO,OAAO;;AAEpB,CAAC;;;;"}
@@ -1,19 +1,41 @@
1
+ type Listener<TState extends object> = (state: TState) => void;
2
+ export type SetState<TState extends object> = (stateModifier: StateModifier<TState>) => TState;
1
3
  export type Store<TState extends object, TActions extends object> = {
2
4
  /** Returns the current state of the store. */
3
5
  get: () => TState;
4
6
  /** Sets the state of the store. */
5
- set: (stateModifier: StateModifier<TState>) => TState;
7
+ set: SetState<TState>;
8
+ /** Resets the state of the store to its initial value. */
9
+ reset: () => TState;
6
10
  /** Actions that can modify the state of the store. */
7
11
  actions: TActions;
8
12
  /** Subscribes to changes in the state of the store. Returns an unsubscribe function. */
9
13
  subscribe: (listener: Listener<TState>) => () => void;
10
14
  };
11
- type Listener<TState extends object> = (state: TState) => void;
12
- export type StoreEventHandler<TState extends object> = (state: TState, set: SetState<TState>) => void;
13
- export type SetState<TState extends object> = (stateModifier: StateModifier<TState>) => TState;
15
+ export type EventHandlerContext<TState extends object> = {
16
+ /** The current state of the store. */
17
+ state: TState;
18
+ /** Sets the state of the store. */
19
+ set: SetState<TState>;
20
+ /** Resets the state of the store to its initial value. */
21
+ reset: () => TState;
22
+ };
23
+ export type StoreEventHandler<TState extends object> = (context: EventHandlerContext<TState>) => void;
14
24
  export type StateModifier<TState extends object> = Partial<TState> | ((state: TState) => Partial<TState>);
15
- export type DefineActions<TState extends object, TActions> = (set: (stateModifier: StateModifier<TState>) => TState, get: () => TState) => TActions;
16
- export type StoreOptions<TState extends object> = {
25
+ export type StateFunctions<TState extends object> = {
26
+ /** Returns the current state of the store. */
27
+ get: () => TState;
28
+ /** Sets the state of the store. */
29
+ set: SetState<TState>;
30
+ /** Resets the state of the store to its initial value. */
31
+ reset: () => TState;
32
+ };
33
+ export type DefineActions<TState extends object, TActions> = (stateFunctions: StateFunctions<TState>) => TActions;
34
+ export type StoreConfiguration<TState extends object, TActions extends object> = {
35
+ /** The initial state of the store. */
36
+ initialState: TState;
37
+ /** A function that defines actions that can modify the state. */
38
+ defineActions?: DefineActions<TState, TActions>;
17
39
  /** Invoked when the store is created. */
18
40
  onLoad?: StoreEventHandler<TState>;
19
41
  /** Invoked when the store is subscribed to. */
@@ -26,9 +48,7 @@ export type StoreOptions<TState extends object> = {
26
48
  /**
27
49
  * Creates a store with an initial state and actions that can modify the state.
28
50
  *
29
- * @param {TState} initialState - The initial state of the store.
30
- * @param {DefineActions<TState, TActions>} [defineActions] - A function that defines actions that can modify the state.
31
- * @param {StoreOptions<TState>} [options] - Additional options for the store.
51
+ * @param {StoreConfiguration<TState>} [config] - The configuration for the store.
32
52
  *
33
53
  * @returns {Store<TState, TActions>} The created store with state management methods.
34
54
  *
@@ -36,13 +56,14 @@ export type StoreOptions<TState extends object> = {
36
56
  * ```ts
37
57
  * import { createStore } from "dharma-core";
38
58
  *
39
- * const store = createStore({ count: 0 }, (set) => ({
40
- * increment: () => set((state) => ({ count: state.count + 1 })),
41
- * decrement: () => set((state) => ({ count: state.count - 1 })),
42
- * reset: () => set({ count: 0 }),
43
- * }));
59
+ * const store = createStore({
60
+ * initialState: { count: 0 },
61
+ * defineActions: ({ set }) => ({
62
+ * increment: () => set((state) => ({ count: state.count + 1 })),
63
+ * decrement: () => set((state) => ({ count: state.count - 1 })),
64
+ * }),
65
+ * });
44
66
  * ```
45
- * @group Core
46
67
  */
47
- export declare const createStore: <TState extends object, TActions extends object = Record<never, never>>(initialState: TState, defineActions: DefineActions<TState, TActions>, { onLoad, onAttach, onDetach, onChange }?: StoreOptions<TState>) => Store<TState, TActions>;
68
+ export declare const createStore: <TState extends object, TActions extends object = Record<never, never>>(config: StoreConfiguration<TState, TActions>) => Store<TState, TActions>;
48
69
  export {};
@@ -3,9 +3,7 @@ import { merge } from './merge.js';
3
3
  /**
4
4
  * Creates a store with an initial state and actions that can modify the state.
5
5
  *
6
- * @param {TState} initialState - The initial state of the store.
7
- * @param {DefineActions<TState, TActions>} [defineActions] - A function that defines actions that can modify the state.
8
- * @param {StoreOptions<TState>} [options] - Additional options for the store.
6
+ * @param {StoreConfiguration<TState>} [config] - The configuration for the store.
9
7
  *
10
8
  * @returns {Store<TState, TActions>} The created store with state management methods.
11
9
  *
@@ -13,15 +11,17 @@ import { merge } from './merge.js';
13
11
  * ```ts
14
12
  * import { createStore } from "dharma-core";
15
13
  *
16
- * const store = createStore({ count: 0 }, (set) => ({
17
- * increment: () => set((state) => ({ count: state.count + 1 })),
18
- * decrement: () => set((state) => ({ count: state.count - 1 })),
19
- * reset: () => set({ count: 0 }),
20
- * }));
14
+ * const store = createStore({
15
+ * initialState: { count: 0 },
16
+ * defineActions: ({ set }) => ({
17
+ * increment: () => set((state) => ({ count: state.count + 1 })),
18
+ * decrement: () => set((state) => ({ count: state.count - 1 })),
19
+ * }),
20
+ * });
21
21
  * ```
22
- * @group Core
23
22
  */
24
- const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach, onChange } = {}) => {
23
+ const createStore = (config) => {
24
+ const { initialState, defineActions, onLoad, onAttach, onDetach, onChange } = config;
25
25
  let state = initialState;
26
26
  const listeners = new Set();
27
27
  const get = () => state;
@@ -29,8 +29,9 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
29
29
  state = merge(state, stateModifier);
30
30
  return state;
31
31
  };
32
+ const resetSilently = () => setSilently(initialState);
32
33
  const dispatch = () => {
33
- onChange?.(state, setSilently);
34
+ onChange?.({ state, set: setSilently, reset: resetSilently });
34
35
  listeners.forEach((listener) => listener(state));
35
36
  };
36
37
  const set = (stateModifier) => {
@@ -38,24 +39,32 @@ const createStore = (initialState, defineActions, { onLoad, onAttach, onDetach,
38
39
  dispatch();
39
40
  return state;
40
41
  };
42
+ const reset = () => {
43
+ resetSilently();
44
+ dispatch();
45
+ return state;
46
+ };
41
47
  const subscribe = (listener) => {
42
- listener(state);
43
48
  if (listeners.size === 0) {
44
- onAttach?.(state, set);
49
+ onAttach?.({ state, set, reset });
45
50
  }
51
+ listener(state);
46
52
  listeners.add(listener);
47
53
  return () => {
48
54
  listeners.delete(listener);
49
55
  if (listeners.size === 0) {
50
- onDetach?.(state, set);
56
+ onDetach?.({ state, set, reset });
51
57
  }
52
58
  };
53
59
  };
54
- const actions = defineActions ? defineActions(set, get) : {};
55
- onLoad?.(state, set);
60
+ const actions = defineActions
61
+ ? defineActions({ set, get, reset })
62
+ : {};
63
+ onLoad?.({ state, set, reset });
56
64
  return {
57
65
  get,
58
66
  set,
67
+ reset,
59
68
  subscribe,
60
69
  actions,
61
70
  };
@@ -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<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,IA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],"sourcesContent":["import { merge } from \"./merge\";\n\ntype Listener<TState extends object> = (state: TState) => void;\n\nexport type SetState<TState extends object> = (\n stateModifier: StateModifier<TState>,\n) => TState;\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: SetState<TState>;\n /** Resets the state of the store to its initial value. */\n reset: () => 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\nexport type EventHandlerContext<TState extends object> = {\n /** The current state of the store. */\n state: TState;\n /** Sets the state of the store. */\n set: SetState<TState>;\n /** Resets the state of the store to its initial value. */\n reset: () => TState;\n};\n\nexport type StoreEventHandler<TState extends object> = (\n context: EventHandlerContext<TState>,\n) => void;\n\nexport type StateModifier<TState extends object> =\n | Partial<TState>\n | ((state: TState) => Partial<TState>);\n\nexport type StateFunctions<TState extends object> = {\n /** Returns the current state of the store. */\n get: () => TState;\n /** Sets the state of the store. */\n set: SetState<TState>;\n /** Resets the state of the store to its initial value. */\n reset: () => TState;\n};\n\nexport type DefineActions<TState extends object, TActions> = (\n stateFunctions: StateFunctions<TState>,\n) => TActions;\n\nexport type StoreConfiguration<\n TState extends object,\n TActions extends object,\n> = {\n /** The initial state of the store. */\n initialState: TState;\n /** A function that defines actions that can modify the state. */\n defineActions?: DefineActions<TState, TActions>;\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 {StoreConfiguration<TState>} [config] - The configuration 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({\n * initialState: { count: 0 },\n * defineActions: ({ set }) => ({\n * increment: () => set((state) => ({ count: state.count + 1 })),\n * decrement: () => set((state) => ({ count: state.count - 1 })),\n * }),\n * });\n * ```\n */\nexport const createStore = <\n TState extends object,\n TActions extends object = Record<never, never>,\n>(\n config: StoreConfiguration<TState, TActions>,\n): Store<TState, TActions> => {\n const { initialState, defineActions, onLoad, onAttach, onDetach, onChange } =\n config;\n\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 resetSilently = () => setSilently(initialState);\n\n const dispatch = () => {\n onChange?.({ state, set: setSilently, reset: resetSilently });\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 reset = () => {\n resetSilently();\n dispatch();\n return state;\n };\n\n const subscribe = (listener: Listener<TState>) => {\n if (listeners.size === 0) {\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 onDetach?.({ state, set, reset });\n }\n };\n };\n\n const actions = defineActions\n ? defineActions({ set, get, reset })\n : ({} as TActions);\n\n onLoad?.({ state, set, reset });\n\n return {\n get,\n set,\n reset,\n subscribe,\n actions,\n };\n};\n"],"names":[],"mappings":";;AAqEA;;;;;;;;;;;;;;;;;;;AAmBG;AACI,MAAM,WAAW,GAAG,CAIzB,MAA4C,KACjB;AAC3B,IAAA,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,GACzE,MAAM;IAER,IAAI,KAAK,GAAG,YAAY;AACxB,IAAA,MAAM,SAAS,GAAG,IAAI,GAAG,EAAoB;AAE7C,IAAA,MAAM,GAAG,GAAG,MAAM,KAAK;AAEvB,IAAA,MAAM,WA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GAAG,EAAE,KAAK,EAAE;UAChC,EAAe;IAEpB,MAAM,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IAE/B,OAAO;QACL,GAAG;QACH,GAAG;QACH,KAAK;QACL,SAAS;QACT,OAAO;KACR;AACH;;;;"}
@@ -11,14 +11,14 @@ import { StateModifier } from "./createStore";
11
11
  * ```ts
12
12
  * import { createStore, merge, StateModifier } from "dharma-core";
13
13
  *
14
- * const store = createStore(
15
- * {
14
+ * const store = createStore({
15
+ * initialState: {
16
16
  * counter: {
17
17
  * count: 0,
18
18
  * },
19
19
  * // ...
20
20
  * },
21
- * (set) => {
21
+ * defineActions: ({ set }) => {
22
22
  * const setCounter = (counter: StateModifier<{ count: number }>) =>
23
23
  * set((state) => ({
24
24
  * counter: merge(state.counter, counter),
@@ -31,8 +31,7 @@ import { StateModifier } from "./createStore";
31
31
  * // ...
32
32
  * };
33
33
  * },
34
- * );
34
+ * });
35
35
  * ```
36
- * @group Utilities
37
36
  */
38
37
  export declare const merge: <T extends object>(currentState: T, stateModifier: StateModifier<T>) => T;
@@ -10,14 +10,14 @@
10
10
  * ```ts
11
11
  * import { createStore, merge, StateModifier } from "dharma-core";
12
12
  *
13
- * const store = createStore(
14
- * {
13
+ * const store = createStore({
14
+ * initialState: {
15
15
  * counter: {
16
16
  * count: 0,
17
17
  * },
18
18
  * // ...
19
19
  * },
20
- * (set) => {
20
+ * defineActions: ({ set }) => {
21
21
  * const setCounter = (counter: StateModifier<{ count: number }>) =>
22
22
  * set((state) => ({
23
23
  * counter: merge(state.counter, counter),
@@ -30,9 +30,8 @@
30
30
  * // ...
31
31
  * };
32
32
  * },
33
- * );
33
+ * });
34
34
  * ```
35
- * @group Utilities
36
35
  */
37
36
  const merge = (currentState, stateModifier) => {
38
37
  const newState = typeof stateModifier === "function"
@@ -1 +1 @@
1
- {"version":3,"file":"merge.js","sources":["../../../src/lib/merge.ts"],"sourcesContent":["import { StateModifier } from \"./createStore\";\n\n/**\n * Merges the current state with a state modifier.\n * Useful for creating custom set functions.\n *\n * @param {T} stateModifier - A function or object that modifies the state.\n * @param {StateModifier<T>} stateModifier - The current state object.\n * @returns {T} The new state object, which is a combination of the current state and the state modifier.\n *\n * @example\n * ```ts\n * import { createStore, merge, StateModifier } from \"dharma-core\";\n *\n * const store = createStore(\n * {\n * counter: {\n * count: 0,\n * },\n * // ...\n * },\n * (set) => {\n * const setCounter = (counter: StateModifier<{ count: number }>) =>\n * set((state) => ({\n * counter: merge(state.counter, counter),\n * }));\n *\n * return {\n * increment: () => setCounter((state) => ({ count: state.count + 1 })),\n * decrement: () => setCounter((state) => ({ count: state.count - 1 })),\n * reset: () => setCounter({ count: 0 }),\n * // ...\n * };\n * },\n * );\n * ```\n * @group Utilities\n */\nexport const merge = <T extends object>(\n currentState: T,\n stateModifier: StateModifier<T>,\n): T => {\n const newState =\n typeof stateModifier === \"function\"\n ? stateModifier(currentState)\n : stateModifier;\n return { ...currentState, ...newState };\n};\n"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCG;MACU,KAAK,GAAG,CACnB,YAAe,EACf,aAA+B,KAC1B;AACL,IAAA,MAAM,QAAQ,GACZ,OAAO,aAAa,KAAK;AACvB,UAAE,aAAa,CAAC,YAAY;UAC1B,aAAa;AACnB,IAAA,OAAO,EAAE,GAAG,YAAY,EAAE,GAAG,QAAQ,EAAE;AACzC;;;;"}
1
+ {"version":3,"file":"merge.js","sources":["../../../src/lib/merge.ts"],"sourcesContent":["import { StateModifier } from \"./createStore\";\n\n/**\n * Merges the current state with a state modifier.\n * Useful for creating custom set functions.\n *\n * @param {T} stateModifier - A function or object that modifies the state.\n * @param {StateModifier<T>} stateModifier - The current state object.\n * @returns {T} The new state object, which is a combination of the current state and the state modifier.\n *\n * @example\n * ```ts\n * import { createStore, merge, StateModifier } from \"dharma-core\";\n *\n * const store = createStore({\n * initialState: {\n * counter: {\n * count: 0,\n * },\n * // ...\n * },\n * defineActions: ({ set }) => {\n * const setCounter = (counter: StateModifier<{ count: number }>) =>\n * set((state) => ({\n * counter: merge(state.counter, counter),\n * }));\n *\n * return {\n * increment: () => setCounter((state) => ({ count: state.count + 1 })),\n * decrement: () => setCounter((state) => ({ count: state.count - 1 })),\n * reset: () => setCounter({ count: 0 }),\n * // ...\n * };\n * },\n * });\n * ```\n */\nexport const merge = <T extends object>(\n currentState: T,\n stateModifier: StateModifier<T>,\n): T => {\n const newState =\n typeof stateModifier === \"function\"\n ? stateModifier(currentState)\n : stateModifier;\n return { ...currentState, ...newState };\n};\n"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCG;MACU,KAAK,GAAG,CACnB,YAAe,EACf,aAA+B,KAC1B;AACL,IAAA,MAAM,QAAQ,GACZ,OAAO,aAAa,KAAK;AACvB,UAAE,aAAa,CAAC,YAAY;UAC1B,aAAa;AACnB,IAAA,OAAO,EAAE,GAAG,YAAY,EAAE,GAAG,QAAQ,EAAE;AACzC;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dharma-core",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "author": "Frans Ekman <fransedvinekman@gmail.com>",
5
5
  "license": "MIT",
6
6
  "description": "My package description",