refactor: don't expose CallbacksRegistry as an internal module (#14389)
This commit is contained in:
parent
0c4e7104cf
commit
3a79eacb6f
6 changed files with 4 additions and 9 deletions
|
@ -1,59 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
|
||||
class CallbacksRegistry {
|
||||
constructor () {
|
||||
this.nextId = 0
|
||||
this.callbacks = {}
|
||||
}
|
||||
|
||||
add (callback) {
|
||||
// The callback is already added.
|
||||
let id = v8Util.getHiddenValue(callback, 'callbackId')
|
||||
if (id != null) return id
|
||||
|
||||
id = this.nextId += 1
|
||||
|
||||
// Capture the location of the function and put it in the ID string,
|
||||
// so that release errors can be tracked down easily.
|
||||
const regexp = /at (.*)/gi
|
||||
const stackString = (new Error()).stack
|
||||
|
||||
let filenameAndLine
|
||||
let match
|
||||
|
||||
while ((match = regexp.exec(stackString)) !== null) {
|
||||
const location = match[1]
|
||||
if (location.includes('(native)')) continue
|
||||
if (location.includes('(<anonymous>)')) continue
|
||||
if (location.includes('electron.asar')) continue
|
||||
|
||||
const ref = /([^/^)]*)\)?$/gi.exec(location)
|
||||
filenameAndLine = ref[1]
|
||||
break
|
||||
}
|
||||
this.callbacks[id] = callback
|
||||
v8Util.setHiddenValue(callback, 'callbackId', id)
|
||||
v8Util.setHiddenValue(callback, 'location', filenameAndLine)
|
||||
return id
|
||||
}
|
||||
|
||||
get (id) {
|
||||
return this.callbacks[id] || function () {}
|
||||
}
|
||||
|
||||
apply (id, ...args) {
|
||||
return this.get(id).apply(global, ...args)
|
||||
}
|
||||
|
||||
remove (id) {
|
||||
const callback = this.callbacks[id]
|
||||
if (callback) {
|
||||
v8Util.deleteHiddenValue(callback, 'callbackId')
|
||||
delete this.callbacks[id]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CallbacksRegistry
|
|
@ -5,7 +5,6 @@ module.exports = [
|
|||
{name: 'nativeImage', file: 'native-image'},
|
||||
{name: 'shell', file: 'shell'},
|
||||
// The internal modules, invisible unless you know their names.
|
||||
{name: 'CallbacksRegistry', file: 'callbacks-registry', private: true},
|
||||
{name: 'deprecate', file: 'deprecate', private: true},
|
||||
{name: 'deprecations', file: 'deprecations', private: true},
|
||||
{name: 'isPromise', file: 'is-promise', private: true}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue