@inglorious/store 12.6.0 → 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 +1 -1
- package/package.json +1 -1
- package/src/behaviors/storage.js +20 -28
- package/types/behaviors/storage.d.ts +1 -1
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
|
|
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.
|
|
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",
|
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
|
|
@@ -17,9 +19,9 @@ const RENDER = /^render/
|
|
|
17
19
|
*
|
|
18
20
|
* A storage whose `getItem` returns a promise, such as Capacitor Preferences,
|
|
19
21
|
* restores the fields once it answers: the entity starts with the type's
|
|
20
|
-
* defaults, and `storageRestore` then brings
|
|
21
|
-
*
|
|
22
|
-
*
|
|
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.
|
|
23
25
|
*
|
|
24
26
|
* @param {string[]} fields The entity's fields to keep.
|
|
25
27
|
* @param {Object} [options]
|
|
@@ -44,13 +46,12 @@ export function withStorage(
|
|
|
44
46
|
typeof key === "function" ? key(entity) : (key ?? entity.id)
|
|
45
47
|
|
|
46
48
|
return (type) => {
|
|
47
|
-
|
|
49
|
+
const source = `withStorage:${++decorators}`
|
|
48
50
|
const kept = new Map()
|
|
49
|
-
|
|
50
|
-
const pending = new Map()
|
|
51
|
+
const waiting = new Set()
|
|
51
52
|
|
|
52
53
|
function save(entity) {
|
|
53
|
-
if (!isEntity(entity) ||
|
|
54
|
+
if (!isEntity(entity) || waiting.has(entity.id)) return
|
|
54
55
|
const value = JSON.stringify(pick(entity, fields))
|
|
55
56
|
if (kept.get(entity.id) === value) return
|
|
56
57
|
kept.set(entity.id, value)
|
|
@@ -73,13 +74,12 @@ export function withStorage(
|
|
|
73
74
|
const stored = readItem(keyOf(entity), storage)
|
|
74
75
|
|
|
75
76
|
if (isPromiseLike(stored)) {
|
|
76
|
-
const result = type.create?.call(this, entity, payload, api)
|
|
77
|
-
pending.set(entity.id, snapshot(entity, fields))
|
|
78
77
|
const { id } = entity
|
|
78
|
+
waiting.add(id)
|
|
79
79
|
const restore = (value) =>
|
|
80
|
-
api.notify(`#${id}:storageRestore`, value
|
|
80
|
+
api.notify(`#${id}:storageRestore`, { source, value })
|
|
81
81
|
stored.then(restore, () => restore(null))
|
|
82
|
-
return
|
|
82
|
+
return type.create?.call(this, entity, payload, api)
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
kept.set(entity.id, stored)
|
|
@@ -89,18 +89,16 @@ export function withStorage(
|
|
|
89
89
|
return result
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
behavior.storageRestore = function (entity,
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const restored = pick(parseJSON(stored), fields)
|
|
99
|
-
for (const [field, value] of Object.entries(restored)) {
|
|
100
|
-
if (JSON.stringify(entity[field]) === created[field]) {
|
|
101
|
-
entity[field] = value
|
|
102
|
-
}
|
|
92
|
+
behavior.storageRestore = function (entity, payload, api) {
|
|
93
|
+
if (payload?.source !== source) {
|
|
94
|
+
type.storageRestore?.call(this, entity, payload, api)
|
|
95
|
+
save(entity)
|
|
96
|
+
return
|
|
103
97
|
}
|
|
98
|
+
waiting.delete(entity.id)
|
|
99
|
+
kept.set(entity.id, payload.value)
|
|
100
|
+
const restored = pick(parseJSON(payload.value), fields)
|
|
101
|
+
Object.assign(entity, restored)
|
|
104
102
|
type.storageRestore?.call(this, entity, restored, api)
|
|
105
103
|
save(entity)
|
|
106
104
|
}
|
|
@@ -109,12 +107,6 @@ export function withStorage(
|
|
|
109
107
|
}
|
|
110
108
|
}
|
|
111
109
|
|
|
112
|
-
function snapshot(entity, fields) {
|
|
113
|
-
return Object.fromEntries(
|
|
114
|
-
fields.map((field) => [field, JSON.stringify(entity[field])]),
|
|
115
|
-
)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
110
|
function isEntity(value) {
|
|
119
111
|
return Boolean(value) && typeof value === "object" && "id" in value
|
|
120
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
|
|
25
|
+
* brings the stored fields back.
|
|
26
26
|
*/
|
|
27
27
|
export declare function withStorage(
|
|
28
28
|
fields: string[],
|