Skip to content

Commit

Permalink
feat(events): Accurately type events API arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Aug 14, 2021
1 parent f97a08d commit 95be8b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/plugins/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://opensource.org/licenses/MIT.
*/

import type { State, Ctx, PlayerID, Game } from '../../types';
import type { State, Ctx, PlayerID, Game, ActivePlayersArg } from '../../types';
import { automaticGameEvent } from '../../core/action-creators';
import { GameMethod } from '../../core/game-methods';

Expand All @@ -32,14 +32,14 @@ const enum Errors {
}

export interface EventsAPI {
endGame?(...args: any[]): void;
endPhase?(...args: any[]): void;
endStage?(...args: any[]): void;
endTurn?(...args: any[]): void;
pass?(...args: any[]): void;
setActivePlayers?(...args: any[]): void;
setPhase?(...args: any[]): void;
setStage?(...args: any[]): void;
endGame(gameover?: any): void;
endPhase(): void;
endStage(): void;
endTurn(arg?: { next: PlayerID }): void;
pass(arg?: { remove: true }): void;
setActivePlayers(arg: ActivePlayersArg): void;
setPhase(newPhase: string): void;
setStage(newStage: string): void;
}

export interface PrivateEventsAPI {
Expand Down Expand Up @@ -82,9 +82,9 @@ export class Events {
}

api() {
const events: EventsAPI & PrivateEventsAPI = {
const events = {
_obj: this,
};
} as unknown as EventsAPI & PrivateEventsAPI;
for (const type of this.flow.eventNames) {
events[type] = (...args: any[]) => {
this.dispatch.push({
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/plugin-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const EventsPlugin: Plugin<EventsAPI & PrivateEventsAPI> = {
fnWrap:
(method, methodType) =>
(G, ctx, ...args) => {
const api = ctx.events as PrivateEventsAPI;
const api = ctx.events as EventsAPI & PrivateEventsAPI;
if (api) api._obj.updateTurnContext(ctx, methodType);
G = method(G, ctx, ...args);
if (api) api._obj.unsetCurrentMethod();
Expand Down

0 comments on commit 95be8b9

Please sign in to comment.