From aff582df12e770054ccfa57966c04e71192e4fc9 Mon Sep 17 00:00:00 2001 From: Andrea Cigana <24194202+ciganandrea@users.noreply.github.com> Date: Thu, 25 Feb 2021 11:11:53 +0100 Subject: [PATCH] feat: Add action name & payload to middleware args (#82) * feat: Add action name & payload to middleware args Relates to #81 * feat: Change order of middleware arguments --- src/stateMachine.tsx | 2 +- src/types.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/stateMachine.tsx b/src/stateMachine.tsx index d821ad8..6e01e60 100644 --- a/src/stateMachine.tsx +++ b/src/stateMachine.tsx @@ -52,7 +52,7 @@ function actionTemplate( if (storeFactory.middleWares.length) { storeFactory.state = storeFactory.middleWares.reduce( (currentValue, currentFunction) => - currentFunction(currentValue) || currentValue, + currentFunction(currentValue, callback.name, payload) || currentValue, storeFactory.state, ); } diff --git a/src/types.ts b/src/types.ts index 518dc6a..ec4a12b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -15,7 +15,11 @@ export type StateMachineContextValue = { setState: React.Dispatch> }; -export type MiddleWare = (arg: T) => T; +export type MiddleWare = ( + state: GlobalState, + payload: any, + callbackName: string, +) => GlobalState; export type StateMachineOptions = { name: string;