@inglorious/store 7.1.3 → 8.0.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 CHANGED
@@ -367,18 +367,16 @@ Inglorious Store has a few built-in events that you can use:
367
367
 
368
368
  The lifecycle events can be used to define event handlers similar to constructor and destructor methods in OOP:
369
369
 
370
- > Remember: events are broadcast to all entities, just like with reducers! Each handler decides if it should respond. More on that in the section below.
370
+ > Note: these special lifecycle events are not broadcast: they are visible only to the added/removed entity!
371
371
 
372
372
  ```javascript
373
373
  const types = {
374
374
  counter: {
375
- create(entity, id) {
376
- if (entity.id !== id) return // "are you talking to me?"
375
+ create(entity) {
377
376
  entity.createdAt = Date.now()
378
377
  },
379
378
 
380
- destroy(entity, id) {
381
- if (entity.id !== id) return // "are you talking to me?"
379
+ destroy(entity) {
382
380
  entity.destroyedAt = Date.now()
383
381
  },
384
382
  },
@@ -721,11 +719,11 @@ Each handler receives three arguments:
721
719
  - `getTypes()` - type definitions (for middleware)
722
720
  - `getType(typeName)` - type definition (for overriding)
723
721
 
724
- ### Built-in Lifecycle Events
722
+ ### Built-in Events
725
723
 
726
- - **`create(entity, id)`** - triggered when entity added via `add` event
727
- - **`destroy(entity, id)`** - triggered when entity removed via `remove` event
728
- - **`morph(entity, newType)`** - triggered when entity type changes
724
+ - **`create(entity)`** - triggered when entity added via `add` event, visible only to that entity
725
+ - **`destroy(entity)`** - triggered when entity removed via `remove` event, visible only to that entity
726
+ - **`morph(typeName, newType)`** - used to change the behavior of a type on the fly
729
727
 
730
728
  ### Notify vs Dispatch
731
729
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "7.1.3",
3
+ "version": "8.0.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",
@@ -5,7 +5,7 @@ const LAST_STATE = 1
5
5
  // HMR-safe container to persist a single DevTools connection across Vite reloads.
6
6
  // Uses import.meta.hot.data when available (preferred), falls back to module scope.
7
7
  const globalContainer = import.meta.hot
8
- ? (import.meta.hot.data.__INGLORIOUS_DEVTOOLS__ ??= {})
8
+ ? ((import.meta.hot.data ??= {}).__INGLORIOUS_DEVTOOLS__ ??= {})
9
9
  : {}
10
10
 
11
11
  // connection shape: {
package/src/store.js CHANGED
@@ -17,12 +17,12 @@ import { augmentType, augmentTypes } from "./types.js"
17
17
  * @returns {Object} The store with methods to interact with state and events.
18
18
  */
19
19
  export function createStore({
20
- types: originalTypes,
21
- entities: originalEntities,
20
+ types: originalTypes = {},
21
+ entities: originalEntities = {},
22
22
  systems = [],
23
23
  middlewares = [],
24
24
  updateMode = "auto",
25
- }) {
25
+ } = {}) {
26
26
  const listeners = new Set()
27
27
 
28
28
  const types = augmentTypes(originalTypes)
@@ -90,18 +90,20 @@ export function createStore({
90
90
 
91
91
  // Handle special system events
92
92
  if (event.type === "morph") {
93
- const { id, type } = event.payload
93
+ const { name, type } = event.payload
94
94
 
95
- const entity = draft[id]
96
- const oldType = types[entity.type]
97
- const oldTypeName = entity.type
95
+ const oldType = types[name]
98
96
 
99
- originalTypes[entity.type] = type
100
- types[entity.type] = augmentType(originalTypes[entity.type])
101
- const newType = types[entity.type]
97
+ originalTypes[name] = type
98
+ types[name] = augmentType(type)
99
+ const newType = types[name]
102
100
 
103
- eventMap.removeEntity(id, oldType, oldTypeName)
104
- eventMap.addEntity(id, newType, oldTypeName) // Use the original type name
101
+ for (const [id, entity] of Object.entries(draft)) {
102
+ if (entity.type === name) {
103
+ eventMap.removeEntity(id, oldType, name)
104
+ eventMap.addEntity(id, newType, name)
105
+ }
106
+ }
105
107
  continue
106
108
  }
107
109
 
@@ -111,7 +113,7 @@ export function createStore({
111
113
  const type = types[entity.type]
112
114
 
113
115
  eventMap.addEntity(id, type, entity.type)
114
- incomingEvents.unshift({ type: "create", payload: id })
116
+ incomingEvents.unshift({ type: `#${id}:create` })
115
117
  continue
116
118
  }
117
119
 
@@ -123,7 +125,7 @@ export function createStore({
123
125
  delete draft[id]
124
126
 
125
127
  eventMap.removeEntity(id, type, typeName)
126
- incomingEvents.unshift({ type: "destroy", payload: id })
128
+ incomingEvents.unshift({ type: `#${id}:destroy` })
127
129
  continue
128
130
  }
129
131
 
@@ -231,11 +233,11 @@ export function createStore({
231
233
  )
232
234
 
233
235
  entitiesToCreate.forEach((id) => {
234
- incomingEvents.push({ type: "create", payload: id })
236
+ incomingEvents.push({ type: `#${id}:create` })
235
237
  })
236
238
 
237
239
  entitiesToDestroy.forEach((id) => {
238
- incomingEvents.push({ type: "destroy", payload: id })
240
+ incomingEvents.push({ type: `#${id}:destroy` })
239
241
  })
240
242
  }
241
243
 
package/src/store.test.js CHANGED
@@ -169,7 +169,7 @@ test("it should change an entity's behavior via a 'morph' event", () => {
169
169
  bug: { id: "bug", type: "bug", isFull: true },
170
170
  })
171
171
 
172
- store.notify("morph", { id: "bug", type: [Caterpillar, Butterfly] })
172
+ store.notify("morph", { name: "bug", type: [Caterpillar, Butterfly] })
173
173
  store.notify("fly")
174
174
  store.update()
175
175
  expect(store.getState()).toStrictEqual({