@inglorious/store 9.5.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/store.js +26 -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.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
@@ -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,6 @@ export function createStore({
49
50
  : baseStore
50
51
  const api = createApi(store, store.extras)
51
52
  store._api = api
52
- reset()
53
53
  return store
54
54
 
55
55
  /**
@@ -93,24 +93,12 @@ export function createStore({
93
93
 
94
94
  // Handle special system events
95
95
  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` })
96
+ addEntity(draft, event.payload)
102
97
  continue
103
98
  }
104
99
 
105
100
  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` })
101
+ removeEntity(draft, event.payload)
114
102
  continue
115
103
  }
116
104
 
@@ -245,12 +233,12 @@ export function createStore({
245
233
  isProcessing = false
246
234
 
247
235
  entitiesToDestroy.forEach((id) => {
248
- notify("remove", id)
236
+ removeEntity(state, id)
249
237
  })
250
238
 
251
239
  entitiesToCreate.forEach((id) => {
252
240
  const entity = newEntities[id]
253
- notify("add", { id, ...entity })
241
+ addEntity(state, { id, ...entity })
254
242
  })
255
243
 
256
244
  if (autoCreateEntities) {
@@ -260,10 +248,7 @@ export function createStore({
260
248
  )
261
249
 
262
250
  if (!hasEntity) {
263
- notify("add", {
264
- id: typeName,
265
- type: typeName,
266
- })
251
+ addEntity(state, { id: typeName, type: typeName })
267
252
  }
268
253
  }
269
254
  }
@@ -275,4 +260,24 @@ export function createStore({
275
260
  function reset() {
276
261
  setState(originalEntities)
277
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
+ }
278
283
  }