dharma-core 0.12.0 → 0.13.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,6 +1,6 @@
1
1
  import { SetState, StoreConfig } from "../types/types";
2
2
  export declare const createStorageAdapter: <TState, TActions>(config: StoreConfig<TState, TActions>, get: () => TState, set: SetState<TState>) => {
3
- onLoad: () => Promise<void>;
3
+ onLoad: () => void;
4
4
  onChange: (newState: TState) => Promise<void>;
5
5
  onAttach: () => void;
6
6
  onDetach: () => void;
@@ -12,24 +12,7 @@ const createStorageAdapter = (config, get, set) => {
12
12
  warn.warn("No storage provider found. If this happened during SSR, you can safely ignore this warning.");
13
13
  return null;
14
14
  }
15
- const { key, serializer = JSON, initialState } = config;
16
- const initKey = `init_${key}`;
17
- const onLoad = async () => {
18
- try {
19
- let initialStateSnapshot = storage.getItem(initKey);
20
- const initialStateString = serializer.stringify(initialState);
21
- if (initialStateSnapshot instanceof Promise) {
22
- initialStateSnapshot = await initialStateSnapshot;
23
- }
24
- if (initialStateSnapshot !== initialStateString) {
25
- storage.setItem(initKey, initialStateString);
26
- storage.removeItem(key);
27
- }
28
- }
29
- catch {
30
- warn.warn("Failed to initialize snapshots. If this happened during SSR, you can safely ignore this warning.");
31
- }
32
- };
15
+ const { key, serializer = JSON, syncWithStorageOn = "load" } = config;
33
16
  const syncWithSnapshot = async () => {
34
17
  try {
35
18
  let currentSnapshot = storage.getItem(key);
@@ -40,12 +23,20 @@ const createStorageAdapter = (config, get, set) => {
40
23
  set(serializer.parse(currentSnapshot));
41
24
  }
42
25
  }
43
- catch {
44
- warn.warn("Failed to update state from snapshot");
26
+ catch (e) {
27
+ warn.warn("Failed to sync state with snapshot. If this happened during SSR, you can safely ignore this warning.");
28
+ console.error(e);
29
+ }
30
+ };
31
+ const onLoad = () => {
32
+ if (syncWithStorageOn === "load") {
33
+ syncWithSnapshot();
45
34
  }
46
35
  };
47
36
  const onAttach = () => {
48
- syncWithSnapshot();
37
+ if (syncWithStorageOn === "attach") {
38
+ syncWithSnapshot();
39
+ }
49
40
  if (IS_BROWSER) {
50
41
  window.addEventListener("focus", syncWithSnapshot);
51
42
  }
@@ -61,8 +52,9 @@ const createStorageAdapter = (config, get, set) => {
61
52
  storage.setItem(key, newSnapshot);
62
53
  }
63
54
  }
64
- catch {
55
+ catch (e) {
65
56
  warn.warn("Failed to update snapshot");
57
+ console.error(e);
66
58
  }
67
59
  };
68
60
  const onDetach = () => {
@@ -1 +1 @@
1
- {"version":3,"file":"createStorageAdapter.js","sources":["../../../src/internal/createStorageAdapter.ts"],"sourcesContent":["import { SetState, StorageAPI, StoreConfig } from \"../types/types\";\nimport { warn } from \"./warn\";\n\nexport const createStorageAdapter = <TState, TActions>(\n config: StoreConfig<TState, TActions>,\n get: () => TState,\n set: SetState<TState>,\n) => {\n if (!config.persist) {\n return null;\n }\n\n const IS_BROWSER = typeof window !== \"undefined\";\n\n const storage =\n config.storage ?? (IS_BROWSER ? (localStorage as StorageAPI) : undefined);\n\n if (!storage) {\n warn(\n \"No storage provider found. If this happened during SSR, you can safely ignore this warning.\",\n );\n return null;\n }\n\n const { key, serializer = JSON, initialState } = config;\n const initKey = `init_${key}`;\n\n const onLoad = async () => {\n try {\n let initialStateSnapshot = storage.getItem(initKey);\n const initialStateString = serializer.stringify(initialState);\n\n if (initialStateSnapshot instanceof Promise) {\n initialStateSnapshot = await initialStateSnapshot;\n }\n\n if (initialStateSnapshot !== initialStateString) {\n storage.setItem(initKey, initialStateString);\n storage.removeItem(key);\n }\n } catch {\n warn(\n \"Failed to initialize snapshots. If this happened during SSR, you can safely ignore this warning.\",\n );\n }\n };\n\n const syncWithSnapshot = async () => {\n try {\n let currentSnapshot = storage.getItem(key);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (currentSnapshot && currentSnapshot !== serializer.stringify(get())) {\n set(serializer.parse(currentSnapshot));\n }\n } catch {\n warn(\"Failed to update state from snapshot\");\n }\n };\n\n const onAttach = () => {\n syncWithSnapshot();\n\n if (IS_BROWSER) {\n window.addEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n const onChange = async (newState: TState) => {\n try {\n let currentSnapshot = storage.getItem(key);\n const newSnapshot = serializer.stringify(newState);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(key, newSnapshot);\n }\n } catch {\n warn(\"Failed to update snapshot\");\n }\n };\n\n const onDetach = () => {\n if (IS_BROWSER) {\n window.removeEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n return {\n onLoad,\n onChange,\n onAttach,\n onDetach,\n };\n};\n"],"names":["warn"],"mappings":";;;;AAGO,MAAM,oBAAoB,GAAG,CAClC,MAAqC,EACrC,GAAiB,EACjB,GAAqB,KACnB;AACF,IAAA,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;AACnB,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW;AAEhD,IAAA,MAAM,OAAO,GACX,MAAM,CAAC,OAAO,KAAK,UAAU,GAAI,YAA2B,GAAG,SAAS,CAAC;IAE3E,IAAI,CAAC,OAAO,EAAE;QACZA,SAAI,CACF,6FAA6F,CAC9F;AACD,QAAA,OAAO,IAAI;IACb;IAEA,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,EAAE,YAAY,EAAE,GAAG,MAAM;AACvD,IAAA,MAAM,OAAO,GAAG,CAAA,KAAA,EAAQ,GAAG,EAAE;AAE7B,IAAA,MAAM,MAAM,GAAG,YAAW;AACxB,QAAA,IAAI;YACF,IAAI,oBAAoB,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;YACnD,MAAM,kBAAkB,GAAG,UAAU,CAAC,SAAS,CAAC,YAAY,CAAC;AAE7D,YAAA,IAAI,oBAAoB,YAAY,OAAO,EAAE;gBAC3C,oBAAoB,GAAG,M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AC;YACnC;QACF;AAAE,QAAA,MAAM;YACNA,SAAI,CAAC,2BAA2B,CAAC;QACnC;AACF,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;QACpB,IAAI,UAAU,EAAE;AACd,YAAA,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC;QACvD;AACF,IAAA,CAAC;IAED,OAAO;QACL,MAAM;QACN,QAAQ;QACR,QAAQ;QACR,QAAQ;KACT;AACH;;;;"}
1
+ {"version":3,"file":"createStorageAdapter.js","sources":["../../../src/internal/createStorageAdapter.ts"],"sourcesContent":["import { SetState, StorageAPI, StoreConfig } from \"../types/types\";\nimport { warn } from \"./warn\";\n\nexport const createStorageAdapter = <TState, TActions>(\n config: StoreConfig<TState, TActions>,\n get: () => TState,\n set: SetState<TState>,\n) => {\n if (!config.persist) {\n return null;\n }\n\n const IS_BROWSER = typeof window !== \"undefined\";\n\n const storage =\n config.storage ?? (IS_BROWSER ? (localStorage as StorageAPI) : undefined);\n\n if (!storage) {\n warn(\n \"No storage provider found. If this happened during SSR, you can safely ignore this warning.\",\n );\n return null;\n }\n\n const { key, serializer = JSON, syncWithStorageOn = \"load\" } = config;\n\n const syncWithSnapshot = async () => {\n try {\n let currentSnapshot = storage.getItem(key);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (currentSnapshot && currentSnapshot !== serializer.stringify(get())) {\n set(serializer.parse(currentSnapshot));\n }\n } catch (e) {\n warn(\n \"Failed to sync state with snapshot. If this happened during SSR, you can safely ignore this warning.\",\n );\n console.error(e);\n }\n };\n\n const onLoad = () => {\n if (syncWithStorageOn === \"load\") {\n syncWithSnapshot();\n }\n };\n\n const onAttach = () => {\n if (syncWithStorageOn === \"attach\") {\n syncWithSnapshot();\n }\n\n if (IS_BROWSER) {\n window.addEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n const onChange = async (newState: TState) => {\n try {\n let currentSnapshot = storage.getItem(key);\n const newSnapshot = serializer.stringify(newState);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(key, newSnapshot);\n }\n } catch (e) {\n warn(\"Failed to update snapshot\");\n console.error(e);\n }\n };\n\n const onDetach = () => {\n if (IS_BROWSER) {\n window.removeEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n return {\n onLoad,\n onChange,\n onAttach,\n onDetach,\n };\n};\n"],"names":["warn"],"mappings":";;;;AAGO,MAAM,oBAAoB,GAAG,CAClC,MAAqC,EACrC,GAAiB,EACjB,GAAqB,KACnB;AACF,IAAA,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;AACnB,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW;AAEhD,IAAA,MAAM,OAAO,GACX,MAAM,CAAC,OAAO,KAAK,UAAU,GAAI,YAA2B,GAAG,SAAS,CAAC;IAE3E,IAAI,CAAC,OAAO,EAAE;QACZA,SAAI,CACF,6FAA6F,CAC9F;AACD,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,EAAE,iBAAiB,GAAG,MAAM,EAAE,GAAG,MAAM;AAErE,IAAA,MAAM,gBAAgB,GAAG,YAAW;AAClC,QAAA,IAAI;YACF,IAAI,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC;AAE1C,YAAA,IAAI,eAAe,YAAY,OAAO,EAAE;gBACtC,eAAe,GAAG,MAAM,eAAe;YACzC;AAEA,YAAA,IAAI,eAAe,IAAI,eAAe,KAAK,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,EAAE;gBACtE,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;YACxC;QACF;QAAE,OAAO,CAAC,EAAE;YACVA,SAAI,CACF,sGAAsG,CACvG;AACD,YAAA,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QAClB;AACF,IAAA,CAAC;IAED,MAAM,MAAM,GAAG,MAAK;AAClB,QAAA,IAAI,iBAAiB,KAAK,MAAM,EAAE;AAChC,YAAA,gBAAgB,EAAE;QACpB;AACF,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;AACpB,QAAA,IAAI,iBAAiB,KAAK,QAAQ,EAAE;AAClC,YAAA,gBAAgB,EAAE;QACpB;QAEA,IAAI,UAAU,EAAE;AACd,YAAA,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,CAAC;QACpD;AACF,IAAA,CAAC;AAED,IAAA,MAAM,QAAQ,GAAG,OAAO,QAAgB,KAAI;AAC1C,QAAA,IAAI;YACF,IAAI,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC;YAC1C,MAAM,WAAW,GAAG,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC;AAElD,YAAA,IAAI,eAAe,YAAY,OAAO,EAAE;gBACtC,eAAe,GAAG,MAAM,eAAe;YACzC;AAEA,YAAA,IAAI,WAAW,KAAK,eAAe,EAAE;AACnC,gBAAA,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,WAAW,CAAC;YACnC;QACF;QAAE,OAAO,CAAC,EAAE;YACVA,SAAI,CAAC,2BAA2B,CAAC;AACjC,YAAA,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QAClB;AACF,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;QACpB,IAAI,UAAU,EAAE;AACd,YAAA,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC;QACvD;AACF,IAAA,CAAC;IAED,OAAO;QACL,MAAM;QACN,QAAQ;QACR,QAAQ;QACR,QAAQ;KACT;AACH;;;;"}
@@ -71,7 +71,6 @@ export type MaybePromise<T> = T | Promise<T>;
71
71
  export type StorageAPI = {
72
72
  getItem: (key: string) => MaybePromise<string | null>;
73
73
  setItem: (key: string, value: string) => MaybePromise<void>;
74
- removeItem: (key: string) => MaybePromise<void>;
75
74
  };
76
75
  export type Serializer<T = any> = {
77
76
  stringify: (value: T) => string;
@@ -90,5 +89,11 @@ export type PersistConfig<TState> = {
90
89
  storage?: StorageAPI;
91
90
  /** The serializer to use for storing the state. Defaults to JSON. */
92
91
  serializer?: Serializer<TState>;
92
+ /**
93
+ * When to initially synchronize the store's state with the storage. Defaults to "load".
94
+ *
95
+ * Set to "attach" when using SSR to avoid hydration errors.
96
+ */
97
+ syncWithStorageOn?: "load" | "attach";
93
98
  };
94
99
  export type StoreConfig<TState, TActions> = BaseConfig<TState, TActions> & (PersistConfig<TState> | NoPersistConfig);
@@ -1,6 +1,6 @@
1
1
  import { SetState, StoreConfig } from "../types/types";
2
2
  export declare const createStorageAdapter: <TState, TActions>(config: StoreConfig<TState, TActions>, get: () => TState, set: SetState<TState>) => {
3
- onLoad: () => Promise<void>;
3
+ onLoad: () => void;
4
4
  onChange: (newState: TState) => Promise<void>;
5
5
  onAttach: () => void;
6
6
  onDetach: () => void;
@@ -10,24 +10,7 @@ const createStorageAdapter = (config, get, set) => {
10
10
  warn("No storage provider found. If this happened during SSR, you can safely ignore this warning.");
11
11
  return null;
12
12
  }
13
- const { key, serializer = JSON, initialState } = config;
14
- const initKey = `init_${key}`;
15
- const onLoad = async () => {
16
- try {
17
- let initialStateSnapshot = storage.getItem(initKey);
18
- const initialStateString = serializer.stringify(initialState);
19
- if (initialStateSnapshot instanceof Promise) {
20
- initialStateSnapshot = await initialStateSnapshot;
21
- }
22
- if (initialStateSnapshot !== initialStateString) {
23
- storage.setItem(initKey, initialStateString);
24
- storage.removeItem(key);
25
- }
26
- }
27
- catch {
28
- warn("Failed to initialize snapshots. If this happened during SSR, you can safely ignore this warning.");
29
- }
30
- };
13
+ const { key, serializer = JSON, syncWithStorageOn = "load" } = config;
31
14
  const syncWithSnapshot = async () => {
32
15
  try {
33
16
  let currentSnapshot = storage.getItem(key);
@@ -38,12 +21,20 @@ const createStorageAdapter = (config, get, set) => {
38
21
  set(serializer.parse(currentSnapshot));
39
22
  }
40
23
  }
41
- catch {
42
- warn("Failed to update state from snapshot");
24
+ catch (e) {
25
+ warn("Failed to sync state with snapshot. If this happened during SSR, you can safely ignore this warning.");
26
+ console.error(e);
27
+ }
28
+ };
29
+ const onLoad = () => {
30
+ if (syncWithStorageOn === "load") {
31
+ syncWithSnapshot();
43
32
  }
44
33
  };
45
34
  const onAttach = () => {
46
- syncWithSnapshot();
35
+ if (syncWithStorageOn === "attach") {
36
+ syncWithSnapshot();
37
+ }
47
38
  if (IS_BROWSER) {
48
39
  window.addEventListener("focus", syncWithSnapshot);
49
40
  }
@@ -59,8 +50,9 @@ const createStorageAdapter = (config, get, set) => {
59
50
  storage.setItem(key, newSnapshot);
60
51
  }
61
52
  }
62
- catch {
53
+ catch (e) {
63
54
  warn("Failed to update snapshot");
55
+ console.error(e);
64
56
  }
65
57
  };
66
58
  const onDetach = () => {
@@ -1 +1 @@
1
- {"version":3,"file":"createStorageAdapter.js","sources":["../../../src/internal/createStorageAdapter.ts"],"sourcesContent":["import { SetState, StorageAPI, StoreConfig } from \"../types/types\";\nimport { warn } from \"./warn\";\n\nexport const createStorageAdapter = <TState, TActions>(\n config: StoreConfig<TState, TActions>,\n get: () => TState,\n set: SetState<TState>,\n) => {\n if (!config.persist) {\n return null;\n }\n\n const IS_BROWSER = typeof window !== \"undefined\";\n\n const storage =\n config.storage ?? (IS_BROWSER ? (localStorage as StorageAPI) : undefined);\n\n if (!storage) {\n warn(\n \"No storage provider found. If this happened during SSR, you can safely ignore this warning.\",\n );\n return null;\n }\n\n const { key, serializer = JSON, initialState } = config;\n const initKey = `init_${key}`;\n\n const onLoad = async () => {\n try {\n let initialStateSnapshot = storage.getItem(initKey);\n const initialStateString = serializer.stringify(initialState);\n\n if (initialStateSnapshot instanceof Promise) {\n initialStateSnapshot = await initialStateSnapshot;\n }\n\n if (initialStateSnapshot !== initialStateString) {\n storage.setItem(initKey, initialStateString);\n storage.removeItem(key);\n }\n } catch {\n warn(\n \"Failed to initialize snapshots. If this happened during SSR, you can safely ignore this warning.\",\n );\n }\n };\n\n const syncWithSnapshot = async () => {\n try {\n let currentSnapshot = storage.getItem(key);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (currentSnapshot && currentSnapshot !== serializer.stringify(get())) {\n set(serializer.parse(currentSnapshot));\n }\n } catch {\n warn(\"Failed to update state from snapshot\");\n }\n };\n\n const onAttach = () => {\n syncWithSnapshot();\n\n if (IS_BROWSER) {\n window.addEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n const onChange = async (newState: TState) => {\n try {\n let currentSnapshot = storage.getItem(key);\n const newSnapshot = serializer.stringify(newState);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(key, newSnapshot);\n }\n } catch {\n warn(\"Failed to update snapshot\");\n }\n };\n\n const onDetach = () => {\n if (IS_BROWSER) {\n window.removeEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n return {\n onLoad,\n onChange,\n onAttach,\n onDetach,\n };\n};\n"],"names":[],"mappings":";;AAGO,MAAM,oBAAoB,GAAG,CAClC,MAAqC,EACrC,GAAiB,EACjB,GAAqB,KACnB;AACF,IAAA,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;AACnB,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW;AAEhD,IAAA,MAAM,OAAO,GACX,MAAM,CAAC,OAAO,KAAK,UAAU,GAAI,YAA2B,GAAG,SAAS,CAAC;IAE3E,IAAI,CAAC,OAAO,EAAE;QACZ,IAAI,CACF,6FAA6F,CAC9F;AACD,QAAA,OAAO,IAAI;IACb;IAEA,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,EAAE,YAAY,EAAE,GAAG,MAAM;AACvD,IAAA,MA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AI,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC;YAC1C,MAAM,WAAW,GAAG,UAAU,CAAC,SAAS,CAAC,QAAQ,CAAC;AAElD,YAAA,IAAI,eAAe,YAAY,OAAO,EAAE;gBACtC,eAAe,GAAG,MAAM,eAAe;YACzC;AAEA,YAAA,IAAI,WAAW,KAAK,eAAe,EAAE;AACnC,gBAAA,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,WAAW,CAAC;YACnC;QACF;AAAE,QAAA,MAAM;YACN,IAAI,CAAC,2BAA2B,CAAC;QACnC;AACF,IAAA,CAAC;IAED,MAAM,QAAQ,GAAG,MAAK;QACpB,IAAI,UAAU,EAAE;AACd,YAAA,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC;QACvD;AACF,IAAA,CAAC;IAED,OAAO;QACL,MAAM;QACN,QAAQ;QACR,QAAQ;QACR,QAAQ;KACT;AACH;;;;"}
1
+ {"version":3,"file":"createStorageAdapter.js","sources":["../../../src/internal/createStorageAdapter.ts"],"sourcesContent":["import { SetState, StorageAPI, StoreConfig } from \"../types/types\";\nimport { warn } from \"./warn\";\n\nexport const createStorageAdapter = <TState, TActions>(\n config: StoreConfig<TState, TActions>,\n get: () => TState,\n set: SetState<TState>,\n) => {\n if (!config.persist) {\n return null;\n }\n\n const IS_BROWSER = typeof window !== \"undefined\";\n\n const storage =\n config.storage ?? (IS_BROWSER ? (localStorage as StorageAPI) : undefined);\n\n if (!storage) {\n warn(\n \"No storage provider found. If this happened during SSR, you can safely ignore this warning.\",\n );\n return null;\n }\n\n const { key, serializer = JSON, syncWithStorageOn = \"load\" } = config;\n\n const syncWithSnapshot = async () => {\n try {\n let currentSnapshot = storage.getItem(key);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (currentSnapshot && currentSnapshot !== serializer.stringify(get())) {\n set(serializer.parse(currentSnapshot));\n }\n } catch (e) {\n warn(\n \"Failed to sync state with snapshot. If this happened during SSR, you can safely ignore this warning.\",\n );\n console.error(e);\n }\n };\n\n const onLoad = () => {\n if (syncWithStorageOn === \"load\") {\n syncWithSnapshot();\n }\n };\n\n const onAttach = () => {\n if (syncWithStorageOn === \"attach\") {\n syncWithSnapshot();\n }\n\n if (IS_BROWSER) {\n window.addEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n const onChange = async (newState: TState) => {\n try {\n let currentSnapshot = storage.getItem(key);\n const newSnapshot = serializer.stringify(newState);\n\n if (currentSnapshot instanceof Promise) {\n currentSnapshot = await currentSnapshot;\n }\n\n if (newSnapshot !== currentSnapshot) {\n storage.setItem(key, newSnapshot);\n }\n } catch (e) {\n warn(\"Failed to update snapshot\");\n console.error(e);\n }\n };\n\n const onDetach = () => {\n if (IS_BROWSER) {\n window.removeEventListener(\"focus\", syncWithSnapshot);\n }\n };\n\n return {\n onLoad,\n onChange,\n onAttach,\n onDetach,\n };\n};\n"],"names":[],"mappings":";;AAGO,MAAM,oBAAoB,GAAG,CAClC,MAAqC,EACrC,GAAiB,EACjB,GAAqB,KACnB;AACF,IAAA,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;AACnB,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,WAAW;AAEhD,IAAA,MAAM,OAAO,GACX,MAAM,CAAC,OAAO,KAAK,UAAU,GAAI,YAA2B,GAAG,SAAS,CAAC;IAE3E,IAAI,CAAC,OAAO,EAAE;QACZ,IAAI,CACF,6FAA6F,CAC9F;AACD,QAAA,OAAO,IAAI;IACb;AAEA,IAAA,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,IAAI,EAAE,iBAAiB,GAAG,MAAM,EAAE,GAAG,MAAM;AAErE,IAAA,MAAM,gBAAgB,GAAG,YAAW;AAClC,QAAA,IAAI;YACF,IAAI,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC;AAE1C,YAAA,IAAI,eAAe,YAA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UAAU,EAAE;AACd,YAAA,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC;QACvD;AACF,IAAA,CAAC;IAED,OAAO;QACL,MAAM;QACN,QAAQ;QACR,QAAQ;QACR,QAAQ;KACT;AACH;;;;"}
@@ -71,7 +71,6 @@ export type MaybePromise<T> = T | Promise<T>;
71
71
  export type StorageAPI = {
72
72
  getItem: (key: string) => MaybePromise<string | null>;
73
73
  setItem: (key: string, value: string) => MaybePromise<void>;
74
- removeItem: (key: string) => MaybePromise<void>;
75
74
  };
76
75
  export type Serializer<T = any> = {
77
76
  stringify: (value: T) => string;
@@ -90,5 +89,11 @@ export type PersistConfig<TState> = {
90
89
  storage?: StorageAPI;
91
90
  /** The serializer to use for storing the state. Defaults to JSON. */
92
91
  serializer?: Serializer<TState>;
92
+ /**
93
+ * When to initially synchronize the store's state with the storage. Defaults to "load".
94
+ *
95
+ * Set to "attach" when using SSR to avoid hydration errors.
96
+ */
97
+ syncWithStorageOn?: "load" | "attach";
93
98
  };
94
99
  export type StoreConfig<TState, TActions> = BaseConfig<TState, TActions> & (PersistConfig<TState> | NoPersistConfig);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dharma-core",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "author": "Frans Ekman <fransedvinekman@gmail.com>",
5
5
  "license": "MIT",
6
6
  "description": "A simple and lightweight state management solution for JavaScript and TypeScript applications.",