@couch-kit/host 0.5.1 → 0.5.2

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 CHANGED
@@ -50,14 +50,20 @@ Returns:
50
50
  - `serverUrl`: HTTP URL phones should open (or `devServerUrl` in dev mode)
51
51
  - `serverError`: static server error (if startup fails)
52
52
 
53
- ## System Actions (Important)
53
+ ## System Actions
54
54
 
55
- The host will dispatch a few **system action types** into your reducer. Treat these as reserved:
55
+ The host automatically dispatches internal system actions (`__PLAYER_JOINED__`, `__PLAYER_LEFT__`, `__HYDRATE__`) into `createGameReducer`, which handles them for you. **You do not need to handle these in your reducer.**
56
56
 
57
- - `PLAYER_JOINED`: payload `{ id: string, name: string, avatar?: string, secret?: string }`
58
- - `PLAYER_LEFT`: payload `{ playerId: string }`
57
+ Player tracking (`state.players`) is managed automatically:
59
58
 
60
- If you want to track players in `state.players`, handle these action types in your reducer. The `secret` field can be used to identify returning players.
59
+ - When a player joins, they are added to `state.players` with `connected: true`.
60
+ - When a player disconnects, they are marked as `connected: false`.
61
+
62
+ To react to player events outside of state (e.g., logging, analytics), use the callback config options:
63
+
64
+ - `onPlayerJoined?: (playerId: string, name: string) => void` -- called when a player successfully joins.
65
+ - `onPlayerLeft?: (playerId: string) => void` -- called when a player disconnects.
66
+ - `onError?: (error: Error) => void` -- called when a server error occurs.
61
67
 
62
68
  ### 1. Configure the Provider
63
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@couch-kit/host",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "React Native host for local multiplayer party games on Android TV — WebSocket server, state management, and static file serving",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -51,7 +51,7 @@
51
51
  "clean": "del-cli lib"
52
52
  },
53
53
  "dependencies": {
54
- "@couch-kit/core": "0.3.1",
54
+ "@couch-kit/core": "0.3.2",
55
55
  "buffer": "^6.0.3",
56
56
  "js-sha1": "^0.7.0",
57
57
  "react-native-fs": "^2.20.0",
@@ -60,8 +60,8 @@
60
60
  "react-native-tcp-socket": "^6.0.6"
61
61
  },
62
62
  "devDependencies": {
63
- "@types/react": "~17.0.21",
64
- "@types/react-native": "0.70.0",
63
+ "@types/react": "^18.2.0",
64
+ "@types/react-native": "^0.72.0",
65
65
  "react": "18.2.0",
66
66
  "react-native": "0.72.6",
67
67
  "del-cli": "^5.1.0",
package/src/provider.tsx CHANGED
@@ -87,6 +87,22 @@ function isValidClientMessage(msg: unknown): msg is ClientMessage {
87
87
  }
88
88
  }
89
89
 
90
+ /**
91
+ * React context provider that turns a React Native TV app into a local game server.
92
+ *
93
+ * Starts a static file server (for the web controller) and a WebSocket game server
94
+ * (for real-time state sync). Manages the canonical game state using the provided
95
+ * reducer and broadcasts state updates to all connected clients.
96
+ *
97
+ * @param config - Host configuration including reducer, initial state, ports, and callbacks.
98
+ *
99
+ * @example
100
+ * ```tsx
101
+ * <GameHostProvider config={{ reducer: gameReducer, initialState }}>
102
+ * <GameScreen />
103
+ * </GameHostProvider>
104
+ * ```
105
+ */
90
106
  export function GameHostProvider<S extends IGameState, A extends IAction>({
91
107
  children,
92
108
  config,
@@ -318,6 +334,16 @@ export function GameHostProvider<S extends IGameState, A extends IAction>({
318
334
  );
319
335
  }
320
336
 
337
+ /**
338
+ * React hook to access the game host context.
339
+ *
340
+ * Must be used within a `<GameHostProvider>`. Returns the canonical game state,
341
+ * a dispatch function for actions, the server URL (for QR codes), and any
342
+ * server startup errors.
343
+ *
344
+ * @returns An object with `state`, `dispatch`, `serverUrl`, and `serverError`.
345
+ * @throws If used outside of a `<GameHostProvider>`.
346
+ */
321
347
  export function useGameHost<S extends IGameState, A extends IAction>() {
322
348
  const context = useContext(GameHostContext);
323
349
  if (!context) {
package/src/server.ts CHANGED
@@ -11,6 +11,16 @@ export interface CouchKitHostConfig {
11
11
  staticDir?: string; // Override the default www directory path (required on Android)
12
12
  }
13
13
 
14
+ /**
15
+ * React hook that manages a static HTTP file server for serving the web controller.
16
+ *
17
+ * In production mode, starts a `StaticServer` bound to `0.0.0.0` on the configured port,
18
+ * serving files from `staticDir` (or `${RNFS.MainBundlePath}/www` by default).
19
+ * In dev mode, skips the server and returns `devServerUrl` directly.
20
+ *
21
+ * @param config - Server configuration including port, dev mode, and static directory.
22
+ * @returns An object with `url` (the server URL or null), `error`, and `loading`.
23
+ */
14
24
  export const useStaticServer = (config: CouchKitHostConfig) => {
15
25
  const [url, setUrl] = useState<string | null>(null);
16
26
  const [error, setError] = useState<Error | null>(null);