Skip to content

Commit

Permalink
keep store on main process, forward actions via IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
hardchor committed May 12, 2016
1 parent 32bf402 commit 9d2c208
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 95 deletions.
40 changes: 11 additions & 29 deletions app/main/index.development.js → app/main/createMainWindow.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { app, BrowserWindow, Menu, crashReporter, shell } from 'electron';
import { app, BrowserWindow, Menu } from 'electron';

let menu;
let template;
let mainWindow = null;

if (process.env.NODE_ENV === 'development') {
require('electron-debug')();
}
export default function createWindow(store) {
let menu;
let template;

function createWindow() {
mainWindow = new BrowserWindow({
const mainWindow = new BrowserWindow({
show: false,
width: 1024,
height: 728
});

// set main redux store
mainWindow.initialState = store.getState();

mainWindow.maximize();

mainWindow.loadURL(`file://${__dirname}/../renderer/app.html`);
Expand All @@ -23,10 +23,6 @@ function createWindow() {
mainWindow.focus();
});

mainWindow.on('closed', () => {
mainWindow = null;
});

if (process.env.NODE_ENV === 'development') {
mainWindow.openDevTools();
}
Expand Down Expand Up @@ -230,20 +226,6 @@ function createWindow() {
menu = Menu.buildFromTemplate(template);
mainWindow.setMenu(menu);
}
}


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) {
createWindow();
}
});


app.on('ready', createWindow);
return mainWindow;
}
36 changes: 36 additions & 0 deletions app/main/index.js
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 removed app/renderer/api/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion app/renderer/containers/JobsPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import Jobs from '../components/Jobs';
import * as JobActions from '../actions/job';
import * as JobActions from '../../shared/actions/job';

function mapStateToProps(state) {
return {
Expand Down
11 changes: 9 additions & 2 deletions app/renderer/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { ipcRenderer, remote } from 'electron';
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { Router, hashHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import routes from './routes';
import configureStore from './store/configureStore';
import configureStore from '../shared/store/configureStore';
import './app.global.css';

const store = configureStore();
const { initialState } = remote.getCurrentWindow();

const store = configureStore(initialState, 'renderer');
const history = syncHistoryWithStore(hashHistory, store);

ipcRenderer.on('redux-action', (event, payload) => {
store.dispatch(payload);
});

render(
<Provider store={store}>
<Router history={history} routes={routes} />
Expand Down
10 changes: 0 additions & 10 deletions app/renderer/reducers/index.js

This file was deleted.

37 changes: 0 additions & 37 deletions app/renderer/store/configureStore.development.js

This file was deleted.

13 changes: 0 additions & 13 deletions app/renderer/store/configureStore.production.js

This file was deleted.

Empty file removed app/renderer/utils/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions app/renderer/actions/job.js → app/shared/actions/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export function startJob(time = new Date()) {
payload: {
time,
},
meta: {
scope: 'main',
},
};
}

Expand All @@ -19,6 +22,9 @@ export function pauseJob(id, time = new Date()) {
id,
time,
},
meta: {
scope: 'main',
},
};
}

Expand All @@ -29,5 +35,8 @@ export function stopJob(id, time = new Date()) {
id,
time,
},
meta: {
scope: 'main',
},
};
}
17 changes: 17 additions & 0 deletions app/shared/reducers/index.js
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.
65 changes: 65 additions & 0 deletions app/shared/store/configureStore.development.js
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.
16 changes: 16 additions & 0 deletions app/shared/store/configureStore.production.js
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);
// }
14 changes: 14 additions & 0 deletions app/shared/store/middleware/forwardToMain.js
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;
20 changes: 20 additions & 0 deletions app/shared/store/middleware/forwardToRenderer.js
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;
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const DEFAULT_OPTS = {
'^/test($|/)',
'^/tools($|/)',
'^/release($|/)',
'^/app/main/index.development.js'
'^/app/main/index.js'
].concat(devDeps.map(name => `/node_modules/${name}($|/)`))
.concat(
deps.filter(name => !electronCfg.externals.includes(name))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build-renderer": "cross-env NODE_ENV=production node -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.production.js --progress --profile --colors",
"build": "npm run build-main && npm run build-renderer",
"start": "cross-env NODE_ENV=production electron ./",
"start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register ./app/main/index.development",
"start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register ./app/main/index",
"package": "cross-env NODE_ENV=production node -r babel-register package.js",
"package-all": "npm run package -- --all",
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {

devtool: 'source-map',

entry: './app/main/index.development',
entry: './app/main/index',

output: {
path: __dirname,
Expand Down

0 comments on commit 9d2c208

Please sign in to comment.