Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example with hot reloading #3

Closed
jhen0409 opened this issue Dec 21, 2015 · 3 comments
Closed

Add example with hot reloading #3

jhen0409 opened this issue Dec 21, 2015 · 3 comments

Comments

@jhen0409
Copy link

First thanks this great work! :D
I simply tried this module with the webpack hot reload, see following code, it used counter example:

Main process (with babel-core/register)

import thunk from 'redux-thunk';
import rootReducer from '../reducers';
import { electronEnhancer } from 'redux-electron-store';
import { createStore, applyMiddleware, compose } from 'redux';
import { ipcMain } from 'electron';

const finalCreateStore = compose(
  applyMiddleware(thunk),
  electronEnhancer({})
)(createStore);

export default function configureStore(initialState) {
  const store = finalCreateStore(rootReducer, initialState);

  ipcMain.on('renderer-reload', (event, action) => {
    delete require.cache[require.resolve('../reducers')];
    delete require.cache[require.resolve('../reducers/counter')];
    store.replaceReducer(require('../reducers'));
    event.returnValue = true;
  });

  return store;
}

Renderer process (with webpack)

import thunk from 'redux-thunk';
import rootReducer from '../reducers';
import DevTools from '../containers/DevTools';
import { createStore, applyMiddleware, compose } from 'redux';
import { ipcRenderer } from 'electron';
import { electronEnhancer } from 'redux-electron-store';

const finalCreateStore = compose(
  applyMiddleware(thunk),
  electronEnhancer({}),
  DevTools.instrument()
)(createStore);

export default function configureStore(initialState) {
  const store = finalCreateStore(rootReducer, initialState);

  if (module.hot) {
    module.hot.accept('../reducers', () => {
      ipcRenderer.sendSync('renderer-reload');
      store.replaceReducer(require('../reducers'))
    });
  }

  return store;
}

There a better way to delete cache of main process?

@samiskin
Copy link
Owner

I am currently working on an example and I will make sure to try this out!

@samiskin
Copy link
Owner

I finally got to trying this out, terribly sorry for taking an entire 4 months to do so. Unfortunately I didn't meet much success while using webpack for my main process files.

I had hoped that http://stackoverflow.com/a/14801711 would work out, however I had no luck. It might work when webpack isn't used for the main process (it handles dynamic requires in a special way).

For me it seemed that deleting only '../reducers' would also invalidate ../reducers/counter as long as I didn't require counter separately, so I guess your way would still work as long as reducers are only included by other reducers.

@samiskin
Copy link
Owner

Forgot to mention:
I put example code in the README, thanks for suggesting it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants