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

fix: stop making actions of initial renderer async #256

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"plugins": [
"import"
],
"globals": {
"window": true
},
"settings": {
"import/core-modules": [ "electron" ]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-redux/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export function replayActionMain(store: Store): void;
export function replayActionRenderer(store: Store): void;
export function getInitialStateRenderer<T>(): T;
export type ForwardToMainParams = { blacklist?: RegExp[] };
export type ForwardToMainParams = { id?: string, blacklist?: RegExp[] };
export function forwardToMainWithParams(
params?: ForwardToMainParams
): Middleware;
Expand Down
22 changes: 18 additions & 4 deletions packages/electron-redux/src/middleware/forwardToMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
9 changes: 7 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 = () => (next) => (action) => {
if (!validateAction(action)) return next(action);
if (action.meta && action.meta.scope === 'local') return next(action);

Expand All @@ -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);
Expand Down