electron/lib/browser/ipc-main-impl.ts
Milan Burda 71fb19ea14
chore: upgrade to eslint v8 (#38472)
chore: upgrade to eslint@8
2023-05-30 13:10:22 +02:00

34 lines
1,007 B
TypeScript

import { EventEmitter } from 'events';
import { IpcMainInvokeEvent } from 'electron/main';
export class IpcMainImpl extends EventEmitter {
private _invokeHandlers: Map<string, (e: IpcMainInvokeEvent, ...args: any[]) => void> = new Map();
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 Error(`Expected handler to be a function, but found type '${typeof fn}'`);
}
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);
}
}