Skip to content

Commit

Permalink
fix(client): Correctly type state returned by client to subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Oct 20, 2020
1 parent 051c43f commit 9fcbed7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/client/client.ts
Expand Up @@ -28,6 +28,7 @@ import {
CredentialedActionShape,
FilteredMetadata,
Game,
LogEntry,
PlayerID,
Reducer,
State,
Expand Down Expand Up @@ -116,6 +117,14 @@ export interface ClientOpts<
enhancer?: StoreEnhancer;
}

export type ClientState<G extends any = any> =
| null
| (State<G> & {
isActive: boolean;
isConnected: boolean;
log: LogEntry[];
});

/**
* Implementation of Client (see below).
*/
Expand Down Expand Up @@ -359,7 +368,7 @@ export class _ClientImpl<G extends any = any> {
this.manager.unregister(this);
}

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

getState() {
getState(): ClientState<G> {
let state = this.store.getState();

if (this.gameStateOverride !== null) {
Expand Down
8 changes: 6 additions & 2 deletions src/client/react.tsx
Expand Up @@ -8,7 +8,12 @@

import React from 'react';
import PropTypes from 'prop-types';
import { Client as RawClient, ClientOpts, _ClientImpl } from './client';
import {
Client as RawClient,
ClientOpts,
ClientState,
_ClientImpl,
} from './client';

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

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

type ClientState<G extends any = any> = ReturnType<_ClientImpl<G>['getState']>;
type ExposedClientProps<G extends any = any> = Pick<
_ClientImpl<G>,
| 'log'
Expand Down

0 comments on commit 9fcbed7

Please sign in to comment.