Skip to content
Permalink
Browse files

refactor: convert ipc-renderer.ts to TypeScript (#19556)

* refactor: convert ipc-renderer.ts to TypeScript

* Update typings/internal-ambient.d.ts

Co-Authored-By: Samuel Attard <sattard@slack-corp.com>
  • Loading branch information...
2 people authored and ckerr committed Aug 2, 2019
1 parent ee64c6a commit 3011a0f14f7c23c5ce56fe6554a2796c02d40503
@@ -140,7 +140,7 @@ auto_filenames = {
"lib/common/web-view-methods.ts",
"lib/renderer/api/crash-reporter.js",
"lib/renderer/api/desktop-capturer.ts",
"lib/renderer/api/ipc-renderer.js",
"lib/renderer/api/ipc-renderer.ts",
"lib/renderer/api/remote.js",
"lib/renderer/api/web-frame.ts",
"lib/renderer/callbacks-registry.ts",
@@ -299,7 +299,7 @@ auto_filenames = {
"lib/renderer/api/crash-reporter.js",
"lib/renderer/api/desktop-capturer.ts",
"lib/renderer/api/exports/electron.js",
"lib/renderer/api/ipc-renderer.js",
"lib/renderer/api/ipc-renderer.ts",
"lib/renderer/api/module-list.js",
"lib/renderer/api/remote.js",
"lib/renderer/api/web-frame.ts",
@@ -348,7 +348,7 @@ auto_filenames = {
"lib/renderer/api/crash-reporter.js",
"lib/renderer/api/desktop-capturer.ts",
"lib/renderer/api/exports/electron.js",
"lib/renderer/api/ipc-renderer.js",
"lib/renderer/api/ipc-renderer.ts",
"lib/renderer/api/module-list.js",
"lib/renderer/api/remote.js",
"lib/renderer/api/web-frame.ts",
@@ -1,10 +1,8 @@
'use strict'

const { ipc } = process.electronBinding('ipc')
const v8Util = process.electronBinding('v8_util')

// Created by init.js.
const ipcRenderer = v8Util.getHiddenValue(global, 'ipc')
const ipcRenderer = v8Util.getHiddenValue<Electron.IpcRenderer>(global, 'ipc')
const internal = false

ipcRenderer.send = function (channel, ...args) {
@@ -23,15 +21,11 @@ ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
return ipc.sendTo(internal, false, webContentsId, channel, args)
}

ipcRenderer.sendToAll = function (webContentsId, channel, ...args) {
return ipc.sendTo(internal, true, webContentsId, channel, args)
}

ipcRenderer.invoke = function (channel, ...args) {
return ipc.invoke(channel, args).then(({ error, result }) => {
if (error) { throw new Error(`Error invoking remote method '${channel}': ${error}`) }
return result
})
}

module.exports = ipcRenderer
export default ipcRenderer
@@ -1,17 +1,16 @@
import { Event } from '@electron/internal/renderer/extensions/event'
import { IpcMainEvent } from 'electron'
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'

class WebNavigation {
private onBeforeNavigate = new Event()
private onCompleted = new Event()

constructor () {
ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', (event: IpcMainEvent, details: any) => {
ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONBEFORENAVIGATE', (event: Electron.IpcRendererEvent, details: any) => {
this.onBeforeNavigate.emit(details)
})

ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONCOMPLETED', (event: IpcMainEvent, details: any) => {
ipcRendererInternal.on('CHROME_WEBNAVIGATION_ONCOMPLETED', (event: Electron.IpcRendererEvent, details: any) => {
this.onCompleted.emit(details)
})
}
@@ -1,22 +1,22 @@
const binding = process.electronBinding('ipc')
const { ipc } = process.electronBinding('ipc')
const v8Util = process.electronBinding('v8_util')

// Created by init.js.
export const ipcRendererInternal: Electron.IpcRendererInternal = v8Util.getHiddenValue(global, 'ipc-internal')
export const ipcRendererInternal = v8Util.getHiddenValue<Electron.IpcRendererInternal>(global, 'ipc-internal')
const internal = true

ipcRendererInternal.send = function (channel, ...args) {
return binding.ipc.send(internal, channel, args)
return ipc.send(internal, channel, args)
}

ipcRendererInternal.sendSync = function (channel, ...args) {
return binding.ipc.sendSync(internal, channel, args)[0]
return ipc.sendSync(internal, channel, args)[0]
}

ipcRendererInternal.sendTo = function (webContentsId, channel, ...args) {
return binding.ipc.sendTo(internal, false, webContentsId, channel, args)
return ipc.sendTo(internal, false, webContentsId, channel, args)
}

ipcRendererInternal.sendToAll = function (webContentsId, channel, ...args) {
return binding.ipc.sendTo(internal, true, webContentsId, channel, args)
return ipc.sendTo(internal, true, webContentsId, channel, args)
}
@@ -11,10 +11,10 @@ const v8Util = process.electronBinding('v8_util')
// Expose Buffer shim as a hidden value. This is used by C++ code to
// deserialize Buffer instances sent from browser process.
v8Util.setHiddenValue(global, 'Buffer', Buffer)
// The `lib/renderer/api/ipc-renderer.js` module looks for the ipc object in the
// The `lib/renderer/api/ipc-renderer.ts` module looks for the ipc object in the
// "ipc" hidden value
v8Util.setHiddenValue(global, 'ipc', new EventEmitter())
// The `lib/renderer/ipc-renderer-internal.js` module looks for the ipc object in the
// The `lib/renderer/ipc-renderer-internal.ts` module looks for the ipc object in the
// "ipc-internal" hidden value
v8Util.setHiddenValue(global, 'ipc-internal', new EventEmitter())
// The process object created by webpack is not an event emitter, fix it so
@@ -14,6 +14,14 @@ declare namespace NodeJS {
isComponentBuild(): boolean;
}

interface IpcBinding {
send(internal: boolean, channel: string, args: any[]): void;
sendSync(internal: boolean, channel: string, args: any[]): any;
sendToHost(channel: string, args: any[]): void;
sendTo(internal: boolean, sendToAll: boolean, webContentsId: number, channel: string, args: any[]): void;
invoke<T>(channel: string, args: any[]): Promise<{ error: string, result: T }>;
}

interface V8UtilBinding {
getHiddenValue<T>(obj: any, key: string): T;
setHiddenValue<T>(obj: any, key: string, value: T): void;
@@ -28,6 +36,7 @@ declare namespace NodeJS {
_linkedBinding(name: string): any;
electronBinding(name: string): any;
electronBinding(name: 'features'): FeaturesBinding;
electronBinding(name: 'ipc'): { ipc: IpcBinding };
electronBinding(name: 'v8_util'): V8UtilBinding;
electronBinding(name: 'app'): { app: Electron.App, App: Function };
electronBinding(name: 'command_line'): Electron.CommandLine;

0 comments on commit 3011a0f

Please sign in to comment.
You can’t perform that action at this time.