Skip to content

Commit

Permalink
chore: convert callbacks-registry to ts (#18682)
Browse files Browse the repository at this point in the history
* chore: convert callbacks-registry to ts

* fix class import syntax

* move cb reg specs to spec-main
  • Loading branch information
codebytere authored and zcbenz committed Jun 15, 2019
1 parent 441857c commit 3309005
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
6 changes: 3 additions & 3 deletions filenames.auto.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion lib/renderer/api/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -30,30 +27,29 @@ 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')
delete this.callbacks[id]
}
}
}

module.exports = CallbacksRegistry
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions typings/internal-ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3309005

Please sign in to comment.