hadron-ipc
Advanced tools
Comparing version 0.0.0-next-a1c61e50bb5e8026e7dfd9aebe1b799e78146ed0 to 0.0.0-next-a1d78d9e601c2fc65ad3577b2f299654745d5d09
@@ -19,2 +19,3 @@ "use strict"; | ||
if (process.env.NODE_ENV !== 'test') { | ||
// eslint-disable-next-line no-console | ||
console.warn('Unsupported environment for hadron-ipc'); | ||
@@ -21,0 +22,0 @@ } |
import type { IpcMain, IpcMainEvent } from 'electron'; | ||
type ResponseHandler = (event: IpcMainEvent, ...args: any[]) => any; | ||
export declare function respondTo(ipcMain: Pick<IpcMain, 'on'> | undefined, methodName: string | Record<string, ResponseHandler>, handler?: ResponseHandler): void; | ||
/** | ||
* Broadcast an event to all the renderer processes | ||
*/ | ||
export declare function broadcast(channel: string, ...args: any[]): void; | ||
/** | ||
* Broadcast an event to currently focused window | ||
*/ | ||
export declare function broadcastFocused(channel: string, ...args: any[]): void; | ||
/** | ||
* Remove listener for a channel | ||
*/ | ||
export declare function remove(ipcMain: Pick<IpcMain, 'removeListener'> | undefined, channel: string, listener: (...args: unknown[]) => void): void; | ||
declare const ipcMain: (Electron.IpcMain & { | ||
/** | ||
* Broadcast an event to all the renderer processes | ||
*/ | ||
broadcast: typeof broadcast; | ||
/** | ||
* Broadcast an event to currently focused window | ||
*/ | ||
broadcastFocused: typeof broadcastFocused; | ||
/** | ||
* Set up a listener for a call from the renderer process dispatched with | ||
* a `ipcRenderer.call` or `ipcRenderer.callQuiet` method | ||
*/ | ||
respondTo: (methodName: string | Record<string, ResponseHandler>, handler?: ResponseHandler | undefined) => void; | ||
/** | ||
* Remove event listener from ipcMain | ||
*/ | ||
remove: (channel: string, listener: (...args: unknown[]) => void) => void; | ||
/** | ||
* Helper method to expose multiple methods from the main process through | ||
* `ipcMain.handle` with stricter types, better error handling, and | ||
* support for AbortController | ||
* | ||
* @param serviceName Identifier used to locate the service methods from | ||
* the renderer process | ||
* @param obj Object which methods will be exposed to the renderer | ||
* process | ||
* @param methodNames List of the method names that will get exposed | ||
*/ | ||
createHandle: <T>(serviceName: string, obj: T, methodNames: Extract<{ [k in keyof T]: T[k] extends (options: any) => Promise<any> ? k : never; }[keyof T], string>[]) => void; | ||
}) | undefined; | ||
/** | ||
* Exported for testing purposes | ||
* @internal | ||
*/ | ||
export declare const ControllerMap: Map<string, AbortController>; | ||
@@ -15,0 +52,0 @@ export declare function setupSignalHandler(_ipcMain?: Pick<IpcMain, 'handle'> | undefined, forceSetup?: boolean): void; |
@@ -62,3 +62,7 @@ "use strict"; | ||
exports.respondTo = respondTo; | ||
/** | ||
* Broadcast an event to all the renderer processes | ||
*/ | ||
function broadcast(channel, ...args) { | ||
// We might not be in electron environment | ||
electron_1.default.BrowserWindow?.getAllWindows().forEach((browserWindow) => { | ||
@@ -69,6 +73,13 @@ browserWindow.webContents?.send(channel, ...args); | ||
exports.broadcast = broadcast; | ||
/** | ||
* Broadcast an event to currently focused window | ||
*/ | ||
function broadcastFocused(channel, ...args) { | ||
// We might not be in electron environment | ||
electron_1.default.BrowserWindow?.getFocusedWindow()?.webContents?.send(channel, ...args); | ||
} | ||
exports.broadcastFocused = broadcastFocused; | ||
/** | ||
* Remove listener for a channel | ||
*/ | ||
function remove(ipcMain, channel, listener) { | ||
@@ -80,6 +91,30 @@ ipcMain?.removeListener(channel, listener); | ||
? Object.assign(electron_1.default.ipcMain, { | ||
/** | ||
* Broadcast an event to all the renderer processes | ||
*/ | ||
broadcast, | ||
/** | ||
* Broadcast an event to currently focused window | ||
*/ | ||
broadcastFocused, | ||
/** | ||
* Set up a listener for a call from the renderer process dispatched with | ||
* a `ipcRenderer.call` or `ipcRenderer.callQuiet` method | ||
*/ | ||
respondTo: respondTo.bind(null, electron_1.default.ipcMain), | ||
/** | ||
* Remove event listener from ipcMain | ||
*/ | ||
remove: remove.bind(null, electron_1.default.ipcMain), | ||
/** | ||
* Helper method to expose multiple methods from the main process through | ||
* `ipcMain.handle` with stricter types, better error handling, and | ||
* support for AbortController | ||
* | ||
* @param serviceName Identifier used to locate the service methods from | ||
* the renderer process | ||
* @param obj Object which methods will be exposed to the renderer | ||
* process | ||
* @param methodNames List of the method names that will get exposed | ||
*/ | ||
createHandle: (serviceName, obj, methodNames) => { | ||
@@ -90,2 +125,6 @@ return ipcHandle(ipcMain, serviceName, obj, methodNames); | ||
: undefined; | ||
/** | ||
* Exported for testing purposes | ||
* @internal | ||
*/ | ||
exports.ControllerMap = new Map(); | ||
@@ -92,0 +131,0 @@ let setup = false; |
import type { IpcRenderer } from 'electron'; | ||
export declare function call(ipcRenderer: Pick<IpcRenderer, 'on' | 'removeAllListeners' | 'send'> | undefined, debug: (...args: unknown[]) => void, methodName: string, ...args: any[]): Promise<any>; | ||
declare const ipcRenderer: (Electron.IpcRenderer & { | ||
/** | ||
* Call a method in the main process set up with `ipcMain.respondTo` | ||
* helper | ||
*/ | ||
call: (methodName: string, ...args: any[]) => Promise<any>; | ||
/** | ||
* Same as `ipcRenderer.call`, but doesn't print any debug information | ||
* when called (`debug` is no-op) | ||
*/ | ||
callQuiet: (methodName: string, ...args: any[]) => Promise<any>; | ||
/** | ||
* Helper method to create a caller for the method in the main process set | ||
* up with `ipcMain.createHandler` method | ||
* | ||
* @param serviceName Identifier used to locate the service methods in the | ||
* main process | ||
* @param methodNames List of the method names that will get exposed | ||
*/ | ||
createInvoke: <T, K extends Extract<{ [k in keyof T]: T[k] extends (options: any) => Promise<any> ? k : never; }[keyof T], string>>(serviceName: string, methodNames: K[]) => Pick<T, K>; | ||
@@ -7,0 +23,0 @@ }) | undefined; |
@@ -35,5 +35,22 @@ "use strict"; | ||
? Object.assign(electron_1.default.ipcRenderer, { | ||
/** | ||
* Call a method in the main process set up with `ipcMain.respondTo` | ||
* helper | ||
*/ | ||
call: call.bind(null, electron_1.default.ipcRenderer, debug), | ||
/** | ||
* Same as `ipcRenderer.call`, but doesn't print any debug information | ||
* when called (`debug` is no-op) | ||
*/ | ||
callQuiet: call.bind(null, electron_1.default.ipcRenderer, () => { | ||
// noop for a quiet call | ||
}), | ||
/** | ||
* Helper method to create a caller for the method in the main process set | ||
* up with `ipcMain.createHandler` method | ||
* | ||
* @param serviceName Identifier used to locate the service methods in the | ||
* main process | ||
* @param methodNames List of the method names that will get exposed | ||
*/ | ||
createInvoke: (serviceName, methodNames) => { | ||
@@ -56,2 +73,5 @@ return ipcInvoke(ipcRenderer, serviceName, methodNames); | ||
}; | ||
// If signal is already aborted, make sure that handler will see it | ||
// when it runs, otherwise just set up abort listener to communicate | ||
// this to main process | ||
if (signal?.aborted) { | ||
@@ -66,2 +86,4 @@ await onAbort(); | ||
const res = await ipcRenderer?.invoke(`${serviceName}.${name}`, { | ||
// We replace this with a matched signal on the other side, this | ||
// is mostly for testing / debugging purposes | ||
signal: signalId, | ||
@@ -68,0 +90,0 @@ ...rest, |
@@ -12,2 +12,4 @@ "use strict"; | ||
try { | ||
// If we can't serialize something, it will mess up with the error we | ||
// get on another side, ignore those properties | ||
v8_1.default.serialize(o[p]); | ||
@@ -24,5 +26,11 @@ return [p, o[p]]; | ||
} | ||
// We are serializing errors to get a better error shape on the other end, ipc | ||
// will only preserve message from the original error. See | ||
// https://github.com/electron/electron/issues/24427 | ||
function serializeErrorForIpc(err) { | ||
return { | ||
$$error: { | ||
// We serialize all the own properties as properties for the Error and then | ||
// cherry-pick name, message and stack, since those are properties that we | ||
// want to have even if they are not own props. | ||
...pickSerializeableProperties(err), | ||
@@ -29,0 +37,0 @@ name: err.name, |
{ | ||
"name": "hadron-ipc", | ||
"description": "Simplified IPC for electron apps.", | ||
"version": "0.0.0-next-a1c61e50bb5e8026e7dfd9aebe1b799e78146ed0", | ||
"version": "0.0.0-next-a1d78d9e601c2fc65ad3577b2f299654745d5d09", | ||
"author": { | ||
@@ -43,3 +43,3 @@ "name": "MongoDB Inc", | ||
"lint": "npm run eslint . && npm run prettier -- --check .", | ||
"depcheck": "depcheck", | ||
"depcheck": "compass-scripts check-peer-deps && depcheck", | ||
"check": "npm run typecheck && npm run lint && npm run depcheck", | ||
@@ -54,6 +54,6 @@ "check-ci": "npm run check", | ||
"devDependencies": { | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-a1c61e50bb5e8026e7dfd9aebe1b799e78146ed0", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-a1c61e50bb5e8026e7dfd9aebe1b799e78146ed0", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-a1c61e50bb5e8026e7dfd9aebe1b799e78146ed0", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-a1c61e50bb5e8026e7dfd9aebe1b799e78146ed0", | ||
"@mongodb-js/eslint-config-compass": "0.0.0-next-a1d78d9e601c2fc65ad3577b2f299654745d5d09", | ||
"@mongodb-js/mocha-config-compass": "0.0.0-next-a1d78d9e601c2fc65ad3577b2f299654745d5d09", | ||
"@mongodb-js/prettier-config-compass": "0.0.0-next-a1d78d9e601c2fc65ad3577b2f299654745d5d09", | ||
"@mongodb-js/tsconfig-compass": "0.0.0-next-a1d78d9e601c2fc65ad3577b2f299654745d5d09", | ||
"@types/chai": "^4.2.21", | ||
@@ -74,6 +74,6 @@ "@types/is-electron-renderer": "^2.0.1", | ||
"debug": "^4.3.4", | ||
"electron": "^25.9.6", | ||
"electron": "^30.5.1", | ||
"is-electron-renderer": "^2.0.1" | ||
}, | ||
"gitHead": "a1c61e50bb5e8026e7dfd9aebe1b799e78146ed0" | ||
"gitHead": "a1d78d9e601c2fc65ad3577b2f299654745d5d09" | ||
} |
@@ -144,7 +144,5 @@ # hadron-ipc [![npm][npm_img]][npm_url] | ||
const ipc = require('hadron-ipc'); | ||
const app = require('hadron-app'); | ||
global.hadronApp = app; | ||
ipc.on('app:refresh-data', () => | ||
global.hadronApp.appRegistry.emit('refresh-data') | ||
// do something | ||
); | ||
@@ -151,0 +149,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
70597
469
0
169
+ Added@types/node@20.17.10(transitive)
+ Addedelectron@30.5.1(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@types/node@18.19.68(transitive)
- Removedelectron@25.9.8(transitive)
- Removedundici-types@5.26.5(transitive)
Updatedelectron@^30.5.1