From 63d34f7990f4fc277166e032a63426d3613294d9 Mon Sep 17 00:00:00 2001 From: Brandon Orther Date: Sun, 3 Dec 2017 22:20:47 -0800 Subject: [PATCH] Fix BUG performLogin called multiple times Since `enable` caused the policy function to be called for every action dispatched to the store, any action(s) dispatched after performLogin was called and before `state.cognito.state` transitioned from `LOGGING_IN` to something else caused `performLogin` to be called. To fix this bug the store subscription handler checks if `state.cognito.state` has changed and only calls the policy function if it has. --- src/policy.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/policy.js b/src/policy.js index 43e1436..cd5cffe 100644 --- a/src/policy.js +++ b/src/policy.js @@ -10,10 +10,16 @@ import { CognitoState } from './states'; * @param {function} f - f(state, dispatch) */ const enable = (store, f, params) => { + let currentCognitoState; + store.subscribe(() => { const state = store.getState(); const dispatch = store.dispatch; - f(state, dispatch, params); + + if (state.cognito.state !== currentCognitoState) { + currentCognitoState = state.cognito.state; + f(state, dispatch, params); + } }); };