klarna / electron-redux

Use redux in the main and browser processes in electron
MIT License
743 stars 94 forks source link

Actions dispatch twice if second window open #330

Closed EdMSL closed 2 years ago

EdMSL commented 2 years ago

Hi. I'm not sure if the problem in your package. I create the app with React and Redux Saga. All worked good until I needed to add second window in my app. Now, if the second window is open and actions dispatch from saga, all this actions dispatch twice. If there are will be three windows in the app, actions will dispatch thrice, etc. The problem is only with actions from sagas. The problem has with 1.5.4 and 2.0.0 versions.

I don't know Redux well and maybe this is his normal behavior.

Here my configureStore function.

export const configureStore = (
  initialState,
  scope: string,
): { store: Store<IAppState>, history: any, } => {
  const sagaMiddleware = createSagaMiddleware();
  let middleware: Middleware[] = [];
  let history: History|undefined;

  if (scope === Scope.RENDERER) {
    history = createHashHistory();
    const routerMiddleware = createRouterMiddleware(history);

    middleware = [
      routerMiddleware,
      sagaMiddleware,
    ];
  }

  const composeEnhancers = composeWithDevToolsDevelopmentOnly({});

  const rootReducer = getRootReducer(scope, history);

  const enhancer: StoreEnhancer = composeEnhancers(composeWithStateSync(
    applyMiddleware(...middleware),
  ));

  const store = createStore(rootReducer, initialState, enhancer);

  if (scope === Scope.RENDERER) {
    sagaMiddleware.run(mainSaga);
  }

  return { store, history };
};

And then I use it in main and render processes. Render:

const initialState = remote.getGlobal('state');
const { store, history } = configureStore(initialState, Scope.RENDERER);

Main:

const initialState = /* prepare state */
global.state = initialState;

const appStore = configureStore(global.state, Scope.MAIN).store;

appStore.subscribe(() => {
  const currentState = appStore.getState();
  global.state = currentState;
  ...
});

Anyone have an idea how to fix this?

EdMSL commented 2 years ago

Ok, the problem is not in the package. This is redux behavior.