@inglorious/store 12.0.0 → 12.1.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 +2 -2
- package/src/client/devtools.js +5 -1
- package/src/client/multiplayer-middleware.js +11 -1
- package/src/middlewares.js +13 -34
- package/src/store.js +3 -4
- package/types/store.d.ts +3 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/store",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.1.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",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"@inglorious/utils": "5.1.0"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@inglorious/eslint-config": "1.
|
|
85
|
+
"@inglorious/eslint-config": "1.2.0",
|
|
86
86
|
"@reduxjs/toolkit": "^2.12.0",
|
|
87
87
|
"prettier": "^3.9.6",
|
|
88
88
|
"typescript": "~7.0.2",
|
package/src/client/devtools.js
CHANGED
|
@@ -33,13 +33,17 @@ export function createDevtools(config = {}) {
|
|
|
33
33
|
|
|
34
34
|
middleware(store) {
|
|
35
35
|
connectDevTools(store, config)
|
|
36
|
-
|
|
36
|
+
|
|
37
|
+
const next = store.dispatch
|
|
38
|
+
store.dispatch = (event) => {
|
|
37
39
|
const result = next(event)
|
|
38
40
|
if (shouldLogEvent(event, config)) {
|
|
39
41
|
sendAction(event, store.getState())
|
|
40
42
|
}
|
|
41
43
|
return result
|
|
42
44
|
}
|
|
45
|
+
|
|
46
|
+
return store
|
|
43
47
|
},
|
|
44
48
|
}
|
|
45
49
|
}
|
|
@@ -19,7 +19,17 @@ export function multiplayerMiddleware(config = {}) {
|
|
|
19
19
|
const localQueue = []
|
|
20
20
|
|
|
21
21
|
// The middleware function that will be applied to the store.
|
|
22
|
-
return (store) =>
|
|
22
|
+
return (store) => {
|
|
23
|
+
const next = store.dispatch
|
|
24
|
+
store.dispatch = (event) => send(store, event, next)
|
|
25
|
+
return store
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Sends an event to the server unless it is filtered out or came from there, then
|
|
30
|
+
* passes it on to the next middleware, and eventually to the store.
|
|
31
|
+
*/
|
|
32
|
+
function send(store, event, next) {
|
|
23
33
|
const shouldBeSent =
|
|
24
34
|
(!blacklist.length || !blacklist.includes(event.type)) &&
|
|
25
35
|
(!whitelist.length || whitelist.includes(event.type)) &&
|
package/src/middlewares.js
CHANGED
|
@@ -1,37 +1,16 @@
|
|
|
1
|
-
import { compose } from "@inglorious/utils/functions"
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
2
|
+
* Wraps the store with each middleware. A middleware takes the store and returns it,
|
|
3
|
+
* changed or wrapped: one that only looks at events replaces `dispatch` with a function
|
|
4
|
+
* that calls the previous one, and returns its result. The first middleware wraps all
|
|
5
|
+
* the others, so events pass through them in the order they are listed.
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} store - The store to wrap.
|
|
8
|
+
* @param {Function[]} middlewares - The middlewares, each taking the store and returning it.
|
|
9
|
+
* @returns {Object} The wrapped store.
|
|
7
10
|
*/
|
|
8
|
-
export function applyMiddlewares(
|
|
9
|
-
return (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// Enhanced store interface that middlewares receive
|
|
17
|
-
const middlewareStore = {
|
|
18
|
-
...store,
|
|
19
|
-
dispatch: (...args) => dispatch(...args),
|
|
20
|
-
notify: (type, payload) => dispatch({ type, payload }),
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Create middleware chain - each middleware gets the store and can access baseApi
|
|
24
|
-
const chain = middlewares.map((middleware) => middleware(middlewareStore))
|
|
25
|
-
|
|
26
|
-
// Compose the middleware chain to create enhanced dispatch
|
|
27
|
-
dispatch = compose(...chain)(store.dispatch)
|
|
28
|
-
|
|
29
|
-
// Return enhanced API that external code will use
|
|
30
|
-
return {
|
|
31
|
-
...middlewareStore,
|
|
32
|
-
// Override dispatch/notify with the middleware-enhanced versions
|
|
33
|
-
dispatch,
|
|
34
|
-
notify: (type, payload) => dispatch({ type, payload }),
|
|
35
|
-
}
|
|
36
|
-
}
|
|
11
|
+
export function applyMiddlewares(store, middlewares) {
|
|
12
|
+
return middlewares.reduceRight(
|
|
13
|
+
(wrapped, middleware) => middleware(wrapped),
|
|
14
|
+
store,
|
|
15
|
+
)
|
|
37
16
|
}
|
package/src/store.js
CHANGED
|
@@ -42,7 +42,8 @@ const UPDATE_STRATEGIES = [STRUCTURAL_SHARING, COPY_ON_DISPATCH]
|
|
|
42
42
|
* @param {Types} [config.types] - The initial types configuration.
|
|
43
43
|
* @param {Entities} [config.entities] - The initial entities configuration.
|
|
44
44
|
* @param {Object[]} [config.systems] - The initial systems configuration.
|
|
45
|
-
* @param {Function[]} [config.middlewares] -
|
|
45
|
+
* @param {Function[]} [config.middlewares] - Functions that each take the store and return it,
|
|
46
|
+
* changed or wrapped. The first wraps all the others, so events pass through them in order.
|
|
46
47
|
* @param {boolean} [config.autoCreateEntities=false] - Creates entities if not defined in `config.entities`.
|
|
47
48
|
* @param {"auto" | "manual"} [config.updateMode="auto"] - Whether each event triggers an update, or updates are batched until `update()` is called.
|
|
48
49
|
* @param {UpdateStrategy} [config.updateStrategy="structural-sharing"] - How the state is copied before events are applied.
|
|
@@ -81,9 +82,7 @@ export function createStore({
|
|
|
81
82
|
reset,
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
const store = middlewares
|
|
85
|
-
? applyMiddlewares(...middlewares)(baseStore)
|
|
86
|
-
: baseStore
|
|
85
|
+
const store = applyMiddlewares(baseStore, middlewares)
|
|
87
86
|
const api = createApi(store, store.extras)
|
|
88
87
|
store._api = api
|
|
89
88
|
|
package/types/store.d.ts
CHANGED
|
@@ -223,7 +223,9 @@ export interface Store<
|
|
|
223
223
|
}
|
|
224
224
|
|
|
225
225
|
/**
|
|
226
|
-
* Middleware function type
|
|
226
|
+
* Middleware function type: takes the store and returns it, changed or wrapped. One that
|
|
227
|
+
* only looks at events replaces `dispatch` with a function that calls the previous one.
|
|
228
|
+
* The first middleware wraps all the others, so events pass through them in order.
|
|
227
229
|
*/
|
|
228
230
|
export type Middleware<
|
|
229
231
|
TEntity extends BaseEntity = BaseEntity,
|