@couch-kit/host 0.5.0 → 0.5.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 +1 -1
- package/src/provider.tsx +9 -3
package/package.json
CHANGED
package/src/provider.tsx
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
InternalActionTypes,
|
|
15
15
|
DEFAULT_HTTP_PORT,
|
|
16
16
|
DEFAULT_WS_PORT_OFFSET,
|
|
17
|
+
createGameReducer,
|
|
17
18
|
type IGameState,
|
|
18
19
|
type IAction,
|
|
19
20
|
type InternalAction,
|
|
@@ -22,7 +23,7 @@ import {
|
|
|
22
23
|
|
|
23
24
|
export interface GameHostConfig<S extends IGameState, A extends IAction> {
|
|
24
25
|
initialState: S;
|
|
25
|
-
reducer: (state: S, action: A
|
|
26
|
+
reducer: (state: S, action: A) => S;
|
|
26
27
|
port?: number; // Static server port (default 8080)
|
|
27
28
|
wsPort?: number; // WebSocket port (default: HTTP port + 2, i.e. 8082)
|
|
28
29
|
devMode?: boolean;
|
|
@@ -39,7 +40,7 @@ export interface GameHostConfig<S extends IGameState, A extends IAction> {
|
|
|
39
40
|
|
|
40
41
|
interface GameHostContextValue<S extends IGameState, A extends IAction> {
|
|
41
42
|
state: S;
|
|
42
|
-
dispatch: (action: A
|
|
43
|
+
dispatch: (action: A) => void;
|
|
43
44
|
serverUrl: string | null;
|
|
44
45
|
serverError: Error | null;
|
|
45
46
|
}
|
|
@@ -93,7 +94,12 @@ export function GameHostProvider<S extends IGameState, A extends IAction>({
|
|
|
93
94
|
children: React.ReactNode;
|
|
94
95
|
config: GameHostConfig<S, A>;
|
|
95
96
|
}) {
|
|
96
|
-
|
|
97
|
+
// Wrap the user's reducer with createGameReducer to handle internal actions
|
|
98
|
+
// (HYDRATE, PLAYER_JOINED, PLAYER_LEFT) automatically.
|
|
99
|
+
const [state, dispatch] = useReducer(
|
|
100
|
+
createGameReducer(config.reducer),
|
|
101
|
+
config.initialState,
|
|
102
|
+
);
|
|
97
103
|
|
|
98
104
|
// Keep a ref to state so we can access it inside callbacks/effects that don't depend on it
|
|
99
105
|
const stateRef = useRef(state);
|