Skip to content

Commit

Permalink
feat: add scoped ipc events
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximKalinin committed May 27, 2021
1 parent 2ebd343 commit 5795a5a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "electron-redux",
"version": "1.5.1",
"version": "2.0.0",
"description": "Using redux in electron",
"main": "index.js",
"repository": "https://github.com/hardchor/electron-redux",
Expand Down
16 changes: 9 additions & 7 deletions packages/electron-redux/src/helpers/getInitialStateRenderer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { remote } from 'electron';

export default function getInitialStateRenderer() {
const getReduxState = remote.getGlobal('getReduxState');
if (!getReduxState) {
throw new Error(
'Could not find reduxState global in main process, did you forget to call replayActionMain?',
);
export default function getInitialStateRenderer(ipc_event_name) {
return function() {
const getReduxState = remote.getGlobal('getReduxState');
if (!getReduxState || !getReduxState[ipc_event_name]) {
throw new Error(
'Could not find reduxState global in main process, did you forget to call replayActionMain?',
);
}
return JSON.parse(getReduxState[ipc_event_name]());
}
return JSON.parse(getReduxState());
}
29 changes: 17 additions & 12 deletions packages/electron-redux/src/helpers/replayActionMain.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { ipcMain } from 'electron';

export default function replayActionMain(store) {
/**
* Give renderers a way to sync the current state of the store, but be sure
* we don't expose any remote objects. In other words, we need our state to
* be serializable.
*
* Refer to https://github.com/electron/electron/blob/master/docs/api/remote.md#remote-objects
*/
global.getReduxState = () => JSON.stringify(store.getState());
export default function replayActionMain(ipc_event_name) {
return function (store) {
/**
* Give renderers a way to sync the current state of the store, but be sure
* we don't expose any remote objects. In other words, we need our state to
* be serializable.
*
* Refer to https://github.com/electron/electron/blob/master/docs/api/remote.md#remote-objects
*/
if(!global.getReduxState)
global.getReduxState = {};

ipcMain.on('redux-action', (event, payload) => {
store.dispatch(payload);
});
global.getReduxState[ipc_event_name] = () => JSON.stringify(store.getState());

ipcMain.on(ipc_event_name, (event, payload) => {
store.dispatch(payload);
});
};
}
10 changes: 6 additions & 4 deletions packages/electron-redux/src/helpers/replayActionRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ipcRenderer } from 'electron';

export default function replayActionRenderer(store) {
ipcRenderer.on('redux-action', (event, payload) => {
store.dispatch(payload);
});
export default function replayActionRenderer(ipc_event_name) {
return function (store) {
ipcRenderer.on(ipc_event_name, (event, payload) => {
store.dispatch(payload);
});
};
}
10 changes: 5 additions & 5 deletions packages/electron-redux/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Middleware, Store, AnyAction } from "redux";
export const forwardToRenderer: Middleware;
export const forwardToMain: Middleware;
export const forwardToRenderer: (ipc_event_name: string) => Middleware;
export const forwardToMain: (ipc_event_name: string) => Middleware;
export const triggerAlias: Middleware;
export function replayActionMain(store: Store): void;
export function replayActionRenderer(store: Store): void;
export function getInitialStateRenderer<T>(): T;
export function replayActionMain(ipc_event_name: string): (store: Store) => void;
export function replayActionRenderer(ipc_event_name: string): (store: Store) => void;
export function getInitialStateRenderer<T>(ipc_event_name: string): () => T;
export type ForwardToMainParams = { blacklist?: RegExp[] };
export function forwardToMainWithParams(
params?: ForwardToMainParams
Expand Down
5 changes: 3 additions & 2 deletions packages/electron-redux/src/middleware/forwardToMain.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ipcRenderer } from 'electron';
import validateAction from '../helpers/validateAction';
import {} from '../const';

// eslint-disable-next-line consistent-return, no-unused-vars
export const forwardToMainWithParams = (params = {}) => store => next => (action) => {
export const forwardToMainWithParams = (params = {}) => ipc_event_name => store => next => (action) => {
const { blacklist = [] } = params;
if (!validateAction(action)) return next(action);
if (action.meta && action.meta.scope === 'local') return next(action);
Expand All @@ -12,7 +13,7 @@ export const forwardToMainWithParams = (params = {}) => store => next => (action
}

// stop action in-flight
ipcRenderer.send('redux-action', action);
ipcRenderer.send(ipc_event_name, action);
};

const forwardToMain = forwardToMainWithParams({
Expand Down
4 changes: 2 additions & 2 deletions packages/electron-redux/src/middleware/forwardToRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { webContents } from 'electron';
import validateAction from '../helpers/validateAction';

const forwardToRenderer = () => next => (action) => {
const forwardToRenderer = ipc_event_name => () => next => (action) => {
if (!validateAction(action)) return next(action);
if (action.meta && action.meta.scope === 'local') return next(action);

Expand All @@ -17,7 +17,7 @@ const forwardToRenderer = () => next => (action) => {
const allWebContents = webContents.getAllWebContents();

allWebContents.forEach((contents) => {
contents.send('redux-action', rendererAction);
contents.send(ipc_event_name, rendererAction);
});

return next(action);
Expand Down

0 comments on commit 5795a5a

Please sign in to comment.