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