Factored out ipcRenderer.on
Summary: Delegate receiving events from the Electron host process to the RenderHost interface. Typed them as well while at it. Reviewed By: timur-valiev Differential Revision: D31828130 fbshipit-source-id: 920055de6f48512b91a433b229dbacd611c6f139
This commit is contained in:
committed by
Facebook GitHub Bot
parent
cc946d12ec
commit
ecf2e32722
@@ -7,6 +7,22 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {NotificationEvents} from './dispatcher/notifications';
|
||||
import {PluginNotification} from './reducers/notifications';
|
||||
|
||||
// Events that are emitted from the main.ts ovr the IPC process bridge in Electron
|
||||
type MainProcessEvents = {
|
||||
'flipper-protocol-handler': [query: string];
|
||||
'open-flipper-file': [url: string];
|
||||
notificationEvent: [
|
||||
eventName: NotificationEvents,
|
||||
pluginNotification: PluginNotification,
|
||||
arg: null | string | number,
|
||||
];
|
||||
trackUsage: any[];
|
||||
getLaunchTime: [launchStartTime: number];
|
||||
};
|
||||
|
||||
/**
|
||||
* Utilities provided by the render host, e.g. Electron, the Browser, etc
|
||||
*/
|
||||
@@ -16,6 +32,10 @@ export interface RenderHost {
|
||||
selectDirectory?(defaultPath?: string): Promise<string | undefined>;
|
||||
registerShortcut(shortCut: string, callback: () => void): void;
|
||||
hasFocus(): boolean;
|
||||
onIpcEvent<Event extends keyof MainProcessEvents>(
|
||||
event: Event,
|
||||
callback: (...arg: MainProcessEvents[Event]) => void,
|
||||
): void;
|
||||
}
|
||||
|
||||
let renderHostInstance: RenderHost | undefined;
|
||||
@@ -41,5 +61,6 @@ if (process.env.NODE_ENV === 'test') {
|
||||
hasFocus() {
|
||||
return true;
|
||||
},
|
||||
onIpcEvent() {},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// Fine for app startup.
|
||||
// eslint-disable-next-line flipper/no-electron-remote-imports
|
||||
import {remote, ipcRenderer, IpcRendererEvent} from 'electron';
|
||||
import {remote} from 'electron';
|
||||
import {Store} from '../reducers/index';
|
||||
import {Logger} from 'flipper-common';
|
||||
import {
|
||||
@@ -19,9 +19,12 @@ import {
|
||||
import {tryCatchReportPlatformFailures} from 'flipper-common';
|
||||
import {handleDeeplink} from '../deeplink';
|
||||
import {Dialog} from 'flipper-plugin';
|
||||
import {getRenderHostInstance} from '../RenderHost';
|
||||
|
||||
export default (store: Store, logger: Logger) => {
|
||||
const currentWindow = remote.getCurrentWindow();
|
||||
const renderHost = getRenderHostInstance();
|
||||
|
||||
const onFocus = () => {
|
||||
setImmediate(() => {
|
||||
store.dispatch({
|
||||
@@ -55,28 +58,22 @@ export default (store: Store, logger: Logger) => {
|
||||
});
|
||||
});
|
||||
|
||||
ipcRenderer.on(
|
||||
'flipper-protocol-handler',
|
||||
(_event: IpcRendererEvent, query: string) => {
|
||||
handleDeeplink(store, logger, query).catch((e) => {
|
||||
console.warn('Failed to handle deeplink', query, e);
|
||||
Dialog.alert({
|
||||
title: 'Failed to open deeplink',
|
||||
type: 'error',
|
||||
message: `Failed to handle deeplink '${query}': ${
|
||||
e.message ?? e.toString()
|
||||
}`,
|
||||
});
|
||||
renderHost.onIpcEvent('flipper-protocol-handler', (query: string) => {
|
||||
handleDeeplink(store, logger, query).catch((e) => {
|
||||
console.warn('Failed to handle deeplink', query, e);
|
||||
Dialog.alert({
|
||||
title: 'Failed to open deeplink',
|
||||
type: 'error',
|
||||
message: `Failed to handle deeplink '${query}': ${
|
||||
e.message ?? e.toString()
|
||||
}`,
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
ipcRenderer.on(
|
||||
'open-flipper-file',
|
||||
(_event: IpcRendererEvent, url: string) => {
|
||||
tryCatchReportPlatformFailures(() => {
|
||||
return importFileToStore(url, store);
|
||||
}, `${IMPORT_FLIPPER_TRACE_EVENT}:Deeplink`);
|
||||
},
|
||||
);
|
||||
renderHost.onIpcEvent('open-flipper-file', (url: string) => {
|
||||
tryCatchReportPlatformFailures(() => {
|
||||
return importFileToStore(url, store);
|
||||
}, `${IMPORT_FLIPPER_TRACE_EVENT}:Deeplink`);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
import {Store} from '../reducers/index';
|
||||
import {Logger} from 'flipper-common';
|
||||
import {PluginNotification} from '../reducers/notifications';
|
||||
import {ipcRenderer, IpcRendererEvent} from 'electron';
|
||||
import {ipcRenderer} from 'electron';
|
||||
import {
|
||||
updatePluginBlocklist,
|
||||
updateCategoryBlocklist,
|
||||
@@ -19,18 +19,24 @@ import {textContent} from 'flipper-plugin';
|
||||
import {getPluginTitle} from '../utils/pluginUtils';
|
||||
import {sideEffect} from '../utils/sideEffect';
|
||||
import {openNotification} from '../sandy-chrome/notification/Notification';
|
||||
import {getRenderHostInstance} from '../RenderHost';
|
||||
|
||||
export type NotificationEvents =
|
||||
| 'show'
|
||||
| 'click'
|
||||
| 'close'
|
||||
| 'reply'
|
||||
| 'action';
|
||||
|
||||
type NotificationEvents = 'show' | 'click' | 'close' | 'reply' | 'action';
|
||||
const NOTIFICATION_THROTTLE = 5 * 1000; // in milliseconds
|
||||
|
||||
export default (store: Store, logger: Logger) => {
|
||||
const knownNotifications: Set<string> = new Set();
|
||||
const lastNotificationTime: Map<string, number> = new Map();
|
||||
|
||||
ipcRenderer.on(
|
||||
getRenderHostInstance().onIpcEvent(
|
||||
'notificationEvent',
|
||||
(
|
||||
_event: IpcRendererEvent,
|
||||
eventName: NotificationEvents,
|
||||
pluginNotification: PluginNotification,
|
||||
arg: null | string | number,
|
||||
|
||||
@@ -7,9 +7,6 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
// Used for PID tracking.
|
||||
// eslint-disable-next-line flipper/no-electron-remote-imports
|
||||
import {ipcRenderer} from 'electron';
|
||||
import {performance} from 'perf_hooks';
|
||||
import {EventEmitter} from 'events';
|
||||
|
||||
@@ -146,16 +143,15 @@ export default (store: Store, logger: Logger) => {
|
||||
);
|
||||
}
|
||||
|
||||
ipcRenderer.on('trackUsage', (event, ...args: any[]) => {
|
||||
renderHost.onIpcEvent('trackUsage', (...args: any[]) => {
|
||||
let state: State;
|
||||
try {
|
||||
state = store.getState();
|
||||
} catch (e) {
|
||||
// if trackUsage is called (indirectly) through a reducer, this will utterly die Flipper. Let's prevent that and log an error instead
|
||||
console.error(
|
||||
'trackUsage triggered indirectly as side effect of a reducer. Event: ',
|
||||
event.type,
|
||||
event,
|
||||
'trackUsage triggered indirectly as side effect of a reducer',
|
||||
e,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -297,5 +297,10 @@ function initializeFlipperForElectron() {
|
||||
hasFocus() {
|
||||
return remote.getCurrentWindow().isFocused();
|
||||
},
|
||||
onIpcEvent(event, callback) {
|
||||
ipcRenderer.on(event, (_ev, ...args: any[]) => {
|
||||
callback(...(args as any));
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import fbConfig from '../fb-stubs/config';
|
||||
import {isFBEmployee} from '../utils/fbEmployee';
|
||||
import {notification} from 'antd';
|
||||
import isProduction from '../utils/isProduction';
|
||||
import {getRenderHostInstance} from '../RenderHost';
|
||||
|
||||
export type ToplevelNavItem =
|
||||
| 'appinspect'
|
||||
@@ -214,9 +215,16 @@ function registerStartupTime(logger: Logger) {
|
||||
// track time since launch
|
||||
const [s, ns] = process.hrtime();
|
||||
const launchEndTime = s * 1e3 + ns / 1e6;
|
||||
ipcRenderer.on('getLaunchTime', (_: any, launchStartTime: number) => {
|
||||
logger.track('performance', 'launchTime', launchEndTime - launchStartTime);
|
||||
});
|
||||
getRenderHostInstance().onIpcEvent(
|
||||
'getLaunchTime',
|
||||
(launchStartTime: number) => {
|
||||
logger.track(
|
||||
'performance',
|
||||
'launchTime',
|
||||
launchEndTime - launchStartTime,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
ipcRenderer.send('getLaunchTime');
|
||||
ipcRenderer.send('componentDidMount');
|
||||
|
||||
Reference in New Issue
Block a user