electron/lib/browser/ipc-main-impl.ts
Samuel Attard e9cb85bea6
build: add import/order eslint rule (#44106)
build: add import/order eslint rule (#44085)

* build: add import/order eslint rule

* chore: run lint:js --fix
2024-10-02 20:52:01 -07:00

35 lines
1 KiB
TypeScript

import { IpcMainInvokeEvent } from 'electron/main';
import { EventEmitter } from 'events';
export class IpcMainImpl extends EventEmitter implements Electron.IpcMain {
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 TypeError(`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);
}
}