@inglorious/store 8.0.1 → 9.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "8.0.1",
3
+ "version": "9.0.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",
@@ -43,18 +43,18 @@
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"
51
51
  },
52
52
  "dependencies": {
53
53
  "mutative": "^1.3.0",
54
- "@inglorious/utils": "3.7.1"
54
+ "@inglorious/utils": "3.7.2"
55
55
  },
56
56
  "peerDependencies": {
57
- "@inglorious/utils": "3.7.1"
57
+ "@inglorious/utils": "3.7.2"
58
58
  },
59
59
  "devDependencies": {
60
60
  "prettier": "^3.6.2",
@@ -66,8 +66,8 @@
66
66
  "node": ">= 22"
67
67
  },
68
68
  "scripts": {
69
- "format": "prettier --write '**/*.{js,jsx}'",
70
- "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
69
+ "format": "prettier --write .",
70
+ "lint": "eslint .",
71
71
  "test:watch": "vitest",
72
72
  "test": "vitest run"
73
73
  }
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