samiskin / redux-electron-store

⎋ A redux store enhancer that allows automatic synchronization between electron processes
MIT License
375 stars 32 forks source link

Lil confused by the docs #37

Closed hhff closed 7 years ago

hhff commented 7 years ago

Hi @samiskin !

Question about the docs:

When explaining how to use the electron enhancer:

import { createStore, applyMiddleware, compose } from 'redux';
import { electronEnhancer } from 'redux-electron-store';

let enhancer = compose(
  applyMiddleware(...middleware),
  // Must be placed after any enhancers which dispatch
  // their own actions such as redux-thunk or redux-saga
  electronEnhancer({
    // Allows synched actions to pass through all enhancers
    dispatchProxy: a => store.dispatch(a),
  })
);

// Note: passing enhancer as the last argument to createStore requires redux@>=3.1.0
let store = createStore(reducer, initialState, enhancer);

The store.dispatch(a) call is defined before the store has been instantiated, and thus you get store is undefined when the app boots.

Not sure how I'm supposed to declare the dispatch call without the store instantiated earlier in the file?

TY for all your hard work!

samiskin commented 7 years ago

It should work (and works on my machine), as even though store is being declared afterwards, whats being passed in to electronEnhancer is a callback, so the store.dispatch line doesn't actually get ran until dispatchProxy gets called, which happens once the store exists.

Its unfortunate that it has to be this way, but I'm not sure if there's a nicer way to do this using store enhancers. I essentially need the dispatch function of the final store, but the electronEnhancer is used while creating the store 😞

Does it not work for you when you run it? A similar setup without having this usage before definition would be

let storeDispatch;
let enhancer = compose(
  ...
  electronEnhancer({ dispatchProxy: a => storeDispatch(a) });
});
let store = createStore(reducer, initialState, enhancer);
storeDispatch = store.dispatch;

Maybe that would work instead?

adamdicarlo commented 7 years ago

@hhff are you transpiling your code at all? if so, which transpiler & version? (might be good to paste your code here too) The electronEnhancer function (from what I can tell) does not call dispatchProxy immediately.

I don't see anything wrong with the README example, so this is a pretty interesting issue!

hhff commented 7 years ago

Hi @adamdicarlo I switched to a different electron redux lib (https://github.com/hardchor/electron-redux) because I couldn't get this up and running. It works great!

The example in the README could not possibly work in any dialect of JS - at the time the enhancer is declared, store is not defined.

@samiskin's example above should work, as the variable is allocated for the call before the declaration. I was messing around with a few different options, and couldn't get this working immediately due to that issue, so I switched before I arrived at Sam's solution. Thought I should flag it here.