electron/lib/browser/ipc-main-impl.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
1 KiB
TypeScript
Raw Normal View History

import { EventEmitter } from 'events';
import { IpcMainInvokeEvent } from 'electron/main';
export class IpcMainImpl extends EventEmitter implements Electron.IpcMain {
private _invokeHandlers: Map<string, (e: IpcMainInvokeEvent, ...args: any[]) => void> = new Map();
2022-08-03 23:55:12 +00:00
constructor () {
super();
// Do not throw exception when channel name is "error".
this.on('error', () => {});
}
handle: Electron.IpcMain['handle'] = (method, fn) => {
if (this._invokeHandlers.has(method)) {
throw new Error(`Attempted to register a second handler for '${method}'`);
}
if (typeof fn !== 'function') {
throw new TypeError(`Expected handler to be a function, but found type '${typeof fn}'`);
}
2023-02-13 21:39:18 +00:00
this._invokeHandlers.set(method, fn);
};
handleOnce: Electron.IpcMain['handleOnce'] = (method, fn) => {
this.handle(method, (e, ...args) => {
this.removeHandler(method);
return fn(e, ...args);
});
};
removeHandler (method: string) {
this._invokeHandlers.delete(method);
}
}