@inglorious/store 9.4.0 → 9.5.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/package.json +1 -1
- package/src/store.js +52 -41
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/store",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.5.1",
|
|
4
4
|
"description": "A state manager for real-time, collaborative apps, inspired by game development patterns and compatible with Redux.",
|
|
5
5
|
"author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
package/src/store.js
CHANGED
|
@@ -93,24 +93,12 @@ export function createStore({
|
|
|
93
93
|
|
|
94
94
|
// Handle special system events
|
|
95
95
|
if (event.type === "add") {
|
|
96
|
-
|
|
97
|
-
draft[id] = augmentEntity(id, entity)
|
|
98
|
-
const type = types[entity.type]
|
|
99
|
-
|
|
100
|
-
eventMap.addEntity(id, type, entity.type)
|
|
101
|
-
incomingEvents.unshift({ type: `#${id}:create` })
|
|
96
|
+
addEntity(draft, event.payload)
|
|
102
97
|
continue
|
|
103
98
|
}
|
|
104
99
|
|
|
105
100
|
if (event.type === "remove") {
|
|
106
|
-
|
|
107
|
-
const entity = draft[id]
|
|
108
|
-
const type = types[entity.type]
|
|
109
|
-
const typeName = entity.type
|
|
110
|
-
delete draft[id]
|
|
111
|
-
|
|
112
|
-
eventMap.removeEntity(id, type, typeName)
|
|
113
|
-
incomingEvents.unshift({ type: `#${id}:destroy` })
|
|
101
|
+
removeEntity(draft, event.payload)
|
|
114
102
|
continue
|
|
115
103
|
}
|
|
116
104
|
|
|
@@ -203,6 +191,13 @@ export function createStore({
|
|
|
203
191
|
eventMap.addEntity(id, newType, typeName)
|
|
204
192
|
}
|
|
205
193
|
}
|
|
194
|
+
|
|
195
|
+
if (autoCreateEntities && !state[typeName]) {
|
|
196
|
+
notify("add", {
|
|
197
|
+
id: typeName,
|
|
198
|
+
type: typeName,
|
|
199
|
+
})
|
|
200
|
+
}
|
|
206
201
|
}
|
|
207
202
|
|
|
208
203
|
/**
|
|
@@ -220,29 +215,7 @@ export function createStore({
|
|
|
220
215
|
*/
|
|
221
216
|
function setState(nextState) {
|
|
222
217
|
const oldEntities = state ?? {}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (autoCreateEntities) {
|
|
226
|
-
for (const typeName of Object.keys(types)) {
|
|
227
|
-
// Check if entity already exists
|
|
228
|
-
const hasEntity = Object.values(newEntities).some(
|
|
229
|
-
(entity) => entity.type === typeName,
|
|
230
|
-
)
|
|
231
|
-
|
|
232
|
-
if (!hasEntity) {
|
|
233
|
-
// No entity for this type → auto-create minimal entity
|
|
234
|
-
newEntities[typeName] = {
|
|
235
|
-
id: typeName,
|
|
236
|
-
type: typeName,
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
state = newEntities
|
|
243
|
-
eventMap = new EventMap(types, newEntities)
|
|
244
|
-
incomingEvents = []
|
|
245
|
-
isProcessing = false
|
|
218
|
+
let newEntities = augmentEntities(nextState)
|
|
246
219
|
|
|
247
220
|
const oldEntityIds = new Set(Object.keys(oldEntities))
|
|
248
221
|
const newEntityIds = new Set(Object.keys(newEntities))
|
|
@@ -254,13 +227,31 @@ export function createStore({
|
|
|
254
227
|
(id) => !newEntityIds.has(id),
|
|
255
228
|
)
|
|
256
229
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
230
|
+
state = oldEntities
|
|
231
|
+
eventMap = new EventMap(types, oldEntities)
|
|
232
|
+
incomingEvents = []
|
|
233
|
+
isProcessing = false
|
|
260
234
|
|
|
261
235
|
entitiesToDestroy.forEach((id) => {
|
|
262
|
-
|
|
236
|
+
removeEntity(state, id)
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
entitiesToCreate.forEach((id) => {
|
|
240
|
+
const entity = newEntities[id]
|
|
241
|
+
addEntity(state, { id, ...entity })
|
|
263
242
|
})
|
|
243
|
+
|
|
244
|
+
if (autoCreateEntities) {
|
|
245
|
+
for (const typeName of Object.keys(types)) {
|
|
246
|
+
const hasEntity = Object.values(state).some(
|
|
247
|
+
(entity) => entity.type === typeName,
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
if (!hasEntity) {
|
|
251
|
+
addEntity(state, { id: typeName, type: typeName })
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
264
255
|
}
|
|
265
256
|
|
|
266
257
|
/**
|
|
@@ -269,4 +260,24 @@ export function createStore({
|
|
|
269
260
|
function reset() {
|
|
270
261
|
setState(originalEntities)
|
|
271
262
|
}
|
|
263
|
+
|
|
264
|
+
function addEntity(draft, payload) {
|
|
265
|
+
const { id, ...entity } = payload
|
|
266
|
+
draft[id] = augmentEntity(id, entity)
|
|
267
|
+
const type = types[entity.type]
|
|
268
|
+
|
|
269
|
+
eventMap.addEntity(id, type, entity.type)
|
|
270
|
+
incomingEvents.unshift({ type: `#${id}:create` })
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function removeEntity(draft, payload) {
|
|
274
|
+
const id = payload
|
|
275
|
+
const entity = draft[id]
|
|
276
|
+
const type = types[entity.type]
|
|
277
|
+
const typeName = entity.type
|
|
278
|
+
delete draft[id]
|
|
279
|
+
|
|
280
|
+
eventMap.removeEntity(id, type, typeName)
|
|
281
|
+
incomingEvents.unshift({ type: `#${id}:destroy` })
|
|
282
|
+
}
|
|
272
283
|
}
|