@inglorious/store 12.6.0 → 12.6.1
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 +1 -1
- package/package.json +1 -1
- package/src/behaviors/storage.js +13 -5
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:
|
|
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:
|
|
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.
|
|
3
|
+
"version": "12.6.1",
|
|
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",
|
package/src/behaviors/storage.js
CHANGED
|
@@ -4,6 +4,8 @@ import { parseJSON, readItem, writeItem } from "@inglorious/utils/storage.js"
|
|
|
4
4
|
|
|
5
5
|
const RENDER = /^render/
|
|
6
6
|
|
|
7
|
+
let decorators = 0
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Keeps some of an entity's fields in storage, as a byproduct of the entity:
|
|
9
11
|
* the entity stays the one source of truth. When the entity is created, the
|
|
@@ -44,6 +46,8 @@ export function withStorage(
|
|
|
44
46
|
typeof key === "function" ? key(entity) : (key ?? entity.id)
|
|
45
47
|
|
|
46
48
|
return (type) => {
|
|
49
|
+
// Tells this decorator's restores from those of others on the same type.
|
|
50
|
+
const source = `withStorage:${++decorators}`
|
|
47
51
|
/** What is kept for each entity, as written. */
|
|
48
52
|
const kept = new Map()
|
|
49
53
|
/** For entities still waiting on storage, their fields as created. */
|
|
@@ -77,7 +81,7 @@ export function withStorage(
|
|
|
77
81
|
pending.set(entity.id, snapshot(entity, fields))
|
|
78
82
|
const { id } = entity
|
|
79
83
|
const restore = (value) =>
|
|
80
|
-
api.notify(`#${id}:storageRestore`, value ?? null)
|
|
84
|
+
api.notify(`#${id}:storageRestore`, { source, value: value ?? null })
|
|
81
85
|
stored.then(restore, () => restore(null))
|
|
82
86
|
return result
|
|
83
87
|
}
|
|
@@ -89,13 +93,17 @@ export function withStorage(
|
|
|
89
93
|
return result
|
|
90
94
|
}
|
|
91
95
|
|
|
92
|
-
behavior.storageRestore = function (entity,
|
|
96
|
+
behavior.storageRestore = function (entity, payload, api) {
|
|
93
97
|
const created = pending.get(entity.id)
|
|
94
|
-
if (!created)
|
|
98
|
+
if (payload?.source !== source || !created) {
|
|
99
|
+
type.storageRestore?.call(this, entity, payload, api)
|
|
100
|
+
save(entity)
|
|
101
|
+
return
|
|
102
|
+
}
|
|
95
103
|
pending.delete(entity.id)
|
|
96
|
-
kept.set(entity.id,
|
|
104
|
+
kept.set(entity.id, payload.value)
|
|
97
105
|
|
|
98
|
-
const restored = pick(parseJSON(
|
|
106
|
+
const restored = pick(parseJSON(payload.value), fields)
|
|
99
107
|
for (const [field, value] of Object.entries(restored)) {
|
|
100
108
|
if (JSON.stringify(entity[field]) === created[field]) {
|
|
101
109
|
entity[field] = value
|