-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
keep store on main process, forward actions via IPC
- Loading branch information
Showing
20 changed files
with
201 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { app, ipcMain } from 'electron'; | ||
import createMainWindow from './createMainWindow'; | ||
import configureStore from '../shared/store/configureStore'; | ||
|
||
const store = configureStore(undefined, 'main'); | ||
let mainWindow = null; | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
require('electron-debug')(); | ||
} | ||
|
||
function doCreateMainWindow() { | ||
mainWindow = createMainWindow(store); | ||
mainWindow.on('closed', () => { | ||
mainWindow = null; | ||
}); | ||
} | ||
|
||
|
||
ipcMain.on('redux-action', (event, payload) => { | ||
store.dispatch(payload); | ||
}); | ||
|
||
app.on('window-all-closed', () => { | ||
if (process.platform !== 'darwin') app.quit(); | ||
}); | ||
|
||
app.on('activate', () => { | ||
// On OS X it's common to re-create a window in the app when the | ||
// dock icon is clicked and there are no other windows open. | ||
if (mainWindow === null) { | ||
doCreateMainWindow(); | ||
} | ||
}); | ||
|
||
app.on('ready', doCreateMainWindow); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { combineReducers } from 'redux'; | ||
import { routerReducer as routing } from 'react-router-redux'; | ||
import job from './job'; | ||
|
||
export default function getRootReducer(scope = 'main') { | ||
let reducers = { | ||
job, | ||
}; | ||
|
||
if (scope === 'renderer') { | ||
reducers = { | ||
...reducers, | ||
routing, | ||
}; | ||
} | ||
return combineReducers({ ...reducers }); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { createStore, applyMiddleware, compose } from 'redux'; | ||
import { persistState } from 'redux-devtools'; | ||
import thunk from 'redux-thunk'; | ||
import createLogger from 'redux-logger'; | ||
import { hashHistory } from 'react-router'; | ||
import { routerMiddleware } from 'react-router-redux'; | ||
import getRootReducer from '../reducers'; | ||
import forwardToMain from './middleware/forwardToMain'; | ||
import forwardToRenderer from './middleware/forwardToRenderer'; | ||
import DevTools from '../../renderer/containers/DevTools'; | ||
|
||
export default function configureStore(initialState, scope = 'main') { | ||
const logger = createLogger({ | ||
level: 'info', | ||
collapsed: true, | ||
}); | ||
const router = routerMiddleware(hashHistory); | ||
|
||
let middleware = [ | ||
thunk, | ||
logger, | ||
]; | ||
|
||
if (scope === 'renderer') { | ||
middleware = [ | ||
forwardToMain, | ||
router, | ||
...middleware, | ||
]; | ||
} | ||
if (scope === 'main') { | ||
middleware = [ | ||
...middleware, | ||
forwardToRenderer, | ||
]; | ||
} | ||
|
||
let enhanced = [ | ||
applyMiddleware(...middleware), | ||
]; | ||
|
||
if (scope === 'renderer') { | ||
enhanced = [ | ||
...enhanced, | ||
DevTools.instrument(), | ||
persistState( | ||
window.location.href.match( | ||
/[?&]debug_session=([^&]+)\b/ | ||
) | ||
), | ||
]; | ||
} | ||
|
||
const rootReducer = getRootReducer(scope); | ||
const enhancer = compose(...enhanced); | ||
const store = createStore(rootReducer, initialState, enhancer); | ||
|
||
if (module.hot) { | ||
module.hot.accept('../reducers', () => { | ||
store.replaceReducer(require('../reducers')); | ||
}); | ||
} | ||
|
||
return store; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// see dev version!!! | ||
|
||
|
||
// import { createStore, applyMiddleware } from 'redux'; | ||
// import thunk from 'redux-thunk'; | ||
// import { hashHistory } from 'react-router'; | ||
// import { routerMiddleware } from 'react-router-redux'; | ||
// import rootReducer from '../reducers'; | ||
// | ||
// const router = routerMiddleware(hashHistory); | ||
// | ||
// const enhancer = applyMiddleware(thunk, router); | ||
// | ||
// export default function configureStore(initialState) { | ||
// return createStore(rootReducer, initialState, enhancer); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ipcRenderer } from 'electron'; | ||
|
||
const forwardToMain = store => next => action => { | ||
if (action.meta && action.meta.scope && action.meta.scope === 'main') { | ||
ipcRenderer.send('redux-action', action); | ||
|
||
// stop action in-flight | ||
return; | ||
} | ||
|
||
return next(action); | ||
}; | ||
|
||
export default forwardToMain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ipcMain, BrowserWindow } from 'electron'; | ||
|
||
const forwardToRenderer = store => next => action => { | ||
// remove scope to avoid endless-loop | ||
const rendererAction = Object.assign({}, action, { | ||
meta: { | ||
scope: 'renderer' | ||
}, | ||
}); | ||
|
||
const openWindows = BrowserWindow.getAllWindows(); | ||
openWindows.forEach(({ webContents }) => { | ||
webContents.send('redux-action', rendererAction); | ||
}); | ||
|
||
// ipcMain.send('redux-action', rendererAction); | ||
return next(action); | ||
}; | ||
|
||
export default forwardToRenderer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters