@plq/use-persisted-state 1.4.1 → 1.4.3

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.
Files changed (64) hide show
  1. package/README.md +3 -3
  2. package/lib/create-async-persisted-state.d.ts.map +1 -1
  3. package/lib/create-async-persisted-state.js +14 -89
  4. package/lib/create-async-persisted-state.js.map +1 -1
  5. package/lib/create-persisted-state.d.ts.map +1 -1
  6. package/lib/create-persisted-state.js +6 -53
  7. package/lib/create-persisted-state.js.map +1 -1
  8. package/lib/index.d.ts.map +1 -1
  9. package/lib/storages/chrome-storage.d.ts.map +1 -1
  10. package/lib/storages/chrome-storage.js +6 -15
  11. package/lib/storages/chrome-storage.js.map +1 -1
  12. package/lib/storages/local-storage.d.ts.map +1 -1
  13. package/lib/storages/local-storage.js +1 -2
  14. package/lib/storages/local-storage.js.map +1 -1
  15. package/lib/storages/session-storage.d.ts.map +1 -1
  16. package/lib/storages/session-storage.js +1 -2
  17. package/lib/storages/session-storage.js.map +1 -1
  18. package/lib/utils/change-notifier.d.ts +1 -8
  19. package/lib/utils/change-notifier.d.ts.map +1 -1
  20. package/lib/utils/change-notifier.js +1 -10
  21. package/lib/utils/change-notifier.js.map +1 -1
  22. package/lib/utils/create-web-storage.d.ts +1 -8
  23. package/lib/utils/create-web-storage.d.ts.map +1 -1
  24. package/lib/utils/create-web-storage.js +5 -17
  25. package/lib/utils/create-web-storage.js.map +1 -1
  26. package/lib/utils/extension-storage.d.ts +2 -12
  27. package/lib/utils/extension-storage.d.ts.map +1 -1
  28. package/lib/utils/extension-storage.js +3 -14
  29. package/lib/utils/extension-storage.js.map +1 -1
  30. package/lib/utils/get-new-item.d.ts +1 -12
  31. package/lib/utils/get-new-item.d.ts.map +1 -1
  32. package/lib/utils/get-new-item.js +2 -16
  33. package/lib/utils/get-new-item.js.map +1 -1
  34. package/lib/utils/get-persisted-value.d.ts +1 -7
  35. package/lib/utils/get-persisted-value.d.ts.map +1 -1
  36. package/lib/utils/get-persisted-value.js +3 -13
  37. package/lib/utils/get-persisted-value.js.map +1 -1
  38. package/lib/utils/is-async-storage.d.ts +1 -7
  39. package/lib/utils/is-async-storage.d.ts.map +1 -1
  40. package/lib/utils/is-async-storage.js +6 -25
  41. package/lib/utils/is-async-storage.js.map +1 -1
  42. package/lib/utils/storage-event-router.d.ts +1 -5
  43. package/lib/utils/storage-event-router.d.ts.map +1 -1
  44. package/lib/utils/storage-event-router.js +3 -14
  45. package/lib/utils/storage-event-router.js.map +1 -1
  46. package/lib/utils/use-storage-handler.d.ts +1 -13
  47. package/lib/utils/use-storage-handler.d.ts.map +1 -1
  48. package/lib/utils/use-storage-handler.js +8 -78
  49. package/lib/utils/use-storage-handler.js.map +1 -1
  50. package/package.json +1 -1
  51. package/src/create-async-persisted-state.ts +14 -58
  52. package/src/create-persisted-state.ts +6 -22
  53. package/src/index.ts +1 -4
  54. package/src/storages/chrome-storage.ts +6 -18
  55. package/src/storages/local-storage.ts +1 -2
  56. package/src/storages/session-storage.ts +1 -2
  57. package/src/utils/change-notifier.ts +1 -10
  58. package/src/utils/create-web-storage.ts +5 -17
  59. package/src/utils/extension-storage.ts +3 -14
  60. package/src/utils/get-new-item.ts +2 -16
  61. package/src/utils/get-persisted-value.ts +3 -13
  62. package/src/utils/is-async-storage.ts +6 -26
  63. package/src/utils/storage-event-router.ts +3 -14
  64. package/src/utils/use-storage-handler.ts +7 -94
@@ -6,17 +6,11 @@ exports.toStoredItems = toStoredItems;
6
6
  exports.createListenerRegistry = createListenerRegistry;
7
7
  const change_notifier_1 = require("./change-notifier");
8
8
  const TRACKED_AREAS = ['local', 'sync', 'managed'];
9
- // Both browsers also report `session` changes, an area this library does not
10
- // expose. Dispatching one would read an undefined notifier and throw.
9
+ // Both browsers also report `session`, an area this library does not expose; dispatching one would throw.
11
10
  function isTrackedArea(area) {
12
11
  return TRACKED_AREAS.includes(area);
13
12
  }
14
- /**
15
- * Extension storage holds arbitrary JSON, while this library only ever writes
16
- * serialized strings. Anything else under a key belongs to other code and is
17
- * reported as absent — which is what effectively happened before, once such a
18
- * value reached `JSON.parse` and the hook fell back to its initial value.
19
- */
13
+ /** Extension storage holds arbitrary JSON; this library writes only strings, so anything else reads as absent. */
20
14
  function toStoredValue(value) {
21
15
  if (value === undefined)
22
16
  return;
@@ -40,12 +34,7 @@ function toStoredItems(items) {
40
34
  }
41
35
  return result;
42
36
  }
43
- /**
44
- * Demultiplexes an extension's single `onChanged` event, which names the area
45
- * that changed, to one notifier per area. Each adapter owns its own registry,
46
- * so a change in one browser's storage never reaches listeners registered on
47
- * another's.
48
- */
37
+ /** Demultiplexes an extension's single `onChanged` event to one notifier per area, one registry per adapter. */
49
38
  function createListenerRegistry() {
50
39
  const notifiers = {
51
40
  local: (0, change_notifier_1.createChangeNotifier)(),
@@ -1 +1 @@
1
- {"version":3,"file":"extension-storage.js","sourceRoot":"","sources":["../../src/utils/extension-storage.ts"],"names":[],"mappings":";;AAmBA,sCAIC;AAED,4CAaC;AAED,sCAQC;AAaD,wDAiBC;AA7ED,uDAA6E;AAE7E,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAU,CAAA;AAI3D,6EAA6E;AAC7E,sEAAsE;AACtE,SAAS,aAAa,CAAC,IAAY;IACjC,OAAQ,aAAmC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;AAC5D,CAAC;AAED;;;;;GAKG;AACH,SAAgB,aAAa,CAAC,KAAc;IAC1C,IAAI,KAAK,KAAK,SAAS;QAAE,OAAM;IAE/B,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAA;AACjD,CAAC;AAED,SAAgB,gBAAgB,CAAC,OAAsE;IAGrG,MAAM,MAAM,GAAqC,EAAE,CAAA;IAEnD,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE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I,CAAC,aAAa,CAAC,IAAI,CAAC;gBAAE,OAAM;YAEhC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC/B,CAAC;QACD,eAAe,CAAC,IAAI;YAClB,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,SAAS,CAAA;QAClC,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -1,14 +1,3 @@
1
- /**
2
- * Merges `newValue` under `key` into the serialized entry a factory shares between all of its
3
- * hooks, and returns the entry to store.
4
- *
5
- * Reports and throws when the entry cannot be merged into, rather than answering with one built
6
- * from nothing. What this returns is written over the whole entry, so an unreadable entry is the
7
- * one case with nothing safe to return: every other hook's key would be replaced with nothing,
8
- * and the bytes a repair still needs would go with them.
9
- *
10
- * @throws {SyntaxError} when the entry is not valid JSON.
11
- * @throws {TypeError} when the entry is valid JSON but not an object of keys.
12
- */
1
+ /** Merges `newValue` under `key` into the shared entry, throwing rather than rebuilding one it cannot read. */
13
2
  export default function getNewItem<T>(key: string, persistedItem: string, newValue: T): string;
14
3
  //# sourceMappingURL=get-new-item.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-new-item.d.ts","sourceRoot":"","sources":["../../src/utils/get-new-item.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,MAAM,CAwB7F"}
1
+ {"version":3,"file":"get-new-item.d.ts","sourceRoot":"","sources":["../../src/utils/get-new-item.ts"],"names":[],"mappings":"AAEA,+GAA+G;AAC/G,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,MAAM,CAqB7F"}
@@ -2,18 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = getNewItem;
4
4
  const is_1 = require("@plq/is");
5
- /**
6
- * Merges `newValue` under `key` into the serialized entry a factory shares between all of its
7
- * hooks, and returns the entry to store.
8
- *
9
- * Reports and throws when the entry cannot be merged into, rather than answering with one built
10
- * from nothing. What this returns is written over the whole entry, so an unreadable entry is the
11
- * one case with nothing safe to return: every other hook's key would be replaced with nothing,
12
- * and the bytes a repair still needs would go with them.
13
- *
14
- * @throws {SyntaxError} when the entry is not valid JSON.
15
- * @throws {TypeError} when the entry is valid JSON but not an object of keys.
16
- */
5
+ /** Merges `newValue` under `key` into the shared entry, throwing rather than rebuilding one it cannot read. */
17
6
  function getNewItem(key, persistedItem, newValue) {
18
7
  let persist;
19
8
  try {
@@ -24,10 +13,7 @@ function getNewItem(key, persistedItem, newValue) {
24
13
  throw err;
25
14
  }
26
15
  if (!(0, is_1.isObject)(persist)) {
27
- // A shared backend can leave any JSON under the key, and neither a primitive nor an array
28
- // survives being merged into: `JSON.stringify` unwraps the first back to itself and keeps the
29
- // second an array, so the value being set disappears with the property added to it. A stored
30
- // `null` did not even get that far - it threw out of `Object.assign` and into the caller.
16
+ // Neither a primitive nor an array survives being merged into: the value being set would disappear.
31
17
  const err = new TypeError('the stored entry is not an object of keys');
32
18
  console.error("use-persisted-state: Can't write value to storage", err);
33
19
  throw err;
@@ -1 +1 @@
1
- {"version":3,"file":"get-new-item.js","sourceRoot":"","sources":["../../src/utils/get-new-item.ts"],"names":[],"mappings":";;AAcA,6BAwBC;AAtCD,gCAAkC;AAElC;;;;;;;;;;;GAWG;AACH,SAAwB,UAAU,CAAI,GAAW,EAAE,aAAqB,EAAE,QAAW;IACnF,IAAI,OAAgB,CAAA;IAEpB,IAAI,CAAC;QACH,OAAO,GAAG,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IAC1D,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;Q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AC,SAAS,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAA;AACxD,CAAC"}
@@ -1,9 +1,3 @@
1
- /**
2
- * Resolves the value a hook starts from: the persisted entry for `key` when the stored payload
3
- * carries one, the initial value otherwise.
4
- *
5
- * Presence is decided by the key rather than by the value, so a persisted `null` comes back as
6
- * the value the user set instead of being mistaken for an absence and replaced.
7
- */
1
+ /** Resolves a hook's starting value by key presence, so a persisted `null` is not mistaken for an absence. */
8
2
  export default function getPersistedValue<T>(key: string, initialValue: T | (() => T), persist?: string): T;
9
3
  //# sourceMappingURL=get-persisted-value.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-persisted-value.d.ts","sourceRoot":"","sources":["../../src/utils/get-persisted-value.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAwB1G"}
1
+ {"version":3,"file":"get-persisted-value.d.ts","sourceRoot":"","sources":["../../src/utils/get-persisted-value.ts"],"names":[],"mappings":"AAEA,8GAA8G;AAC9G,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAoB1G"}
@@ -2,29 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = getPersistedValue;
4
4
  const is_1 = require("@plq/is");
5
- /**
6
- * Resolves the value a hook starts from: the persisted entry for `key` when the stored payload
7
- * carries one, the initial value otherwise.
8
- *
9
- * Presence is decided by the key rather than by the value, so a persisted `null` comes back as
10
- * the value the user set instead of being mistaken for an absence and replaced.
11
- */
5
+ /** Resolves a hook's starting value by key presence, so a persisted `null` is not mistaken for an absence. */
12
6
  function getPersistedValue(key, initialValue, persist) {
13
7
  let initialPersist;
14
8
  try {
15
9
  initialPersist = persist ? JSON.parse(persist) : {};
16
10
  }
17
11
  catch (err) {
18
- // A shared backend can hold a foreign or truncated entry, so a parse failure is expected here
19
- // and must not stop the component mounting. It is reported rather than swallowed because the
20
- // fallback discards whatever was persisted, and because the change path logs the same failure.
12
+ // A shared backend can hold a foreign or truncated entry, so a parse failure must not stop the mount.
21
13
  console.error("use-persisted-state: Can't parse value from storage", err);
22
14
  initialPersist = {};
23
15
  }
24
16
  let initialOrPersistedValue = (0, is_1.isFunction)(initialValue) ? initialValue() : initialValue;
25
- // `JSON.parse` yields any JSON value, so a foreign entry can be a primitive,
26
- // and `in` throws on one. This runs in the `useState` initializer, where a
27
- // throw stops the component mounting instead of falling back.
17
+ // `in` throws on a primitive, and this runs in the `useState` initializer, where a throw stops the mount.
28
18
  if ((0, is_1.isObject)(initialPersist) && key in initialPersist) {
29
19
  initialOrPersistedValue = initialPersist[key];
30
20
  }
@@ -1 +1 @@
1
- {"version":3,"file":"get-persisted-value.js","sourceRoot":"","sources":["../../src/utils/get-persisted-value.ts"],"names":[],"mappings":";;AASA,oCAwBC;AAjCD,gCAA8C;AAE9C;;;;;;GAMG;AACH,SAAwB,iBAAiB,CAAI,GAAW,EAAE,YAA2B,EAAE,OAAgB;IACrG,IAAI,cAAuB,CAAA;IAE3B,IAAI,CAAC;QACH,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IACrD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,8FAA8F;QAC9F,6FAA6F;QAC7F,+FAA+F;QAC/F,OAAO,CAAC,KAAK,CAAC,qDAAqD,EAAE,GAAG,CAAC,CAAA;QAEzE,cAAc,GAAG,EAAE,CAAA;IACrB,CAAC;IAED,IAAI,uBAAuB,GAAG,IAAA,eAAU,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAA;IAEtF,6EAA6E;IAC7E,2EAA2E;IAC3E,8DAA8D;IAC9D,IAAI,IAAA,aAAQ,EAAC,cAAc,CAAC,IAAI,GAAG,IAAI,cAAc,EAAE,CAAC;QACtD,uBAAuB,GAAG,cAAc,CAAC,GAAG,CAAM,CAAA;IACpD,CAAC;IAED,OAAO,uBAAuB,CAAA;AAChC,CAAC"}
1
+ {"version":3,"file":"get-persisted-value.js","sourceRoot":"","sources":["../../src/utils/get-persisted-value.ts"],"names":[],"mappings":";;AAGA,oCAoBC;AAvBD,gCAA8C;AAE9C,8GAA8G;AAC9G,SAAwB,iBAAiB,CAAI,GAAW,EAAE,YAA2B,EAAE,OAAgB;IACrG,IAAI,cAAuB,CAAA;IAE3B,IAAI,CAAC;QACH,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IACrD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,sGAAsG;QACtG,OAAO,CAAC,KAAK,CAAC,qDAAqD,EAAE,GAAG,CAAC,CAAA;QAEzE,cAAc,GAAG,EAAE,CAAA;IACrB,CAAC;IAED,IAAI,uBAAuB,GAAG,IAAA,eAAU,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAA;IAEtF,0GAA0G;IAC1G,IAAI,IAAA,aAAQ,EAAC,cAAc,CAAC,IAAI,GAAG,IAAI,cAAc,EAAE,CAAC;QACtD,uBAAuB,GAAG,cAAc,CAAC,GAAG,CAAM,CAAA;IACpD,CAAC;IAED,OAAO,uBAAuB,CAAA;AAChC,CAAC"}
@@ -1,10 +1,4 @@
1
1
  import type { AsyncStorage } from '../@types/storage';
2
- /**
3
- * Narrows a storage to {@link AsyncStorage}, deciding which hook the entry point builds.
4
- *
5
- * Inspection never writes: `set` and `remove` are only checked for shape, never invoked, so
6
- * `get` alone decides. A candidate pairing an async `get` with a sync `set` satisfies neither
7
- * storage interface, which is why proving the other two members is not worth a write.
8
- */
2
+ /** Narrows a storage to {@link AsyncStorage}. Inspecting never writes: `set` and `remove` are never called. */
9
3
  export default function isAsyncStorage(storage: unknown): storage is AsyncStorage;
10
4
  //# sourceMappingURL=is-async-storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-async-storage.d.ts","sourceRoot":"","sources":["../../src/utils/is-async-storage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAcrD;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,YAAY,CAoDhF"}
1
+ {"version":3,"file":"is-async-storage.d.ts","sourceRoot":"","sources":["../../src/utils/is-async-storage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAWrD,+GAA+G;AAC/G,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,YAAY,CAyChF"}
@@ -2,17 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = isAsyncStorage;
4
4
  const is_1 = require("@plq/is");
5
- // `AsyncStorage` is three methods, so every member has to be callable: a promise standing in for
6
- // one satisfies nothing the hook then does with it. `@plq/is` classifies async functions as their
7
- // own type, so `isFunction` rejects them and both checks are needed to admit a callable member.
5
+ // `@plq/is` classifies async functions as their own type, so `isFunction` alone rejects them.
8
6
  const isCallableMember = (member) => (0, is_1.isFunction)(member) || (0, is_1.isAsyncFunction)(member);
9
- /**
10
- * Narrows a storage to {@link AsyncStorage}, deciding which hook the entry point builds.
11
- *
12
- * Inspection never writes: `set` and `remove` are only checked for shape, never invoked, so
13
- * `get` alone decides. A candidate pairing an async `get` with a sync `set` satisfies neither
14
- * storage interface, which is why proving the other two members is not worth a write.
15
- */
7
+ /** Narrows a storage to {@link AsyncStorage}. Inspecting never writes: `set` and `remove` are never called. */
16
8
  function isAsyncStorage(storage) {
17
9
  const candidate = storage;
18
10
  if (!candidate) {
@@ -29,31 +21,20 @@ function isAsyncStorage(storage) {
29
21
  if (!(0, is_1.isFunction)(get)) {
30
22
  return false;
31
23
  }
32
- // `AsyncStorage` admits a plain function returning a promise, and nothing short of a call tells
33
- // that apart from a sync get. Any adapter written that way can only be recognised here, and a
34
- // third-party one is free to be written that way, so this line stays necessary however the
35
- // shipped adapters happen to declare themselves. A read is the one probe that leaves the
36
- // inspected storage as it found it.
24
+ // A plain function returning a promise is a valid `AsyncStorage`, and only a call tells it from a sync get.
37
25
  let probe;
38
26
  try {
39
- // Called as a method of the storage it belongs to: an adapter written as an
40
- // object of methods reads its own state through `this`, and probing the
41
- // extracted function would throw on a storage that is perfectly valid.
27
+ // Called as a method: an adapter reading its own state through `this` would throw on a torn-off function.
42
28
  probe = get.call(candidate, '');
43
29
  }
44
30
  catch {
45
- // Not swallowed, deferred: consumers build the factory at module scope, where
46
- // a throw takes the import down instead of reaching a component. A storage
47
- // whose read fails is not provably async, and the sync path raises the same
48
- // failure at the first read, where it can be handled.
31
+ // Deferred, not swallowed: factories are built at module scope, where a throw takes the import down.
49
32
  return false;
50
33
  }
51
34
  if (!(0, is_1.isPromise)(probe)) {
52
35
  return false;
53
36
  }
54
- // Only the probe's shape answers the question, so its outcome is discarded by construction.
55
- // The rejection still has to be claimed: unclaimed, it terminates the process on Node 15+,
56
- // letting a storage that merely fails a read take the consuming application down with it.
37
+ // An unclaimed rejection terminates the process on Node 15+, so the discarded probe still has to be claimed.
57
38
  probe.then(undefined, () => undefined);
58
39
  return true;
59
40
  }
@@ -1 +1 @@
1
- {"version":3,"file":"is-async-storage.js","sourceRoot":"","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CAAC,IAAA,cAAS,EAAC,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,KAAK,CAAA;IACd,CAAC;IAED,6GAA6G;IAC7G,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;IAEtC,OAAO,IAAI,CAAA;AACb,CAAC"}
@@ -6,11 +6,7 @@ export interface StorageEventRoute {
6
6
  [key: string]: StorageChange;
7
7
  }): void;
8
8
  }
9
- /**
10
- * Starts routing DOM storage events to `route`. Every adapter shares one
11
- * subscription on the global object, so creating adapters nobody listens to
12
- * costs nothing.
13
- */
9
+ /** Starts routing DOM storage events to `route`. Every adapter shares one subscription on the global object. */
14
10
  export declare function addRoute(route: StorageEventRoute): void;
15
11
  /** Stops routing to `route`, releasing the shared subscription with the last one. */
16
12
  export declare function removeRoute(route: StorageEventRoute): void;
@@ -1 +1 @@
1
- {"version":3,"file":"storage-event-router.d.ts","sourceRoot":"","sources":["../../src/utils/storage-event-router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEtD,yEAAyE;AACzE,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,UAAU,CAAC,OAAO,CAAA;IAC3B,IAAI,CAAC,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAAA;KAAE,GAAG,IAAI,CAAA;CACtD;AAoCD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAQvD;AAED,qFAAqF;AACrF,wBAAgB,WAAW,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAQ1D"}
1
+ {"version":3,"file":"storage-event-router.d.ts","sourceRoot":"","sources":["../../src/utils/storage-event-router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAEtD,yEAAyE;AACzE,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,UAAU,CAAC,OAAO,CAAA;IAC3B,IAAI,CAAC,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAAA;KAAE,GAAG,IAAI,CAAA;CACtD;AA6BD,gHAAgH;AAChH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAQvD;AAED,qFAAqF;AACrF,wBAAgB,WAAW,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAQ1D"}
@@ -14,28 +14,17 @@ function routeStorageEvent(event) {
14
14
  },
15
15
  };
16
16
  for (const route of routes) {
17
- // A browser always names the area it changed, so real cross-tab events
18
- // reach only the adapter of that area. An event a consumer synthesized —
19
- // the sole way to test cross-tab sync under jsdom, and a common way to
20
- // notify the writing tab in production — often cannot name one: the
21
- // StorageEvent constructor rejects a mocked storage there. Those still go
22
- // everywhere, exactly as they did before areas were told apart.
17
+ // An event a consumer synthesized often cannot name an area, so those still reach every route.
23
18
  if (event.storageArea == null || event.storageArea === route.storage) {
24
19
  route.fire(changes);
25
20
  }
26
21
  }
27
22
  }
28
- // A runtime can provide storage without the DOM event API, and can provide half
29
- // of that API: subscribing where nothing can unsubscribe strands a listener and
30
- // makes releasing it throw, so both halves are required before taking one.
23
+ // A runtime can provide half the DOM event API, and subscribing where nothing can unsubscribe strands a listener.
31
24
  function canSubscribe() {
32
25
  return typeof globalThis.addEventListener === 'function' && typeof globalThis.removeEventListener === 'function';
33
26
  }
34
- /**
35
- * Starts routing DOM storage events to `route`. Every adapter shares one
36
- * subscription on the global object, so creating adapters nobody listens to
37
- * costs nothing.
38
- */
27
+ /** Starts routing DOM storage events to `route`. Every adapter shares one subscription on the global object. */
39
28
  function addRoute(route) {
40
29
  routes.add(route);
41
30
  if (!subscription && canSubscribe()) {
@@ -1 +1 @@
1
- {"version":3,"file":"storage-event-router.js","sourceRoot":"","sources":["../../src/utils/storage-event-router.ts"],"names":[],"mappings":";;AA+CA,4BAQC;AA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AA;IACtD,CAAC;AACH,CAAC;AAED,qFAAqF;AACrF,SAAgB,WAAW,CAAC,KAAwB;IAClD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAEpB,IAAI,MAAM,CAAC,IAAI,KAAK,CAAC,IAAI,YAAY,EAAE,CAAC;QACtC,UAAU,CAAC,mBAAmB,CAAC,SAAS,EAAE,YAAY,CAAC,CAAA;QAEvD,YAAY,GAAG,IAAI,CAAA;IACrB,CAAC;AACH,CAAC"}
@@ -1,15 +1,3 @@
1
- import type React from 'react';
2
1
  import type { AsyncStorage, Storage } from '../@types/storage';
3
- /**
4
- * Remembers an entry the hook is about to write, so the change the backend
5
- * reports for it can be told apart from someone else's write.
6
- *
7
- * Recorded before the write, because a backend may report it before the call
8
- * settles. One slot was not enough: a backend free to report after its write
9
- * settles - chrome and browser storage both are - lets the next write overwrite
10
- * the record of a write still waiting to be reported, and that unsuppressed echo
11
- * then puts the earlier value back over the later one.
12
- */
13
- export declare function recordOwnWrite(pendingOwnWrites: React.RefObject<string[]>, item: string): void;
14
- export default function useStorageHandler<T>(key: string, storageKey: string, applyValue: (value: T) => void, storage: AsyncStorage | Storage, initialValue: T | (() => T), pendingOwnWrites: React.RefObject<string[]>): void;
2
+ export default function useStorageHandler<T>(key: string, storageKey: string, applyValue: (value: T) => void, storage: AsyncStorage | Storage, initialValue: T | (() => T)): void;
15
3
  //# sourceMappingURL=use-storage-handler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-storage-handler.d.ts","sourceRoot":"","sources":["../../src/utils/use-storage-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAiB,MAAM,mBAAmB,CAAA;AAkE7E;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,gBAAgB,EAAE,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAM9F;AA+DD,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,CAAC,EACzC,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,EAC9B,OAAO,EAAE,YAAY,GAAG,OAAO,EAC/B,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAC3B,gBAAgB,EAAE,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC,GAC1C,IAAI,CAsBN"}
1
+ {"version":3,"file":"use-storage-handler.d.ts","sourceRoot":"","sources":["../../src/utils/use-storage-handler.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAiB,MAAM,mBAAmB,CAAA;AAgE7E,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,CAAC,EACzC,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,EAC9B,OAAO,EAAE,YAAY,GAAG,OAAO,EAC/B,YAAY,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAC1B,IAAI,CAiBN"}
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.recordOwnWrite = recordOwnWrite;
4
3
  exports.default = useStorageHandler;
5
4
  const react_1 = require("react");
6
5
  const is_1 = require("@plq/is");
@@ -13,119 +12,50 @@ function readStoredValue(key, entry) {
13
12
  console.error("use-persisted-state: Can't parse value from storage", err);
14
13
  return { status: 'unavailable' };
15
14
  }
16
- // A foreign entry can be any JSON value, and `in` throws on a primitive. This
17
- // runs inside the adapter's notify loop, where a throw also cuts off every
18
- // listener queued behind this one.
15
+ // `in` throws on a primitive, and a throw here cuts off every listener queued behind this one.
19
16
  if (!(0, is_1.isObject)(parsed) || !(key in parsed))
20
17
  return { status: 'unavailable' };
21
18
  return { status: 'stored', value: parsed[key] };
22
19
  }
23
- // Restores the initial value when the whole entry is removed from the storage.
24
20
  function applyRemoval(change, itemKey, applyValue, latestInitialValue) {
25
21
  if (change.oldValue === null || change.oldValue === undefined)
26
22
  return;
27
23
  const oldValue = readStoredValue(itemKey, change.oldValue);
28
24
  const declaredInitialValue = latestInitialValue.current;
29
- // Compared against the resolved value: a factory is never equal to what it
30
- // produces, so comparing the declaration re-applies the initial value on every
31
- // removal.
25
+ // Resolved before comparing: a factory is never equal to what it produces.
32
26
  const initialValue = (0, is_1.isFunction)(declaredInitialValue) ? declaredInitialValue() : declaredInitialValue;
33
- // An entry with no readable value for the key cannot be shown to have held the
34
- // initial value, so the fallback is applied rather than skipped. The entry is
35
- // gone either way, and stale state left in place is worse than a redundant
36
- // restore.
27
+ // Restore redundantly rather than leave stale state when the entry's old value cannot be read.
37
28
  if (oldValue.status === 'stored' && oldValue.value === initialValue)
38
29
  return;
39
30
  applyValue(initialValue);
40
31
  }
41
- // A backend that reports nothing back leaves every record unmatched, so the list
42
- // needs a ceiling or it grows by one string per write for as long as the hook is
43
- // mounted. A bound, not a tuning: a backend that does report drains the list on
44
- // every echo, and one that reports late is what the list exists for.
45
- const MAX_PENDING_OWN_WRITES = 8;
46
- /**
47
- * Remembers an entry the hook is about to write, so the change the backend
48
- * reports for it can be told apart from someone else's write.
49
- *
50
- * Recorded before the write, because a backend may report it before the call
51
- * settles. One slot was not enough: a backend free to report after its write
52
- * settles - chrome and browser storage both are - lets the next write overwrite
53
- * the record of a write still waiting to be reported, and that unsuppressed echo
54
- * then puts the earlier value back over the later one.
55
- */
56
- function recordOwnWrite(pendingOwnWrites, item) {
57
- const pending = pendingOwnWrites.current;
58
- if (pending.length >= MAX_PENDING_OWN_WRITES)
59
- pending.shift();
60
- pending.push(item);
61
- }
62
- /**
63
- * Reports whether this change is a write the hook itself made, forgetting the
64
- * record when it is. A backend reports a write to every listener, the one that
65
- * made it included, and applying that echo is the storage-to-state re-sync this
66
- * hook was rebuilt without, arriving by another road: it decodes the entry again
67
- * and hands the caller an equal value with a new identity, plus a render for a
68
- * value it already holds.
69
- *
70
- * The price, accepted knowingly: for a value JSON cannot carry, the writer keeps
71
- * what it set - NaN - while every other component on the key decodes the null
72
- * that reached storage, so the two disagree until one of them remounts. Applying
73
- * the echo would settle that disagreement and bring back both the second
74
- * storage-to-state path and the render per write, so it is not the repair it
75
- * looks like.
76
- *
77
- * Forgetting on the first match keeps a later identical entry a change. Another
78
- * hook writing the same bytes is suppressed along with it, and nothing is lost:
79
- * it would have replaced a value with an equal one.
80
- */
81
- function consumeOwnWriteEcho(entry, pendingOwnWrites) {
82
- const matched = pendingOwnWrites.current.indexOf(entry);
83
- if (matched === -1)
84
- return false;
85
- // Everything recorded before the reported entry goes with it: a backend applies
86
- // writes in the order it took them and reports them in that order too, so a
87
- // record still unmatched by now has no echo left to wait for.
88
- pendingOwnWrites.current.splice(0, matched + 1);
89
- return true;
90
- }
91
- // Builds the change handler. Not a hook, despite living next to one.
92
- function createStorageHandler(itemKey, storageKey, applyValue, latestInitialValue, pendingOwnWrites) {
32
+ function createStorageHandler(itemKey, storageKey, applyValue, latestInitialValue) {
93
33
  return (changes) => {
94
34
  for (const [key, change] of Object.entries(changes)) {
95
35
  if (key !== storageKey)
96
36
  continue;
97
- // Ahead of the echo check, which needs an entry to match and a removal has
98
- // none. A removal is never one of this hook's own writes anyway: only the
99
- // entries it stores are recorded.
100
37
  if (change.newValue === null || change.newValue === undefined) {
101
38
  applyRemoval(change, itemKey, applyValue, latestInitialValue);
102
39
  continue;
103
40
  }
104
- if (consumeOwnWriteEcho(change.newValue, pendingOwnWrites))
105
- continue;
106
41
  const newValue = readStoredValue(itemKey, change.newValue);
107
42
  if (newValue.status === 'stored')
108
43
  applyValue(newValue.value);
109
44
  }
110
45
  };
111
46
  }
112
- function useStorageHandler(key, storageKey, applyValue, storage, initialValue, pendingOwnWrites) {
113
- // A removal restores the initial value the hook holds now, the same one the
114
- // key-change path reads, so a caller whose default travels with its data gets
115
- // that record's default back rather than the mounted one. Tracked through a ref
116
- // rather than a dependency because an inline object or factory has a new
117
- // identity every render, which would tear the subscription down and rebuild it;
118
- // written during render, as the key the hook is rendering for is.
47
+ function useStorageHandler(key, storageKey, applyValue, storage, initialValue) {
48
+ // A ref, not a dependency: an inline initial value changes identity every render and would churn it.
119
49
  const latestInitialValue = (0, react_1.useRef)(initialValue);
120
50
  latestInitialValue.current = initialValue;
121
51
  (0, react_1.useEffect)(() => {
122
- const handleStorage = createStorageHandler(key, storageKey, applyValue, latestInitialValue, pendingOwnWrites);
52
+ const handleStorage = createStorageHandler(key, storageKey, applyValue, latestInitialValue);
123
53
  storage.onChanged.addListener(handleStorage);
124
54
  return () => {
125
55
  if (storage.onChanged.hasListener(handleStorage)) {
126
56
  storage.onChanged.removeListener(handleStorage);
127
57
  }
128
58
  };
129
- }, [key, storage.onChanged, storageKey, applyValue, pendingOwnWrites]);
59
+ }, [key, storage.onChanged, storageKey, applyValue]);
130
60
  }
131
61
  //# sourceMappingURL=use-storage-handler.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-storage-handler.js","sourceRoot":"","sources":["../../src/utils/use-storage-handler.ts"],"names":[],"mappings":";;AA8EA,wCAMC;AA+DD,oCA6BC;AA/KD,iCAAyC;AAEzC,gCAA8C;AAe9C,SAAS,eAAe,CAAI,GAAW,EAAE,KAAa;IACpD,IAAI,MAAe,CAAA;IAEnB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,qDAAqD,EAAE,GAAG,CAAC,CAAA;QAEzE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,CAAA;IAClC,CAAC;IAED,8EAA8E;IAC9E,2EAA2E;IAC3E,mCAAmC;IACnC,IAAI,CAAC,IAAA,aAAQ,EAAC,MAAM,CAAC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AAI,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,kBAAkB,CAAC,CAAA;gBAChE,SAAQ;YACV,CAAC;YAED,MAAM,QAAQ,GAAG,eAAe,CAAI,OAAO,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAA;YAE7D,IAAI,QAAQ,CAAC,MAAM,KAAK,QAAQ;gBAAE,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC9D,CAAC;IACH,CAAC,CAAA;AACH,CAAC;AAED,SAAwB,iBAAiB,CACvC,GAAW,EACX,UAAkB,EAClB,UAA8B,EAC9B,OAA+B,EAC/B,YAA2B;IAE3B,qGAAqG;IACrG,MAAM,kBAAkB,GAAG,IAAA,cAAM,EAAC,YAAY,CAAC,CAAA;IAE/C,kBAAkB,CAAC,OAAO,GAAG,YAAY,CAAA;IAEzC,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,aAAa,GAAG,oBAAoB,CAAI,GAAG,EAAE,UAAU,EAAE,UAAU,EAAE,kBAAkB,CAAC,CAAA;QAE9F,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,aAAa,CAAC,CAAA;QAE5C,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,aAAa,CAAC,EAAE,CAAC;gBACjD,OAAO,CAAC,SAAS,CAAC,cAAc,CAAC,aAAa,CAAC,CAAA;YACjD,CAAC;QACH,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,SAAS,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAA;AACtD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plq/use-persisted-state",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "useState hook with persistence in storage",
5
5
  "type": "commonjs",
6
6
  "main": "lib/index.js",