@inglorious/store 1.2.0 → 2.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/package.json +1 -1
- package/src/store.js +1 -3
- package/src/store.test.js +40 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/store",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A state manager inspired by Redux, but tailored for the specific needs of game development.",
|
|
5
5
|
"author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
package/src/store.js
CHANGED
|
@@ -56,12 +56,10 @@ export function createStore({
|
|
|
56
56
|
* @param {number} dt - The delta time since the last update in milliseconds.
|
|
57
57
|
* @param {Object} api - The engine's public API.
|
|
58
58
|
*/
|
|
59
|
-
function update(
|
|
59
|
+
function update(api) {
|
|
60
60
|
const processedEvents = []
|
|
61
61
|
|
|
62
62
|
state = produce(state, (state) => {
|
|
63
|
-
incomingEvents.push({ type: "update", payload: dt })
|
|
64
|
-
|
|
65
63
|
while (incomingEvents.length) {
|
|
66
64
|
const event = incomingEvents.shift()
|
|
67
65
|
processedEvents.push(event)
|
package/src/store.test.js
CHANGED
|
@@ -3,6 +3,37 @@ import { expect, test } from "vitest"
|
|
|
3
3
|
import { createStore } from "./store.js"
|
|
4
4
|
|
|
5
5
|
test("it should process events by mutating state inside handlers", () => {
|
|
6
|
+
const config = {
|
|
7
|
+
types: {
|
|
8
|
+
kitty: {
|
|
9
|
+
feed(entity) {
|
|
10
|
+
entity.isFed = true
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
entities: {
|
|
15
|
+
kitty1: { type: "kitty" },
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
const afterState = {
|
|
19
|
+
entities: {
|
|
20
|
+
kitty1: {
|
|
21
|
+
id: "kitty1",
|
|
22
|
+
type: "kitty",
|
|
23
|
+
isFed: true,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const store = createStore(config)
|
|
29
|
+
store.notify("feed")
|
|
30
|
+
store.update()
|
|
31
|
+
|
|
32
|
+
const state = store.getState()
|
|
33
|
+
expect(state).toStrictEqual(afterState)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test("it should process an event queue in the same update cycle", () => {
|
|
6
37
|
const config = {
|
|
7
38
|
types: {
|
|
8
39
|
kitty: {
|
|
@@ -31,7 +62,8 @@ test("it should process events by mutating state inside handlers", () => {
|
|
|
31
62
|
|
|
32
63
|
const store = createStore(config)
|
|
33
64
|
store.notify("feed")
|
|
34
|
-
store.update
|
|
65
|
+
store.notify("update")
|
|
66
|
+
store.update()
|
|
35
67
|
|
|
36
68
|
const state = store.getState()
|
|
37
69
|
expect(state).toStrictEqual(afterState)
|
|
@@ -65,7 +97,8 @@ test("it should send an event from an entity and process it in the same update c
|
|
|
65
97
|
|
|
66
98
|
const store = createStore(config)
|
|
67
99
|
const api = { notify: store.notify }
|
|
68
|
-
store.update
|
|
100
|
+
store.notify("update")
|
|
101
|
+
store.update(api)
|
|
69
102
|
|
|
70
103
|
const state = store.getState()
|
|
71
104
|
expect(state).toStrictEqual(afterState)
|
|
@@ -82,7 +115,7 @@ test("it should add an entity via an 'add' event", () => {
|
|
|
82
115
|
|
|
83
116
|
const store = createStore(config)
|
|
84
117
|
store.notify("add", newEntity)
|
|
85
|
-
store.update(
|
|
118
|
+
store.update()
|
|
86
119
|
|
|
87
120
|
const state = store.getState()
|
|
88
121
|
expect(state).toStrictEqual({
|
|
@@ -102,8 +135,7 @@ test("it should remove an entity via a 'remove' event", () => {
|
|
|
102
135
|
const store = createStore(config)
|
|
103
136
|
|
|
104
137
|
store.notify("remove", "kitty1")
|
|
105
|
-
|
|
106
|
-
store.update(0, {})
|
|
138
|
+
store.update()
|
|
107
139
|
|
|
108
140
|
const state = store.getState()
|
|
109
141
|
expect(state.entities.kitty1).toBeUndefined()
|
|
@@ -134,7 +166,8 @@ test("it should change an entity's behavior via a 'morph' event", () => {
|
|
|
134
166
|
const store = createStore(config)
|
|
135
167
|
|
|
136
168
|
store.notify("eat")
|
|
137
|
-
store.update(
|
|
169
|
+
store.update()
|
|
170
|
+
|
|
138
171
|
expect(store.getState()).toStrictEqual({
|
|
139
172
|
entities: {
|
|
140
173
|
bug: { id: "bug", type: "bug", isFull: true },
|
|
@@ -143,7 +176,7 @@ test("it should change an entity's behavior via a 'morph' event", () => {
|
|
|
143
176
|
|
|
144
177
|
store.notify("morph", { id: "bug", type: [Caterpillar, Butterfly] })
|
|
145
178
|
store.notify("fly")
|
|
146
|
-
store.update(
|
|
179
|
+
store.update()
|
|
147
180
|
expect(store.getState()).toStrictEqual({
|
|
148
181
|
entities: {
|
|
149
182
|
bug: { id: "bug", type: "bug", isFull: true, hasFlown: true },
|