Skip to content

Commit

Permalink
refactor: convert ipc-renderer.ts to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jul 31, 2019
1 parent a5f87ce commit 6beef3b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions filenames.auto.gni
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
@@ -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')
export const ipcRenderer = v8Util.getHiddenValue<Electron.IpcRenderer>(global, 'ipc')
const internal = false

ipcRenderer.send = function (channel, ...args) {
Expand All @@ -23,15 +21,9 @@ 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 }) => {
return ipc.invoke(internal, channel, args).then(({ error, result }) => {
if (error) { throw new Error(`Error invoking remote method '${channel}': ${error}`) }
return result
})
}

module.exports = ipcRenderer
7 changes: 3 additions & 4 deletions lib/renderer/extensions/web-navigation.ts
@@ -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)
})
}
Expand Down
12 changes: 6 additions & 6 deletions lib/renderer/ipc-renderer-internal.ts
@@ -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)
}
4 changes: 2 additions & 2 deletions lib/sandboxed_renderer/init.js
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions typings/internal-ambient.d.ts
Expand Up @@ -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(internal: boolean, channel: string, args: any[]): Promise<{ error: string, result: any }>;
}

interface V8UtilBinding {
getHiddenValue<T>(obj: any, key: string): T;
setHiddenValue<T>(obj: any, key: string, value: T): void;
Expand All @@ -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;
Expand Down

0 comments on commit 6beef3b

Please sign in to comment.