Skip to content

Commit

Permalink
Overwrite more listener methods of replicants
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoishin committed Jun 8, 2019
1 parent 03d83d9 commit f4f957d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
33 changes: 26 additions & 7 deletions helper/replicant.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class ReplicantCommon<
value?: unknown,
options?: {throwOnInvalid?: boolean},
): value is TSchema;
once(event: 'change', listener: (value: TSchema) => void): this;
value?: TSchema;
}

Expand All @@ -42,6 +41,15 @@ export class ReplicantServer<
event: 'change',
listener: (newValue: TSchema, oldValue?: TSchema) => void,
): this;
once(
event: 'change',
listener: (newValue: TSchema, oldValue?: TSchema) => void,
): this;
removeListener(
event: 'change',
listener: (newValue: TSchema, oldValue?: TSchema) => void,
): this;
removeAllListeners(event: 'change'): this;
}

export class ReplicantBrowser<
Expand All @@ -56,14 +64,25 @@ export class ReplicantBrowser<
socket: SocketIOClient.Socket,
);
status: 'undeclared' | 'declared' | 'declaring';
on(
event: 'declared' | 'fullUpdate',
listener: (data: TSchema) => void,
on<TEvent extends 'change' | 'declared' | 'fullUpdate'>(
event: TEvent,
listener: TEvent extends 'change'
? ((newValue: TSchema, oldValue?: TSchema) => void)
: ((data: TSchema) => void),
): this;
on(
event: 'change',
listener: (newValue: TSchema, oldValue?: TSchema) => void,
once<TEvent extends 'change' | 'declared' | 'fullUpdate'>(
event: TEvent,
listener: TEvent extends 'change'
? ((newValue: TSchema, oldValue?: TSchema) => void)
: ((data: TSchema) => void),
): this;
removeListener<TEvent extends 'change' | 'declared' | 'fullUpdate'>(
event: TEvent,
listener: TEvent extends 'change'
? ((newValue: TSchema, oldValue?: TSchema) => void)
: ((data: TSchema) => void),
): this;
removeAllListeners(event: 'change' | 'declared' | 'fullUpdate'): this;
}

export type Replicant<
Expand Down
25 changes: 20 additions & 5 deletions test-d/replicant.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {expectError} from 'tsd';
import {expectError, expectType} from 'tsd';
import {CreateNodecgInstance} from '../browser';

type OtherBundleRepMap = {
player: {playerId: string; country: string};
};
type ThisBundleRepMap = {
trashcan: {};
game: {gameId: string; players: [string, string]};
};

Expand All @@ -19,10 +20,24 @@ type ThisBundle = CreateNodecgInstance<{}, 'this-bundle', ThisBundleRepMap, {}>;

declare const nodecg: OtherBundle & ThisBundle;

nodecg.Replicant('game');
nodecg.Replicant('game', {});
nodecg.Replicant('game', 'this-bundle');
nodecg.Replicant('game', 'this-bundle', {});
const gameRep = nodecg.Replicant('game');
expectType<typeof gameRep>(nodecg.Replicant('game', {}));
expectType<typeof gameRep>(nodecg.Replicant('game', 'this-bundle'));
expectType<typeof gameRep>(nodecg.Replicant('game', 'this-bundle', {}));
expectType<undefined | {}>(gameRep.value);
gameRep.on('change', (newVal) => {
expectType<{gameId: string; players: [string, string]}>(newVal);
});
expectError(gameRep.on('changee', () => {}));
gameRep.once('change', (newVal) => {
expectType<{gameId: string; players: [string, string]}>(newVal);
});
expectError(gameRep.once('cchange', () => {}));
gameRep.removeListener('change', () => {});
expectError(gameRep.removeListener('changeee', () => {}));
gameRep.removeAllListeners('change');
expectError(gameRep.removeAllListeners('chamge'));

expectError(nodecg.Replicant('game', 'other-bundle'));
expectError(nodecg.Replicant('game', 'other-bundle', {}));

Expand Down

0 comments on commit f4f957d

Please sign in to comment.