diff --git a/filenames.auto.gni b/filenames.auto.gni index f4c1c11b1cbb..78976452acb9 100644 --- a/filenames.auto.gni +++ b/filenames.auto.gni @@ -131,7 +131,7 @@ auto_filenames = { "lib/renderer/api/ipc-renderer.js", "lib/renderer/api/remote.js", "lib/renderer/api/web-frame.ts", - "lib/renderer/callbacks-registry.js", + "lib/renderer/callbacks-registry.ts", "lib/renderer/chrome-api.ts", "lib/renderer/content-scripts-injector.ts", "lib/renderer/extensions/event.ts", @@ -291,7 +291,7 @@ auto_filenames = { "lib/renderer/api/module-list.js", "lib/renderer/api/remote.js", "lib/renderer/api/web-frame.ts", - "lib/renderer/callbacks-registry.js", + "lib/renderer/callbacks-registry.ts", "lib/renderer/chrome-api.ts", "lib/renderer/content-scripts-injector.ts", "lib/renderer/extensions/event.ts", @@ -340,7 +340,7 @@ auto_filenames = { "lib/renderer/api/module-list.js", "lib/renderer/api/remote.js", "lib/renderer/api/web-frame.ts", - "lib/renderer/callbacks-registry.js", + "lib/renderer/callbacks-registry.ts", "lib/renderer/ipc-renderer-internal-utils.ts", "lib/renderer/ipc-renderer-internal.ts", "lib/renderer/webpack-provider.ts", diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index 01a57ffb4c62..009e7ea3c7d9 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -4,7 +4,7 @@ const v8Util = process.electronBinding('v8_util') const { isPromise } = require('electron') const resolvePromise = Promise.resolve.bind(Promise) -const CallbacksRegistry = require('@electron/internal/renderer/callbacks-registry') +const { CallbacksRegistry } = require('@electron/internal/renderer/callbacks-registry') const bufferUtils = require('@electron/internal/common/buffer-utils') const errorUtils = require('@electron/internal/common/error-utils') const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') diff --git a/lib/renderer/callbacks-registry.js b/lib/renderer/callbacks-registry.ts similarity index 76% rename from lib/renderer/callbacks-registry.js rename to lib/renderer/callbacks-registry.ts index 40ef0c840779..32d50e892719 100644 --- a/lib/renderer/callbacks-registry.js +++ b/lib/renderer/callbacks-registry.ts @@ -1,16 +1,12 @@ -'use strict' - const v8Util = process.electronBinding('v8_util') -class CallbacksRegistry { - constructor () { - this.nextId = 0 - this.callbacks = {} - } +export class CallbacksRegistry { + private nextId: number = 0 + private callbacks: Record = {} - add (callback) { + add (callback: Function) { // The callback is already added. - let id = v8Util.getHiddenValue(callback, 'callbackId') + let id = v8Util.getHiddenValue(callback, 'callbackId') if (id != null) return id id = this.nextId += 1 @@ -19,6 +15,7 @@ class CallbacksRegistry { // so that release errors can be tracked down easily. const regexp = /at (.*)/gi const stackString = (new Error()).stack + if (!stackString) return let filenameAndLine let match @@ -30,24 +27,25 @@ class CallbacksRegistry { if (location.includes('electron/js2c')) continue const ref = /([^/^)]*)\)?$/gi.exec(location) - filenameAndLine = ref[1] + if (ref) filenameAndLine = ref![1] break } + this.callbacks[id] = callback v8Util.setHiddenValue(callback, 'callbackId', id) v8Util.setHiddenValue(callback, 'location', filenameAndLine) return id } - get (id) { + get (id: number) { return this.callbacks[id] || function () {} } - apply (id, ...args) { + apply (id: number, ...args: any[]) { return this.get(id).apply(global, ...args) } - remove (id) { + remove (id: number) { const callback = this.callbacks[id] if (callback) { v8Util.deleteHiddenValue(callback, 'callbackId') @@ -55,5 +53,3 @@ class CallbacksRegistry { } } } - -module.exports = CallbacksRegistry diff --git a/spec/api-callbacks-registry-spec.js b/spec-main/api-callbacks-registry-spec.js similarity index 93% rename from spec/api-callbacks-registry-spec.js rename to spec-main/api-callbacks-registry-spec.js index 63f74bb048fd..5d8f0fef229f 100644 --- a/spec/api-callbacks-registry-spec.js +++ b/spec-main/api-callbacks-registry-spec.js @@ -4,7 +4,7 @@ const dirtyChai = require('dirty-chai') const { expect } = chai chai.use(dirtyChai) -const CallbacksRegistry = require('../lib/renderer/callbacks-registry') +const { CallbacksRegistry } = require('../lib/renderer/callbacks-registry') describe('CallbacksRegistry module', () => { let registry = null diff --git a/typings/internal-ambient.d.ts b/typings/internal-ambient.d.ts index 466c0b7d75a9..395a7bbc3576 100644 --- a/typings/internal-ambient.d.ts +++ b/typings/internal-ambient.d.ts @@ -16,6 +16,7 @@ declare namespace NodeJS { interface V8UtilBinding { getHiddenValue(obj: any, key: string): T; setHiddenValue(obj: any, key: string, value: T): void; + deleteHiddenValue(obj: any, key: string): void; requestGarbageCollectionForTesting(): void; } interface Process {