electron-redux icon indicating copy to clipboard operation
electron-redux copied to clipboard

createAliasedAction in main and renderer, registry is empty in main

Open eric-burel opened this issue 6 years ago • 6 comments

Hi,

I have some trouble using createAliasedAction. Here is what I understood about the workflow from the doc and timesheets:

  1. We define an aliased action creator as usual, in the shared folder
  2. We dispatch the action. Since only the main process has the triggerAlias, it will handle the action triggering.
  3. My action is triggered in the main process, amazing, I can even use redux-sage.

That's the theory and it's an excellent pattern. However in practice, since the action creator is called in the renderer, the alias registry is set in the renderer. It does not seem to be set correctly in the main process too. Reimporting the registry in the main process will fail, my alias is not defined, so triggerAlias fails too and does not reckognize my action.

I suspect that somewhere in the timesheets app main process there is an import that loads the action, though I can't find such import. This way your registry is correctly defined, and your app works (at least my app work when I load my aliased action in the main).

However, this is not a desirable behaviour, since most of the time action creators are renderer process stuffs, one can't expect them to be imported in the main too.

A solution would be to use a global, createAliasedAction would set a remote variable when imported from the renderer and a global when imported from the main process.

eric-burel avatar Aug 28 '17 12:08 eric-burel

You're right, that shouldn't even work (and I'm slightly puzzled why it does nevertheless). Saving to a global store should be relatively easy, even given the current implementation.

PRs welcome 😄 otherwise, I'll take a look tonight

hardchor avatar Aug 28 '17 12:08 hardchor

Thanks. I think I might actually use another pattern and split reducers and sagas. Indeed, when using redux-sagas, there are no async actions, you simply add listeners on normal actions. So the easiest way in this case is not to create an aliased action, but to simply remove the relevant sagas from the renderer store and put them in the main store. This way they are only read once.

I will lack time to PR a fix for the registry bug (all the more that I have no fix yet ^^), but I might write something on redux-saga and aliasedAction, since the doc is a bit terse right now.

eric-burel avatar Aug 28 '17 12:08 eric-burel

@eric-burel Sorry, just catching up on emails! As mentioned in the other issue, that's what aliased actions are meant to be for - whenever you need to trigger an action exactly once (on the main thread), e.g. data fetching or sagas, as in your case.

hardchor avatar Sep 04 '17 13:09 hardchor

Hi, no problem, there is no hurry. Yep indeed I noticed that I will need them eventually. Placing the sagas only in the main solves the problems in terms of behaviour, which at least provides a working solution, but indeed I now have "ghost actions" in the renderer, that I should alias instead.

I may rework on this project in a few weeks, I'll tell you if I find a solution.

eric-burel avatar Sep 07 '17 09:09 eric-burel

Let me know how you get on! If this becomes a serious hinderance, we should probably look into how we can address this library-side.

ghost avatar Sep 07 '17 09:09 ghost

I wanted to add I started using this library and hit this bug as well. If you come up with a solution I'm happy to test it out.

dewyze avatar Jun 30 '19 12:06 dewyze