From 00efc6d1f188908a18eba36867ac401d4943c48b Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Fri, 25 Mar 2016 22:37:06 +0100 Subject: [PATCH] Update ipc-main + ipc-renderer modules --- .../github-electron-renderer-tests.ts | 4 +++- github-electron/github-electron.ipc-main.d.ts | 21 ++++++++++++++++-- .../github-electron.ipc-renderer.d.ts | 22 +++++++++++++++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/github-electron/github-electron-renderer-tests.ts b/github-electron/github-electron-renderer-tests.ts index b827b2b56..6ec4d0b2f 100644 --- a/github-electron/github-electron-renderer-tests.ts +++ b/github-electron/github-electron-renderer-tests.ts @@ -16,9 +16,11 @@ import * as fs from 'fs'; // https://github.com/atom/electron/blob/master/docs/api/ipc-renderer.md console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong" -ipcRenderer.on('asynchronous-reply', (arg: any) => { +ipcRenderer.on('asynchronous-reply', (event: Electron.IpcRendererEvent, arg: any) => { console.log(arg); // prints "pong" + event.sender.send('another-message', 'Hello World!'); }); + ipcRenderer.send('asynchronous-message', 'ping'); // remote diff --git a/github-electron/github-electron.ipc-main.d.ts b/github-electron/github-electron.ipc-main.d.ts index 887df011a..200f1af15 100644 --- a/github-electron/github-electron.ipc-main.d.ts +++ b/github-electron/github-electron.ipc-main.d.ts @@ -7,13 +7,30 @@ /// declare namespace Electron { - + /** + * The ipcMain module handles asynchronous and synchronous messages + * sent from a renderer process (web page). + * Messages sent from a renderer will be emitted to this module. + */ class IpcMain extends EventEmitter { - on(event: string, listener: (event: IpcMainEvent, ...args: any[]) => any): this; + addListener(channel: string, listener: IpcMainEventListener): this; + on(channel: string, listener: IpcMainEventListener): this; + once(channel: string, listener: IpcMainEventListener): this; + removeListener(channel: string, listener: IpcMainEventListener): this; + removeAllListeners(channel?: string): this; } + type IpcMainEventListener = (event: IpcMainEvent, ...args: any[]) => void; + interface IpcMainEvent { + /** + * Set this to the value to be returned in a synchronous message. + */ returnValue?: any; + /** + * Returns the webContents that sent the message, you can call sender.send + * to reply to the asynchronous message. + */ sender: WebContents; } } diff --git a/github-electron/github-electron.ipc-renderer.d.ts b/github-electron/github-electron.ipc-renderer.d.ts index 1fa41385f..00f429eeb 100644 --- a/github-electron/github-electron.ipc-renderer.d.ts +++ b/github-electron/github-electron.ipc-renderer.d.ts @@ -6,8 +6,17 @@ /// declare namespace Electron { - + /** + * The ipcRenderer module provides a few methods so you can send synchronous + * and asynchronous messages from the render process (web page) to the main process. + * You can also receive replies from the main process. + */ class IpcRenderer extends EventEmitter { + addListener(channel: string, listener: IpcRendererEventListener): this; + on(channel: string, listener: IpcRendererEventListener): this; + once(channel: string, listener: IpcRendererEventListener): this; + removeListener(channel: string, listener: IpcRendererEventListener): this; + removeAllListeners(channel?: string): this; /** * Send ...args to the renderer via channel in asynchronous message, the main * process can handle it by listening to the channel event of ipc module. @@ -21,11 +30,20 @@ declare namespace Electron { * message would block the whole renderer process. * @returns The result sent from the main process. */ - sendSync(channel: string, ...args: any[]): string; + sendSync(channel: string, ...args: any[]): any; /** * Like ipc.send but the message will be sent to the host page instead of the main process. * This is mainly used by the page in to communicate with host page. */ sendToHost(channel: string, ...args: any[]): void; } + + type IpcRendererEventListener = (event: IpcRendererEvent, ...args: any[]) => void; + + interface IpcRendererEvent { + /** + * You can call sender.send to reply to the asynchronous message. + */ + sender: IpcRenderer; + } }