chore: convert callbacks-registry to ts (#18682)

* chore: convert callbacks-registry to ts

* fix class import syntax

* move cb reg specs to spec-main
This commit is contained in:
Shelley Vohr 2019-06-15 01:18:25 -07:00 committed by Cheng Zhao
parent 441857c6e7
commit 3309005325
5 changed files with 17 additions and 20 deletions

View file

@ -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",

View file

@ -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')

View file

@ -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<number, Function> = {}
add (callback) {
add (callback: Function) {
// The callback is already added.
let id = v8Util.getHiddenValue(callback, 'callbackId')
let id = v8Util.getHiddenValue<number>(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

View file

@ -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

View file

@ -16,6 +16,7 @@ declare namespace NodeJS {
interface V8UtilBinding {
getHiddenValue<T>(obj: any, key: string): T;
setHiddenValue<T>(obj: any, key: string, value: T): void;
deleteHiddenValue(obj: any, key: string): void;
requestGarbageCollectionForTesting(): void;
}
interface Process {