@inglorious/store 8.0.1 → 9.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
@@ -30,7 +30,7 @@ Game engines solved state complexity years ago — Inglorious Store brings those
30
30
  - ✅ Entity-based state (manage multiple instances effortlessly)
31
31
  - ✅ No action creators, thunks, or slices
32
32
  - ✅ Predictable, testable, purely functional code
33
- - ✅ Built-in lifecycle events (`add`, `remove`, `morph`)
33
+ - ✅ Built-in lifecycle events (`add`, `remove`)
34
34
  - ✅ 10x faster immutability than Redux Toolkit (Mutative vs Immer)
35
35
 
36
36
  ---
@@ -363,7 +363,6 @@ Inglorious Store has a few built-in events that you can use:
363
363
 
364
364
  - `add`: adds a new entity to the state. Triggers a `create` lifecycle event.
365
365
  - `remove`: removes an entity from the state. Triggers a `destroy` lifecycle event.
366
- - `morph`: changes the behavior of a type (advanced, used by middlewares/rendering systems)
367
366
 
368
367
  The lifecycle events can be used to define event handlers similar to constructor and destructor methods in OOP:
369
368
 
@@ -718,12 +717,12 @@ Each handler receives three arguments:
718
717
  - `dispatch(action)` - optional, if you prefer Redux-style dispatching
719
718
  - `getTypes()` - type definitions (for middleware)
720
719
  - `getType(typeName)` - type definition (for overriding)
720
+ - `setType(typeName, type)` - change the behavior of a type
721
721
 
722
722
  ### Built-in Events
723
723
 
724
724
  - **`create(entity)`** - triggered when entity added via `add` event, visible only to that entity
725
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
727
726
 
728
727
  ### Notify vs Dispatch
729
728
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "8.0.1",
3
+ "version": "9.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",
@@ -43,8 +43,8 @@
43
43
  },
44
44
  "files": [
45
45
  "src",
46
- "!src/**/*.test.js",
47
- "types"
46
+ "types",
47
+ "!src/**/*.test.js"
48
48
  ],
49
49
  "publishConfig": {
50
50
  "access": "public"
package/src/api.js CHANGED
@@ -2,6 +2,7 @@ export function createApi(store, extras) {
2
2
  return {
3
3
  getTypes: store.getTypes,
4
4
  getType: store.getType,
5
+ setType: store.setType,
5
6
  getEntities: store.getState,
6
7
  getEntity: (id) => store.getState()[id],
7
8
  dispatch: store.dispatch,
package/src/event-map.js CHANGED
@@ -44,7 +44,7 @@ export class EventMap {
44
44
 
45
45
  /**
46
46
  * Adds an entity's ID to the Sets for all event handlers defined in its type.
47
- * This should be called when a new entity is created or its type is morphed.
47
+ * This should be called when a new entity is created or its type is changed via setType.
48
48
  *
49
49
  * @param {string} entityId - The ID of the entity.
50
50
  * @param {Type} type - The augmented type object of the entity.
@@ -71,7 +71,7 @@ export class EventMap {
71
71
 
72
72
  /**
73
73
  * Removes an entity's ID from the Sets for all event handlers defined in its type.
74
- * This should be called when an entity is removed or its type is morphed.
74
+ * This should be called when an entity is removed or its type is changed via setType.
75
75
  *
76
76
  * @param {string} entityId - The ID of the entity.
77
77
  * @param {Type} type - The augmented type object of the entity.
package/src/store.js CHANGED
@@ -37,6 +37,7 @@ export function createStore({
37
37
  dispatch, // needed for compatibility with Redux
38
38
  getTypes,
39
39
  getType,
40
+ setType,
40
41
  getState,
41
42
  setState,
42
43
  reset,
@@ -89,24 +90,6 @@ export function createStore({
89
90
  processedEvents.push(event)
90
91
 
91
92
  // Handle special system events
92
- if (event.type === "morph") {
93
- const { name, type } = event.payload
94
-
95
- const oldType = types[name]
96
-
97
- originalTypes[name] = type
98
- types[name] = augmentType(type)
99
- const newType = types[name]
100
-
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
- }
107
- continue
108
- }
109
-
110
93
  if (event.type === "add") {
111
94
  const { id, ...entity } = event.payload
112
95
  draft[id] = augmentEntity(id, entity)
@@ -200,6 +183,26 @@ export function createStore({
200
183
  return types[typeName]
201
184
  }
202
185
 
186
+ /**
187
+ * Sets an augmented type configuration given its name.
188
+ * @param {string} typeName - The name of the type to set.
189
+ * @param {Object} type - The type configuration.
190
+ */
191
+ function setType(typeName, type) {
192
+ const oldType = types[typeName]
193
+
194
+ originalTypes[typeName] = type
195
+ types[typeName] = augmentType(type)
196
+ const newType = types[typeName]
197
+
198
+ for (const [id, entity] of Object.entries(state)) {
199
+ if (entity.type === typeName) {
200
+ eventMap.removeEntity(id, oldType, typeName)
201
+ eventMap.addEntity(id, newType, typeName)
202
+ }
203
+ }
204
+ }
205
+
203
206
  /**
204
207
  * Retrieves the current state.
205
208
  * @returns {Object} The current state.
package/types/store.d.ts CHANGED
@@ -75,6 +75,7 @@ export interface Api<
75
75
  > {
76
76
  getTypes: () => TypesConfig<TEntity>
77
77
  getType: (typeName: string) => EntityType<TEntity>
78
+ setType: (typeName: string, type: EntityType<TEntity>) => void
78
79
  getEntities: () => TState
79
80
  getEntity: (id: string) => TEntity | undefined
80
81
  dispatch: (event: Event) => void
@@ -95,6 +96,7 @@ export interface Store<
95
96
  dispatch: (event: Event) => void
96
97
  getTypes: () => TypesConfig<TEntity>
97
98
  getType: (typeName: string) => EntityType<TEntity>
99
+ setType: (typeName: string, type: EntityType<TEntity>) => void
98
100
  getState: () => TState
99
101
  setState: (nextState: TState) => void
100
102
  reset: () => void