@inglorious/store 12.5.0 → 12.6.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.
- package/README.md +15 -0
- package/package.json +3 -3
- package/src/behaviors/storage.js +50 -35
- package/types/behaviors/storage.d.ts +13 -3
package/README.md
CHANGED
|
@@ -834,6 +834,21 @@ 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:
|
|
838
|
+
|
|
839
|
+
```javascript
|
|
840
|
+
const Session = [
|
|
841
|
+
{
|
|
842
|
+
create(entity) {
|
|
843
|
+
entity.user ??= null
|
|
844
|
+
},
|
|
845
|
+
storageRestore(entity, restored, api) {
|
|
846
|
+
api.notify("sessionChange", { user: entity.user })
|
|
847
|
+
},
|
|
848
|
+
},
|
|
849
|
+
withStorage(["user"], { storage: () => preferences }),
|
|
850
|
+
]
|
|
851
|
+
```
|
|
837
852
|
|
|
838
853
|
---
|
|
839
854
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/store",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.6.0",
|
|
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",
|
|
@@ -82,11 +82,11 @@
|
|
|
82
82
|
"mutative": "^2.0.2"
|
|
83
83
|
},
|
|
84
84
|
"peerDependencies": {
|
|
85
|
-
"@inglorious/utils": "^5.
|
|
85
|
+
"@inglorious/utils": "^5.4.0"
|
|
86
86
|
},
|
|
87
87
|
"devDependencies": {
|
|
88
88
|
"@inglorious/eslint-config": "1.2.0",
|
|
89
|
-
"@inglorious/utils": "5.
|
|
89
|
+
"@inglorious/utils": "5.4.0",
|
|
90
90
|
"@reduxjs/toolkit": "^2.14.0",
|
|
91
91
|
"prettier": "^3.9.10",
|
|
92
92
|
"typescript": "~7.0.2",
|
package/src/behaviors/storage.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { pick } from "@inglorious/utils/data-structures/object.js"
|
|
2
|
+
import { isPromiseLike } from "@inglorious/utils/data-structures/promise.js"
|
|
3
|
+
import { parseJSON, readItem, writeItem } from "@inglorious/utils/storage.js"
|
|
4
|
+
|
|
1
5
|
const RENDER = /^render/
|
|
2
6
|
|
|
3
7
|
/**
|
|
@@ -11,12 +15,20 @@ const RENDER = /^render/
|
|
|
11
15
|
* Storage that is missing, full or refusing, as in a private window, only
|
|
12
16
|
* means nothing is kept.
|
|
13
17
|
*
|
|
18
|
+
* A storage whose `getItem` returns a promise, such as Capacitor Preferences,
|
|
19
|
+
* restores the fields once it answers: the entity starts with the type's
|
|
20
|
+
* defaults, and `storageRestore` then brings back the fields no handler has
|
|
21
|
+
* changed in the meantime. Nothing is written until then. A type's own
|
|
22
|
+
* `storageRestore(entity, restored, api)` runs after.
|
|
23
|
+
*
|
|
14
24
|
* @param {string[]} fields The entity's fields to keep.
|
|
15
25
|
* @param {Object} [options]
|
|
16
26
|
* @param {string | ((entity: Object) => string)} [options.key] Where the
|
|
17
27
|
* fields are kept. Defaults to the entity's id.
|
|
18
28
|
* @param {() => Storage | undefined} [options.storage] A Web Storage, such as
|
|
19
|
-
* `() => sessionStorage` for the tab only
|
|
29
|
+
* `() => sessionStorage` for the tab only, or anything with the same
|
|
30
|
+
* `getItem` and `setItem`, answering now or with a promise. Defaults to
|
|
31
|
+
* `localStorage`.
|
|
20
32
|
*
|
|
21
33
|
* @example
|
|
22
34
|
* const types = {
|
|
@@ -34,13 +46,15 @@ export function withStorage(
|
|
|
34
46
|
return (type) => {
|
|
35
47
|
/** What is kept for each entity, as written. */
|
|
36
48
|
const kept = new Map()
|
|
49
|
+
/** For entities still waiting on storage, their fields as created. */
|
|
50
|
+
const pending = new Map()
|
|
37
51
|
|
|
38
52
|
function save(entity) {
|
|
39
|
-
if (!isEntity(entity)) return
|
|
53
|
+
if (!isEntity(entity) || pending.has(entity.id)) return
|
|
40
54
|
const value = JSON.stringify(pick(entity, fields))
|
|
41
55
|
if (kept.get(entity.id) === value) return
|
|
42
56
|
kept.set(entity.id, value)
|
|
43
|
-
|
|
57
|
+
writeItem(keyOf(entity), value, storage)
|
|
44
58
|
}
|
|
45
59
|
|
|
46
60
|
const behavior = {}
|
|
@@ -56,50 +70,51 @@ export function withStorage(
|
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
behavior.create = function (entity, payload, api) {
|
|
59
|
-
const stored =
|
|
73
|
+
const stored = readItem(keyOf(entity), storage)
|
|
74
|
+
|
|
75
|
+
if (isPromiseLike(stored)) {
|
|
76
|
+
const result = type.create?.call(this, entity, payload, api)
|
|
77
|
+
pending.set(entity.id, snapshot(entity, fields))
|
|
78
|
+
const { id } = entity
|
|
79
|
+
const restore = (value) =>
|
|
80
|
+
api.notify(`#${id}:storageRestore`, value ?? null)
|
|
81
|
+
stored.then(restore, () => restore(null))
|
|
82
|
+
return result
|
|
83
|
+
}
|
|
84
|
+
|
|
60
85
|
kept.set(entity.id, stored)
|
|
61
|
-
Object.assign(entity, pick(
|
|
86
|
+
Object.assign(entity, pick(parseJSON(stored), fields))
|
|
62
87
|
const result = type.create?.call(this, entity, payload, api)
|
|
63
88
|
save(entity)
|
|
64
89
|
return result
|
|
65
90
|
}
|
|
66
91
|
|
|
92
|
+
behavior.storageRestore = function (entity, stored, api) {
|
|
93
|
+
const created = pending.get(entity.id)
|
|
94
|
+
if (!created) return
|
|
95
|
+
pending.delete(entity.id)
|
|
96
|
+
kept.set(entity.id, stored)
|
|
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
|
+
}
|
|
103
|
+
}
|
|
104
|
+
type.storageRestore?.call(this, entity, restored, api)
|
|
105
|
+
save(entity)
|
|
106
|
+
}
|
|
107
|
+
|
|
67
108
|
return behavior
|
|
68
109
|
}
|
|
69
110
|
}
|
|
70
111
|
|
|
71
|
-
function
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
return picked
|
|
112
|
+
function snapshot(entity, fields) {
|
|
113
|
+
return Object.fromEntries(
|
|
114
|
+
fields.map((field) => [field, JSON.stringify(entity[field])]),
|
|
115
|
+
)
|
|
77
116
|
}
|
|
78
117
|
|
|
79
118
|
function isEntity(value) {
|
|
80
119
|
return Boolean(value) && typeof value === "object" && "id" in value
|
|
81
120
|
}
|
|
82
|
-
|
|
83
|
-
function parse(stored) {
|
|
84
|
-
try {
|
|
85
|
-
return stored ? JSON.parse(stored) : null
|
|
86
|
-
} catch {
|
|
87
|
-
return null
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function read(storage, key) {
|
|
92
|
-
try {
|
|
93
|
-
return storage()?.getItem(key) ?? null
|
|
94
|
-
} catch {
|
|
95
|
-
return null
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function write(storage, key, value) {
|
|
100
|
-
try {
|
|
101
|
-
storage()?.setItem(key, value)
|
|
102
|
-
} catch {
|
|
103
|
-
// Nothing is kept: the entity still has the value.
|
|
104
|
-
}
|
|
105
|
-
}
|
|
@@ -1,18 +1,28 @@
|
|
|
1
|
+
import type { KeyValueStorage } from "@inglorious/utils/storage"
|
|
2
|
+
|
|
1
3
|
import { Api } from "../api"
|
|
2
4
|
import { BaseEntity, EntityType } from "../store"
|
|
3
5
|
|
|
6
|
+
export type { KeyValueStorage }
|
|
7
|
+
|
|
4
8
|
export interface StorageOptions {
|
|
5
9
|
/** Where the fields are kept. Defaults to the entity's id. */
|
|
6
10
|
key?: string | ((entity: BaseEntity & { id: string }) => string)
|
|
7
|
-
/**
|
|
8
|
-
|
|
11
|
+
/**
|
|
12
|
+
* A Web Storage, such as `() => sessionStorage`, or one answering with
|
|
13
|
+
* promises, which restores the fields once it answers. Defaults to
|
|
14
|
+
* `localStorage`.
|
|
15
|
+
*/
|
|
16
|
+
storage?: () => KeyValueStorage | undefined
|
|
9
17
|
}
|
|
10
18
|
|
|
11
19
|
/**
|
|
12
20
|
* Keeps some of an entity's fields in storage, as a byproduct of the entity,
|
|
13
21
|
* which stays the one source of truth: they come back when it is created,
|
|
14
22
|
* before the type's own `create`, and are written back after any handler that
|
|
15
|
-
* changes them. Put it last in the type's array.
|
|
23
|
+
* changes them. Put it last in the type's array. With a storage answering
|
|
24
|
+
* with promises, the entity starts with the defaults and `storageRestore`
|
|
25
|
+
* brings back the fields no handler changed while waiting.
|
|
16
26
|
*/
|
|
17
27
|
export declare function withStorage(
|
|
18
28
|
fields: string[],
|