@inglorious/store 9.5.0 → 9.5.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/store.js +31 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "9.5.0",
3
+ "version": "9.5.2",
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
@@ -30,6 +30,7 @@ export function createStore({
30
30
  const types = augmentTypes(originalTypes)
31
31
 
32
32
  let state, eventMap, incomingEvents, isProcessing
33
+ reset()
33
34
 
34
35
  const baseStore = {
35
36
  subscribe,
@@ -49,7 +50,11 @@ export function createStore({
49
50
  : baseStore
50
51
  const api = createApi(store, store.extras)
51
52
  store._api = api
52
- reset()
53
+
54
+ if (updateMode === "auto" && incomingEvents.length) {
55
+ update()
56
+ }
57
+
53
58
  return store
54
59
 
55
60
  /**
@@ -93,24 +98,12 @@ export function createStore({
93
98
 
94
99
  // Handle special system events
95
100
  if (event.type === "add") {
96
- const { id, ...entity } = event.payload
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` })
101
+ addEntity(draft, event.payload)
102
102
  continue
103
103
  }
104
104
 
105
105
  if (event.type === "remove") {
106
- const id = event.payload
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` })
106
+ removeEntity(draft, event.payload)
114
107
  continue
115
108
  }
116
109
 
@@ -245,12 +238,12 @@ export function createStore({
245
238
  isProcessing = false
246
239
 
247
240
  entitiesToDestroy.forEach((id) => {
248
- notify("remove", id)
241
+ removeEntity(state, id)
249
242
  })
250
243
 
251
244
  entitiesToCreate.forEach((id) => {
252
245
  const entity = newEntities[id]
253
- notify("add", { id, ...entity })
246
+ addEntity(state, { id, ...entity })
254
247
  })
255
248
 
256
249
  if (autoCreateEntities) {
@@ -260,10 +253,7 @@ export function createStore({
260
253
  )
261
254
 
262
255
  if (!hasEntity) {
263
- notify("add", {
264
- id: typeName,
265
- type: typeName,
266
- })
256
+ addEntity(state, { id: typeName, type: typeName })
267
257
  }
268
258
  }
269
259
  }
@@ -275,4 +265,24 @@ export function createStore({
275
265
  function reset() {
276
266
  setState(originalEntities)
277
267
  }
268
+
269
+ function addEntity(draft, payload) {
270
+ const { id, ...entity } = payload
271
+ draft[id] = augmentEntity(id, entity)
272
+ const type = types[entity.type]
273
+
274
+ eventMap.addEntity(id, type, entity.type)
275
+ incomingEvents.unshift({ type: `#${id}:create` })
276
+ }
277
+
278
+ function removeEntity(draft, payload) {
279
+ const id = payload
280
+ const entity = draft[id]
281
+ const type = types[entity.type]
282
+ const typeName = entity.type
283
+ delete draft[id]
284
+
285
+ eventMap.removeEntity(id, type, typeName)
286
+ incomingEvents.unshift({ type: `#${id}:destroy` })
287
+ }
278
288
  }