From 8bf7818fa55aa0de1183aad7fdf95b52568693d8 Mon Sep 17 00:00:00 2001 From: April Arcus Date: Sun, 17 Apr 2022 22:48:51 -0700 Subject: [PATCH] unsubscribe from Redux when AngularJS app is destroyed --- src/components/ngRedux.js | 2 +- src/components/storeWrapper.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/ngRedux.js b/src/components/ngRedux.js index 1b17dde..e34b2d1 100644 --- a/src/components/ngRedux.js +++ b/src/components/ngRedux.js @@ -107,7 +107,7 @@ export default function ngReduxProvider() { const mergedStore = assign({}, store, { connect: Connector(store) }); - if (_providedStore) wrapStore(_providedStore, mergedStore); + if (_providedStore) wrapStore(_providedStore, mergedStore, injector.get('$rootScope')); return mergedStore; }; diff --git a/src/components/storeWrapper.js b/src/components/storeWrapper.js index ae03797..e87e251 100644 --- a/src/components/storeWrapper.js +++ b/src/components/storeWrapper.js @@ -1,5 +1,5 @@ -export default function wrapStore(providedStore, ngReduxStore) { - providedStore.subscribe(() => { +export default function wrapStore(providedStore, ngReduxStore, $rootScope) { + const unsubscribe = providedStore.subscribe(() => { let newState = providedStore.getState(); ngReduxStore.dispatch({ type: '@@NGREDUX_PASSTHROUGH', @@ -7,4 +7,5 @@ export default function wrapStore(providedStore, ngReduxStore) { }); }); providedStore.dispatch({ type: '@@NGREDUX_PASSTHROUGH_INIT' }) + $rootScope.$on('$destroy', unsubscribe) }