From 6a3d8c62b4ecd7aaf1090bc83421c5babab624d6 Mon Sep 17 00:00:00 2001 From: Trent Walls Date: Thu, 3 Feb 2022 23:08:27 -0500 Subject: [PATCH] Added flow options argument to onWillChooseFlow function call --- package.json | 2 +- src/FlowStarter.ts | 12 +++++++++--- src/Types.ts | 14 +++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 5b37f71..7568a18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aquaman-redux", - "version": "1.2.3", + "version": "1.3.0", "description": "Redux middlewear for composable, declarative control flow", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/FlowStarter.ts b/src/FlowStarter.ts index 9d1b70d..f176703 100644 --- a/src/FlowStarter.ts +++ b/src/FlowStarter.ts @@ -12,7 +12,7 @@ import flowController, { AQUAMAN_LOCATION_ID, } from "./DispatchController"; -import { FlowObj, MapReduxToConfig, AquamanConfig, OnWillChooseFlowReturn } from "./Types"; +import { FlowObj, MapReduxToConfig, AquamanConfig, OnWillChooseFlowReturn, FlowTriggerType } from "./Types"; import { Excluder, FlowExcluder } from "./FlowExcluder"; const defaultReduxConfig = { @@ -97,7 +97,10 @@ export class FlowStarter { const canStartFlow = flow.condition && flow.condition(...states); if (canStartFlow) { - const returnDataOrFlowObj = this.config.onWillChooseFlow(flow); + const flowOptions = { + triggerType: FlowTriggerType.Conditions + }; + const returnDataOrFlowObj = this.config.onWillChooseFlow(flow, flowOptions); const onWillChooseFlowReturn = ((): OnWillChooseFlowReturn => { if (typeof returnDataOrFlowObj == 'object' && returnDataOrFlowObj != null) { @@ -141,7 +144,10 @@ export class FlowStarter { return; } - const returnDataOrFlowObj = this.config.onWillChooseFlow(forcedFlow); + const flowOptions = { + triggerType: FlowTriggerType.ForceFlow + }; + const returnDataOrFlowObj = this.config.onWillChooseFlow(forcedFlow, flowOptions); const onWillChooseFlowReturn = ((): OnWillChooseFlowReturn => { if (typeof returnDataOrFlowObj == 'object' && returnDataOrFlowObj != null) { diff --git a/src/Types.ts b/src/Types.ts index 7eaa0f4..c869e09 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -21,12 +21,24 @@ export interface OnWillChooseFlowReturn { preventFlowStart?: boolean, } +export enum FlowTriggerType { + Conditions = 'Conditions', + ForceFlow = 'ForceFlow', +} + +export interface FlowOptions { + triggerType: FlowTriggerType; +} + +type OnWillChooseFlow = (flow: FlowObj) => OnWillChooseFlowReturn | FlowObj | false | void; +type OnWillChooseFlowWithOptions = (flow: FlowObj, flowOptions: FlowOptions) => OnWillChooseFlowReturn | FlowObj | false | void; + export interface AquamanConfig { persistSettings?: PersistSettings; onEndFlow: (flowId: string) => Promise; onStep: (flowId: string, stepCount: number) => void; shouldStartFlow: (flowId: string) => boolean | void; - onWillChooseFlow: (flow: FlowObj) => OnWillChooseFlowReturn | FlowObj | false | void; + onWillChooseFlow: OnWillChooseFlow | OnWillChooseFlowWithOptions; functionMap: { [functionName: string]: Function }; mutuallyExclusiveFlows?: string[][]; }