@inglorious/store 12.6.1 → 12.6.2

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/README.md CHANGED
@@ -834,7 +834,7 @@ const types = {
834
834
  - Put it last in the type's array: it saves after the handlers before it.
835
835
  - `key` names where the fields go (the entity's id by default), or derives it from the entity. `storage` is any Web Storage, `localStorage` by default.
836
836
  - Storage that is missing, full or refusing, as in a private window, only means nothing is kept.
837
- - A storage whose `getItem` returns a promise works too, such as Capacitor Preferences through `createNativeStorage` from `@inglorious/web/mobile`. The entity then starts with the type's defaults, and once the storage answers, a `storageRestore` event brings back the fields no handler changed meanwhile. Nothing is written before that. A type with its own `storageRestore(entity, restored, api)` hears what came back, to announce it. Several `withStorage` on one type each restore their own fields:
837
+ - A storage whose `getItem` returns a promise works too, such as Capacitor Preferences through `createNativeStorage` from `@inglorious/web/mobile`. The entity then starts with the type's defaults, and once the storage answers, a `storageRestore` event brings the stored fields back. Nothing is written before that. A type with its own `storageRestore(entity, restored, api)` hears what came back, to announce it. Several `withStorage` on one type each restore their own fields:
838
838
 
839
839
  ```javascript
840
840
  const Session = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "12.6.1",
3
+ "version": "12.6.2",
4
4
  "description": "A state manager for real-time, collaborative apps, inspired by game development patterns and compatible with Redux.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -19,9 +19,9 @@ let decorators = 0
19
19
  *
20
20
  * A storage whose `getItem` returns a promise, such as Capacitor Preferences,
21
21
  * restores the fields once it answers: the entity starts with the type's
22
- * defaults, and `storageRestore` then brings back the fields no handler has
23
- * changed in the meantime. Nothing is written until then. A type's own
24
- * `storageRestore(entity, restored, api)` runs after.
22
+ * defaults, and `storageRestore` then brings the stored fields back. Nothing is
23
+ * written until then. A type's own `storageRestore(entity, restored, api)`
24
+ * runs after.
25
25
  *
26
26
  * @param {string[]} fields The entity's fields to keep.
27
27
  * @param {Object} [options]
@@ -46,15 +46,12 @@ export function withStorage(
46
46
  typeof key === "function" ? key(entity) : (key ?? entity.id)
47
47
 
48
48
  return (type) => {
49
- // Tells this decorator's restores from those of others on the same type.
50
49
  const source = `withStorage:${++decorators}`
51
- /** What is kept for each entity, as written. */
52
50
  const kept = new Map()
53
- /** For entities still waiting on storage, their fields as created. */
54
- const pending = new Map()
51
+ const waiting = new Set()
55
52
 
56
53
  function save(entity) {
57
- if (!isEntity(entity) || pending.has(entity.id)) return
54
+ if (!isEntity(entity) || waiting.has(entity.id)) return
58
55
  const value = JSON.stringify(pick(entity, fields))
59
56
  if (kept.get(entity.id) === value) return
60
57
  kept.set(entity.id, value)
@@ -77,13 +74,12 @@ export function withStorage(
77
74
  const stored = readItem(keyOf(entity), storage)
78
75
 
79
76
  if (isPromiseLike(stored)) {
80
- const result = type.create?.call(this, entity, payload, api)
81
- pending.set(entity.id, snapshot(entity, fields))
82
77
  const { id } = entity
78
+ waiting.add(id)
83
79
  const restore = (value) =>
84
- api.notify(`#${id}:storageRestore`, { source, value: value ?? null })
80
+ api.notify(`#${id}:storageRestore`, { source, value })
85
81
  stored.then(restore, () => restore(null))
86
- return result
82
+ return type.create?.call(this, entity, payload, api)
87
83
  }
88
84
 
89
85
  kept.set(entity.id, stored)
@@ -94,21 +90,15 @@ export function withStorage(
94
90
  }
95
91
 
96
92
  behavior.storageRestore = function (entity, payload, api) {
97
- const created = pending.get(entity.id)
98
- if (payload?.source !== source || !created) {
93
+ if (payload?.source !== source) {
99
94
  type.storageRestore?.call(this, entity, payload, api)
100
95
  save(entity)
101
96
  return
102
97
  }
103
- pending.delete(entity.id)
98
+ waiting.delete(entity.id)
104
99
  kept.set(entity.id, payload.value)
105
-
106
100
  const restored = pick(parseJSON(payload.value), fields)
107
- for (const [field, value] of Object.entries(restored)) {
108
- if (JSON.stringify(entity[field]) === created[field]) {
109
- entity[field] = value
110
- }
111
- }
101
+ Object.assign(entity, restored)
112
102
  type.storageRestore?.call(this, entity, restored, api)
113
103
  save(entity)
114
104
  }
@@ -117,12 +107,6 @@ export function withStorage(
117
107
  }
118
108
  }
119
109
 
120
- function snapshot(entity, fields) {
121
- return Object.fromEntries(
122
- fields.map((field) => [field, JSON.stringify(entity[field])]),
123
- )
124
- }
125
-
126
110
  function isEntity(value) {
127
111
  return Boolean(value) && typeof value === "object" && "id" in value
128
112
  }
@@ -22,7 +22,7 @@ export interface StorageOptions {
22
22
  * before the type's own `create`, and are written back after any handler that
23
23
  * changes them. Put it last in the type's array. With a storage answering
24
24
  * with promises, the entity starts with the defaults and `storageRestore`
25
- * brings back the fields no handler changed while waiting.
25
+ * brings the stored fields back.
26
26
  */
27
27
  export declare function withStorage(
28
28
  fields: string[],