@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 +7 -9
- package/package.json +1 -1
- package/src/client/devtools.js +1 -1
- package/src/store.js +18 -16
- package/src/store.test.js +1 -1
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
|
-
>
|
|
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
|
|
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
|
|
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
|
|
722
|
+
### Built-in Events
|
|
725
723
|
|
|
726
|
-
- **`create(entity
|
|
727
|
-
- **`destroy(entity
|
|
728
|
-
- **`morph(
|
|
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": "
|
|
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",
|
package/src/client/devtools.js
CHANGED
|
@@ -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 {
|
|
93
|
+
const { name, type } = event.payload
|
|
94
94
|
|
|
95
|
-
const
|
|
96
|
-
const oldType = types[entity.type]
|
|
97
|
-
const oldTypeName = entity.type
|
|
95
|
+
const oldType = types[name]
|
|
98
96
|
|
|
99
|
-
originalTypes[
|
|
100
|
-
types[
|
|
101
|
-
const newType = types[
|
|
97
|
+
originalTypes[name] = type
|
|
98
|
+
types[name] = augmentType(type)
|
|
99
|
+
const newType = types[name]
|
|
102
100
|
|
|
103
|
-
|
|
104
|
-
|
|
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:
|
|
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:
|
|
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:
|
|
236
|
+
incomingEvents.push({ type: `#${id}:create` })
|
|
235
237
|
})
|
|
236
238
|
|
|
237
239
|
entitiesToDestroy.forEach((id) => {
|
|
238
|
-
incomingEvents.push({ type:
|
|
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", {
|
|
172
|
+
store.notify("morph", { name: "bug", type: [Caterpillar, Butterfly] })
|
|
173
173
|
store.notify("fly")
|
|
174
174
|
store.update()
|
|
175
175
|
expect(store.getState()).toStrictEqual({
|