Skip to content

Commit 9fcbed7

Browse files
committed
fix(client): Correctly type state returned by client to subscribers
1 parent 051c43f commit 9fcbed7

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/client/client.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
CredentialedActionShape,
2929
FilteredMetadata,
3030
Game,
31+
LogEntry,
3132
PlayerID,
3233
Reducer,
3334
State,
@@ -116,6 +117,14 @@ export interface ClientOpts<
116117
enhancer?: StoreEnhancer;
117118
}
118119

120+
export type ClientState<G extends any = any> =
121+
| null
122+
| (State<G> & {
123+
isActive: boolean;
124+
isConnected: boolean;
125+
log: LogEntry[];
126+
});
127+
119128
/**
120129
* Implementation of Client (see below).
121130
*/
@@ -359,7 +368,7 @@ export class _ClientImpl<G extends any = any> {
359368
this.manager.unregister(this);
360369
}
361370

362-
subscribe(fn: (state: State<G>) => void) {
371+
subscribe(fn: (state: ClientState<G>) => void) {
363372
const id = Object.keys(this.subscribers).length;
364373
this.subscribers[id] = fn;
365374
this.transport.subscribe(() => this.notifySubscribers());
@@ -378,7 +387,7 @@ export class _ClientImpl<G extends any = any> {
378387
return this.initialState;
379388
}
380389

381-
getState() {
390+
getState(): ClientState<G> {
382391
let state = this.store.getState();
383392

384393
if (this.gameStateOverride !== null) {

src/client/react.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
import React from 'react';
1010
import PropTypes from 'prop-types';
11-
import { Client as RawClient, ClientOpts, _ClientImpl } from './client';
11+
import {
12+
Client as RawClient,
13+
ClientOpts,
14+
ClientState,
15+
_ClientImpl,
16+
} from './client';
1217

1318
type WrappedBoardDelegates = 'matchID' | 'playerID' | 'credentials';
1419

@@ -17,7 +22,6 @@ export type WrappedBoardProps = Pick<
1722
WrappedBoardDelegates | 'debug'
1823
>;
1924

20-
type ClientState<G extends any = any> = ReturnType<_ClientImpl<G>['getState']>;
2125
type ExposedClientProps<G extends any = any> = Pick<
2226
_ClientImpl<G>,
2327
| 'log'

0 commit comments

Comments
 (0)