@inglorious/store 12.6.2 → 12.7.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 +30 -1856
- package/package.json +1 -1
- package/src/behaviors/optimistic.js +1 -1
- package/src/behaviors/state-machine/fsm.js +1 -7
- package/src/behaviors/state-machine/scenes.js +8 -28
- package/src/client/devtools.js +33 -54
- package/src/client/event-filter.js +20 -0
- package/src/client/multiplayer-middleware.js +4 -8
- package/src/migration/rtk.js +1 -1
- package/src/store.js +337 -224
- package/types/store.d.ts +9 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/store",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.7.0",
|
|
4
4
|
"description": "A state manager for real-time, collaborative apps, inspired by game development patterns and compatible with Redux.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -187,7 +187,7 @@ function resolveAsyncMeta(behavior) {
|
|
|
187
187
|
|
|
188
188
|
const [runHandler, ...rest] = runHandlers
|
|
189
189
|
|
|
190
|
-
if (!runHandler
|
|
190
|
+
if (!runHandler || rest.length) {
|
|
191
191
|
throw new Error(
|
|
192
192
|
"optimistic expects a single handleAsync result or one '*Run' handler",
|
|
193
193
|
)
|
|
@@ -33,13 +33,7 @@ export function fsm(states) {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
/**
|
|
37
|
-
* A machine announces where it has landed, but only when it has actually moved: a
|
|
38
|
-
* handler that reads the state without changing it should not wake anything up.
|
|
39
|
-
*
|
|
40
|
-
* The announcement is broadcast and carries the id, rather than being addressed to
|
|
41
|
-
* this entity, because what wants to hear it is usually something else.
|
|
42
|
-
*/
|
|
36
|
+
/** Broadcasts `stateChange` when the handler actually moved the entity. */
|
|
43
37
|
function announce(entity, from, api) {
|
|
44
38
|
const to = entity.state
|
|
45
39
|
|
|
@@ -1,42 +1,22 @@
|
|
|
1
1
|
import { isDeepEqual } from "@inglorious/utils/object.js"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* that the play has, and two states of the same play have all of it in common.
|
|
10
|
-
*
|
|
11
|
-
* Only the difference between two states is touched. Anything they have in common is
|
|
12
|
-
* left standing as it is, which is what carries whatever the two share from one into the
|
|
13
|
-
* next without rebuilding it, and so without losing whatever the world has already done
|
|
14
|
-
* to it.
|
|
15
|
-
*
|
|
16
|
-
* A thing that stands on both sides but is not configured the same way is patched rather
|
|
17
|
-
* than rebuilt, so one state may take over part of it -- a keyboard whose keys mean
|
|
18
|
-
* something else there -- without the rest of it being torn up to make room.
|
|
4
|
+
* The entities that stand in each state of a machine, usually paired with
|
|
5
|
+
* `fsm`. On `create` and on its own `stateChange`, it adds the entities only
|
|
6
|
+
* the new state has, removes those only the old one had, and patches those both
|
|
7
|
+
* have but configure differently, compared by `id`, so what two states share
|
|
8
|
+
* survives the move untouched. The current list is kept in `scene`.
|
|
19
9
|
*
|
|
20
10
|
* @example
|
|
21
11
|
* ```js
|
|
22
|
-
* import { fsm } from "@inglorious/store/fsm"
|
|
23
|
-
* import { scenes } from "@inglorious/store/scenes"
|
|
24
|
-
*
|
|
25
|
-
* const SCENES = {
|
|
26
|
-
* title: () => [createTitleEntity(), createMenuEntity()],
|
|
27
|
-
* play: () => [createPaddleEntity(), createBallEntity()],
|
|
28
|
-
* }
|
|
29
|
-
*
|
|
30
12
|
* const types = {
|
|
31
13
|
* Game: [scenes(SCENES), fsm({ title: { start(e) { e.state = "play" } } })],
|
|
32
14
|
* }
|
|
33
|
-
*
|
|
34
|
-
* const entities = { game: { type: "Game", state: "title" } }
|
|
35
15
|
* ```
|
|
36
16
|
*
|
|
37
|
-
* @param {object} scenesByState -
|
|
38
|
-
*
|
|
39
|
-
* @returns {object} The
|
|
17
|
+
* @param {Object<string, (entity: object) => object[]>} scenesByState - The
|
|
18
|
+
* entities to have standing in each state, given the entity running the machine.
|
|
19
|
+
* @returns {object} The behavior.
|
|
40
20
|
*/
|
|
41
21
|
export function scenes(scenesByState) {
|
|
42
22
|
function build(entity, state, api) {
|
package/src/client/devtools.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { deserialize } from "@inglorious/utils/object.js"
|
|
2
2
|
|
|
3
|
+
import { createEventFilter } from "./event-filter.js"
|
|
4
|
+
|
|
3
5
|
const LAST_STATE = 1
|
|
4
6
|
|
|
5
7
|
const globalContainer = import.meta.hot
|
|
@@ -7,6 +9,11 @@ const globalContainer = import.meta.hot
|
|
|
7
9
|
: {}
|
|
8
10
|
|
|
9
11
|
export function createDevtools(config = {}) {
|
|
12
|
+
const shouldLog =
|
|
13
|
+
(config.updateMode ?? "auto") === "auto"
|
|
14
|
+
? createEventFilter(config)
|
|
15
|
+
: () => false
|
|
16
|
+
|
|
10
17
|
return {
|
|
11
18
|
connect(store) {
|
|
12
19
|
connectDevTools(store, config)
|
|
@@ -26,7 +33,7 @@ export function createDevtools(config = {}) {
|
|
|
26
33
|
const next = store.dispatch
|
|
27
34
|
store.dispatch = (event) => {
|
|
28
35
|
const result = next(event)
|
|
29
|
-
if (
|
|
36
|
+
if (shouldLog(event)) {
|
|
30
37
|
sendAction(event, store.getState())
|
|
31
38
|
}
|
|
32
39
|
return result
|
|
@@ -42,34 +49,16 @@ function connectDevTools(store, config = {}) {
|
|
|
42
49
|
|
|
43
50
|
const existing = getConnection()
|
|
44
51
|
if (existing) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
existing.restoreSetState?.()
|
|
48
|
-
const baseSetState = store.setState
|
|
49
|
-
const restoreSetState =
|
|
50
|
-
updateMode === "auto"
|
|
51
|
-
? () => {
|
|
52
|
-
if (store.setState !== baseSetState) {
|
|
53
|
-
store.setState = baseSetState
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
: () => {
|
|
57
|
-
// no-op when not wrapping setState
|
|
58
|
-
}
|
|
59
|
-
if (updateMode === "auto") {
|
|
60
|
-
store.setState = (newState) => {
|
|
61
|
-
baseSetState(newState)
|
|
62
|
-
sendAction({ type: "stateInit", payload: newState }, store.getState())
|
|
63
|
-
}
|
|
64
|
-
}
|
|
52
|
+
if (existing.updateMode === updateMode) {
|
|
53
|
+
existing.restoreSetState()
|
|
65
54
|
existing.store = store
|
|
66
|
-
existing.restoreSetState =
|
|
55
|
+
existing.restoreSetState = wrapSetState(store, updateMode)
|
|
67
56
|
}
|
|
68
57
|
|
|
69
58
|
try {
|
|
70
59
|
existing.unsubscribe?.()
|
|
71
60
|
} finally {
|
|
72
|
-
existing.restoreSetState
|
|
61
|
+
existing.restoreSetState()
|
|
73
62
|
clearConnection()
|
|
74
63
|
}
|
|
75
64
|
}
|
|
@@ -79,23 +68,7 @@ function connectDevTools(store, config = {}) {
|
|
|
79
68
|
}
|
|
80
69
|
|
|
81
70
|
const name = config.name ?? document.title
|
|
82
|
-
const
|
|
83
|
-
const restoreSetState =
|
|
84
|
-
updateMode === "auto"
|
|
85
|
-
? () => {
|
|
86
|
-
if (store.setState !== baseSetState) {
|
|
87
|
-
store.setState = baseSetState
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
: () => {
|
|
91
|
-
// noop; we didn't wrap setState
|
|
92
|
-
}
|
|
93
|
-
if (updateMode === "auto") {
|
|
94
|
-
store.setState = (newState) => {
|
|
95
|
-
baseSetState(newState)
|
|
96
|
-
sendAction({ type: "stateInit", payload: newState }, store.getState())
|
|
97
|
-
}
|
|
98
|
-
}
|
|
71
|
+
const restoreSetState = wrapSetState(store, updateMode)
|
|
99
72
|
|
|
100
73
|
const devToolsInstance = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
|
|
101
74
|
name,
|
|
@@ -136,6 +109,26 @@ function connectDevTools(store, config = {}) {
|
|
|
136
109
|
})
|
|
137
110
|
}
|
|
138
111
|
|
|
112
|
+
/**
|
|
113
|
+
* In auto mode, reports each `setState` to the devtools as a `stateInit` action.
|
|
114
|
+
* @returns {() => void} Puts the store's own `setState` back.
|
|
115
|
+
*/
|
|
116
|
+
function wrapSetState(store, updateMode) {
|
|
117
|
+
if (updateMode !== "auto") return () => {}
|
|
118
|
+
|
|
119
|
+
const baseSetState = store.setState
|
|
120
|
+
store.setState = (newState) => {
|
|
121
|
+
baseSetState(newState)
|
|
122
|
+
sendAction({ type: "stateInit", payload: newState }, store.getState())
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return () => {
|
|
126
|
+
if (store.setState !== baseSetState) {
|
|
127
|
+
store.setState = baseSetState
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
139
132
|
function disconnectDevTools() {
|
|
140
133
|
const existing = getConnection()
|
|
141
134
|
if (!existing) return
|
|
@@ -217,17 +210,3 @@ function handleAction(message) {
|
|
|
217
210
|
const action = deserialize(message.payload)
|
|
218
211
|
store.dispatch(action)
|
|
219
212
|
}
|
|
220
|
-
|
|
221
|
-
function shouldLogEvent(event, config) {
|
|
222
|
-
const {
|
|
223
|
-
updateMode = "auto",
|
|
224
|
-
blacklist = [],
|
|
225
|
-
whitelist = [],
|
|
226
|
-
filter = null,
|
|
227
|
-
} = config
|
|
228
|
-
if (updateMode !== "auto") return false
|
|
229
|
-
const passesBlacklist = !blacklist.length || !blacklist.includes(event.type)
|
|
230
|
-
const passesWhitelist = !whitelist.length || whitelist.includes(event.type)
|
|
231
|
-
const passesFilter = !filter || filter(event)
|
|
232
|
-
return passesBlacklist && passesWhitelist && passesFilter
|
|
233
|
-
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an event passes a blacklist, a whitelist and a predicate, as the
|
|
3
|
+
* devtools and the multiplayer middleware filter the events they relay.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} [config]
|
|
6
|
+
* @param {string[]} [config.blacklist] - Event types never relayed.
|
|
7
|
+
* @param {string[]} [config.whitelist] - The only event types relayed, when given.
|
|
8
|
+
* @param {(event: { type: string, payload?: any }) => boolean} [config.filter] - A last say on each event.
|
|
9
|
+
* @returns {(event: { type: string, payload?: any }) => boolean}
|
|
10
|
+
*/
|
|
11
|
+
export function createEventFilter({
|
|
12
|
+
blacklist = [],
|
|
13
|
+
whitelist = [],
|
|
14
|
+
filter = null,
|
|
15
|
+
} = {}) {
|
|
16
|
+
return (event) =>
|
|
17
|
+
(!blacklist.length || !blacklist.includes(event.type)) &&
|
|
18
|
+
(!whitelist.length || whitelist.includes(event.type)) &&
|
|
19
|
+
(!filter || filter(event))
|
|
20
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { deserialize, extend, serialize } from "@inglorious/utils/object.js"
|
|
2
2
|
|
|
3
|
+
import { createEventFilter } from "./event-filter.js"
|
|
4
|
+
|
|
3
5
|
const DEFAULT_RECONNECTION_DELAY = 1000
|
|
4
6
|
|
|
5
7
|
/**
|
|
@@ -11,9 +13,7 @@ export function multiplayerMiddleware(config = {}) {
|
|
|
11
13
|
const reconnectionDelay =
|
|
12
14
|
config.reconnectionDelay ?? DEFAULT_RECONNECTION_DELAY
|
|
13
15
|
|
|
14
|
-
const
|
|
15
|
-
const whitelist = config.whitelist ?? []
|
|
16
|
-
const filter = config.filter ?? null
|
|
16
|
+
const shouldSend = createEventFilter(config)
|
|
17
17
|
|
|
18
18
|
let ws = null
|
|
19
19
|
const localQueue = []
|
|
@@ -29,11 +29,7 @@ export function multiplayerMiddleware(config = {}) {
|
|
|
29
29
|
* passes it on to the next middleware, and eventually to the store.
|
|
30
30
|
*/
|
|
31
31
|
function send(store, event, next) {
|
|
32
|
-
|
|
33
|
-
(!blacklist.length || !blacklist.includes(event.type)) &&
|
|
34
|
-
(!whitelist.length || whitelist.includes(event.type)) &&
|
|
35
|
-
(!filter || filter(event))
|
|
36
|
-
if (!shouldBeSent) {
|
|
32
|
+
if (!shouldSend(event)) {
|
|
37
33
|
return next(event)
|
|
38
34
|
}
|
|
39
35
|
|
package/src/migration/rtk.js
CHANGED
|
@@ -356,7 +356,7 @@ export function createMigrationGuide(slice) {
|
|
|
356
356
|
lines.push(` const { someField } = api.getEntity('${slice.name}')`)
|
|
357
357
|
lines.push(" // or")
|
|
358
358
|
lines.push(
|
|
359
|
-
` const data = useSelector(state => state
|
|
359
|
+
` const data = useSelector(state => state.${slice.name}.someField)`,
|
|
360
360
|
)
|
|
361
361
|
|
|
362
362
|
return lines.join("\n")
|