@inglorious/store 9.4.0 → 9.5.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/store.js +34 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "9.4.0",
3
+ "version": "9.5.0",
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,7 +30,6 @@ export function createStore({
30
30
  const types = augmentTypes(originalTypes)
31
31
 
32
32
  let state, eventMap, incomingEvents, isProcessing
33
- reset()
34
33
 
35
34
  const baseStore = {
36
35
  subscribe,
@@ -50,6 +49,7 @@ export function createStore({
50
49
  : baseStore
51
50
  const api = createApi(store, store.extras)
52
51
  store._api = api
52
+ reset()
53
53
  return store
54
54
 
55
55
  /**
@@ -203,6 +203,13 @@ export function createStore({
203
203
  eventMap.addEntity(id, newType, typeName)
204
204
  }
205
205
  }
206
+
207
+ if (autoCreateEntities && !state[typeName]) {
208
+ notify("add", {
209
+ id: typeName,
210
+ type: typeName,
211
+ })
212
+ }
206
213
  }
207
214
 
208
215
  /**
@@ -220,29 +227,7 @@ export function createStore({
220
227
  */
221
228
  function setState(nextState) {
222
229
  const oldEntities = state ?? {}
223
- const newEntities = augmentEntities(nextState)
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
230
+ let newEntities = augmentEntities(nextState)
246
231
 
247
232
  const oldEntityIds = new Set(Object.keys(oldEntities))
248
233
  const newEntityIds = new Set(Object.keys(newEntities))
@@ -254,13 +239,34 @@ export function createStore({
254
239
  (id) => !newEntityIds.has(id),
255
240
  )
256
241
 
257
- entitiesToCreate.forEach((id) => {
258
- incomingEvents.push({ type: `#${id}:create` })
259
- })
242
+ state = oldEntities
243
+ eventMap = new EventMap(types, oldEntities)
244
+ incomingEvents = []
245
+ isProcessing = false
260
246
 
261
247
  entitiesToDestroy.forEach((id) => {
262
- incomingEvents.push({ type: `#${id}:destroy` })
248
+ notify("remove", id)
249
+ })
250
+
251
+ entitiesToCreate.forEach((id) => {
252
+ const entity = newEntities[id]
253
+ notify("add", { id, ...entity })
263
254
  })
255
+
256
+ if (autoCreateEntities) {
257
+ for (const typeName of Object.keys(types)) {
258
+ const hasEntity = Object.values(state).some(
259
+ (entity) => entity.type === typeName,
260
+ )
261
+
262
+ if (!hasEntity) {
263
+ notify("add", {
264
+ id: typeName,
265
+ type: typeName,
266
+ })
267
+ }
268
+ }
269
+ }
264
270
  }
265
271
 
266
272
  /**