diff --git a/.eslintrc b/.eslintrc index e119a36d..9b4d8661 100644 --- a/.eslintrc +++ b/.eslintrc @@ -8,6 +8,9 @@ "plugins": [ "import" ], + "globals": { + "window": true + }, "settings": { "import/core-modules": [ "electron" ] } diff --git a/packages/electron-redux/src/index.d.ts b/packages/electron-redux/src/index.d.ts index 9c4b97a6..0d3a5825 100644 --- a/packages/electron-redux/src/index.d.ts +++ b/packages/electron-redux/src/index.d.ts @@ -5,7 +5,7 @@ export function replayActionMain(store: Store): void; export function replayActionRenderer(store: Store): void; export function getInitialStateRenderer(): T; - export type ForwardToMainParams = { blacklist?: RegExp[] }; + export type ForwardToMainParams = { id?: string, blacklist?: RegExp[] }; export function forwardToMainWithParams( params?: ForwardToMainParams ): Middleware; diff --git a/packages/electron-redux/src/middleware/forwardToMain.js b/packages/electron-redux/src/middleware/forwardToMain.js index 1dd603d1..4d6d39bd 100644 --- a/packages/electron-redux/src/middleware/forwardToMain.js +++ b/packages/electron-redux/src/middleware/forwardToMain.js @@ -2,17 +2,31 @@ import { ipcRenderer } from 'electron'; import validateAction from '../helpers/validateAction'; // eslint-disable-next-line consistent-return, no-unused-vars -export const forwardToMainWithParams = (params = {}) => store => next => (action) => { - const { blacklist = [] } = params; +export const forwardToMainWithParams = (params = {}) => (store) => (next) => (action) => { + const { blacklist = [], id } = params; if (!validateAction(action)) return next(action); if (action.meta && action.meta.scope === 'local') return next(action); - if (blacklist.some(rule => rule.test(action.type))) { + if (blacklist.some((rule) => rule.test(action.type))) { return next(action); } + let mainAction = action; + + if (id) { + next(action); + mainAction = { + ...action, + meta: { + ...action.meta, + id, + }, + }; + window.electronReduxId = id; + } + // stop action in-flight - ipcRenderer.send('redux-action', action); + ipcRenderer.send('redux-action', mainAction); }; const forwardToMain = forwardToMainWithParams({ diff --git a/packages/electron-redux/src/middleware/forwardToRenderer.js b/packages/electron-redux/src/middleware/forwardToRenderer.js index 403aa001..653be823 100644 --- a/packages/electron-redux/src/middleware/forwardToRenderer.js +++ b/packages/electron-redux/src/middleware/forwardToRenderer.js @@ -1,7 +1,7 @@ import { webContents } from 'electron'; import validateAction from '../helpers/validateAction'; -const forwardToRenderer = () => next => (action) => { +const forwardToRenderer = () => (next) => (action) => { if (!validateAction(action)) return next(action); if (action.meta && action.meta.scope === 'local') return next(action); @@ -17,7 +17,12 @@ const forwardToRenderer = () => next => (action) => { const allWebContents = webContents.getAllWebContents(); allWebContents.forEach((contents) => { - contents.send('redux-action', rendererAction); + if (contents.getURL().startsWith('devtools://')) return; + contents.executeJavaScript('window.electronReduxId').then((id) => { + if (action.meta.id !== id || !id) { + contents.send('redux-action', rendererAction); + } + }); }); return next(action);