From 750994faba93715ddb7b02a29f2f6557eaf9dc5b Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Tue, 3 Aug 2021 09:51:41 -0400 Subject: [PATCH 1/3] feat(phases): Adds support for next phase fn --- src/core/flow.test.ts | 15 +++++++++++++++ src/core/flow.ts | 9 ++++++++- src/types.ts | 3 ++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/core/flow.test.ts b/src/core/flow.test.ts index 8678b630a..8b0e20f20 100644 --- a/src/core/flow.test.ts +++ b/src/core/flow.test.ts @@ -665,6 +665,21 @@ test('init', () => { expect(state.G).toMatchObject({ done: true }); }); +test('next', () => { + const flow = Flow({ + phases: { + A: { start: true, next: () => 'C' }, + B: {}, + C: {}, + }, + }); + + let state = { ctx: flow.ctx(3) } as State; + state = flow.processEvent(state, gameEvent('endPhase')); + + expect(state.ctx.phase).toEqual('C'); +}); + describe('endIf', () => { test('basic', () => { const flow = Flow({ endIf: (G) => G.win }); diff --git a/src/core/flow.ts b/src/core/flow.ts index d0cb65177..21f6e9ac3 100644 --- a/src/core/flow.ts +++ b/src/core/flow.ts @@ -163,6 +163,10 @@ export function Flow({ onEnd: HookWrapper(phaseConfig.turn.onEnd), endIf: TriggerWrapper(phaseConfig.turn.endIf), }; + + if (typeof phaseConfig.next === 'function') { + phaseConfig.wrapped.next = HookWrapper(phaseConfig.next); + } } function GetPhase(ctx: { phase: string }): PhaseConfig { @@ -315,7 +319,10 @@ export function Flow({ return state; } } else if (phaseConfig.next !== undefined) { - ctx = { ...ctx, phase: phaseConfig.next }; + ctx = + typeof phaseConfig.next === 'function' + ? { ...ctx, phase: phaseConfig.wrapped.next(state) } + : { ...ctx, phase: phaseConfig.next }; } else { ctx = { ...ctx, phase: null }; } diff --git a/src/types.ts b/src/types.ts index cc0a15459..3e297984d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -205,7 +205,7 @@ export interface PhaseConfig< CtxWithPlugins extends Ctx = Ctx > { start?: boolean; - next?: string; + next?: ((G: G, ctx: CtxWithPlugins) => any) | string; onBegin?: (G: G, ctx: CtxWithPlugins) => any; onEnd?: (G: G, ctx: CtxWithPlugins) => any; endIf?: (G: G, ctx: CtxWithPlugins) => boolean | void | { next: string }; @@ -217,6 +217,7 @@ export interface PhaseConfig< ) => boolean | void | { next: string }; onBegin?: (state: State) => any; onEnd?: (state: State) => any; + next?: (state: State) => any; }; } From 83bf3b0494f1a63f05c08615745443c75698dbcf Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Tue, 3 Aug 2021 13:34:30 -0400 Subject: [PATCH 2/3] Changed next is now always wrapped --- src/core/flow.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/flow.ts b/src/core/flow.ts index 21f6e9ac3..a08df4f13 100644 --- a/src/core/flow.ts +++ b/src/core/flow.ts @@ -164,9 +164,11 @@ export function Flow({ endIf: TriggerWrapper(phaseConfig.turn.endIf), }; - if (typeof phaseConfig.next === 'function') { - phaseConfig.wrapped.next = HookWrapper(phaseConfig.next); + if (typeof phaseConfig.next !== 'function') { + const { next } = phaseConfig; + phaseConfig.next = () => next || null; } + phaseConfig.wrapped.next = HookWrapper(phaseConfig.next); } function GetPhase(ctx: { phase: string }): PhaseConfig { @@ -319,10 +321,7 @@ export function Flow({ return state; } } else if (phaseConfig.next !== undefined) { - ctx = - typeof phaseConfig.next === 'function' - ? { ...ctx, phase: phaseConfig.wrapped.next(state) } - : { ...ctx, phase: phaseConfig.next }; + ctx = { ...ctx, phase: phaseConfig.wrapped.next(state) || null }; } else { ctx = { ...ctx, phase: null }; } From 37bbd90f1dfc007b7c22b330652b4ae6d7158a70 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Tue, 3 Aug 2021 13:34:46 -0400 Subject: [PATCH 3/3] Fixed next types --- src/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.ts b/src/types.ts index 3e297984d..6c50510c9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -205,7 +205,7 @@ export interface PhaseConfig< CtxWithPlugins extends Ctx = Ctx > { start?: boolean; - next?: ((G: G, ctx: CtxWithPlugins) => any) | string; + next?: ((G: G, ctx: CtxWithPlugins) => string | void) | string; onBegin?: (G: G, ctx: CtxWithPlugins) => any; onEnd?: (G: G, ctx: CtxWithPlugins) => any; endIf?: (G: G, ctx: CtxWithPlugins) => boolean | void | { next: string }; @@ -217,7 +217,7 @@ export interface PhaseConfig< ) => boolean | void | { next: string }; onBegin?: (state: State) => any; onEnd?: (state: State) => any; - next?: (state: State) => any; + next?: (state: State) => string | void; }; }