Merge pull request #11135 from electron/add_callbacks_spec
add callbacks registry spec
This commit is contained in:
commit
e10feb6be2
3 changed files with 1099 additions and 18 deletions
|
@ -10,26 +10,25 @@ class CallbacksRegistry {
|
|||
|
||||
add (callback) {
|
||||
// The callback is already added.
|
||||
var filenameAndLine, id, location, match, ref, regexp, stackString
|
||||
id = v8Util.getHiddenValue(callback, 'callbackId')
|
||||
if (id != null) {
|
||||
return id
|
||||
}
|
||||
id = ++this.nextId
|
||||
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.
|
||||
regexp = /at (.*)/gi
|
||||
stackString = (new Error()).stack
|
||||
const regexp = /at (.*)/gi
|
||||
const stackString = (new Error()).stack
|
||||
|
||||
let filenameAndLine
|
||||
let match
|
||||
|
||||
while ((match = regexp.exec(stackString)) !== null) {
|
||||
location = match[1]
|
||||
if (location.indexOf('(native)') !== -1) {
|
||||
continue
|
||||
}
|
||||
if (location.indexOf('electron.asar') !== -1) {
|
||||
continue
|
||||
}
|
||||
ref = /([^/^)]*)\)?$/gi.exec(location)
|
||||
const location = match[1]
|
||||
if (location.includes('native')) continue
|
||||
if (location.includes('electron.asar')) continue
|
||||
|
||||
const ref = /([^/^)]*)\)?$/gi.exec(location)
|
||||
filenameAndLine = ref[1]
|
||||
break
|
||||
}
|
||||
|
@ -40,8 +39,7 @@ class CallbacksRegistry {
|
|||
}
|
||||
|
||||
get (id) {
|
||||
var ref
|
||||
return (ref = this.callbacks[id]) != null ? ref : function () {}
|
||||
return this.callbacks[id] || function () {}
|
||||
}
|
||||
|
||||
apply (id, ...args) {
|
||||
|
|
48
spec/api-callbacks-registry-spec.js
Normal file
48
spec/api-callbacks-registry-spec.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
const {assert} = require('chai')
|
||||
const {CallbacksRegistry} = require('electron')
|
||||
|
||||
describe('CallbacksRegistry module', () => {
|
||||
let registry = null
|
||||
|
||||
beforeEach(() => {
|
||||
registry = new CallbacksRegistry()
|
||||
})
|
||||
|
||||
it('adds a callback to the registry', () => {
|
||||
const cb = () => [1, 2, 3, 4, 5]
|
||||
const key = registry.add(cb)
|
||||
|
||||
assert.exists(key)
|
||||
})
|
||||
|
||||
it('returns a specified callback if it is in the registry', () => {
|
||||
const cb = () => [1, 2, 3, 4, 5]
|
||||
const key = registry.add(cb)
|
||||
const callback = registry.get(key)
|
||||
|
||||
assert.equal(callback.toString(), cb.toString())
|
||||
})
|
||||
|
||||
it('returns an empty function if the cb doesnt exist', () => {
|
||||
const callback = registry.get(1)
|
||||
|
||||
assert.isFunction(callback)
|
||||
})
|
||||
|
||||
it('removes a callback to the registry', () => {
|
||||
const cb = () => [1, 2, 3, 4, 5]
|
||||
const key = registry.add(cb)
|
||||
|
||||
assert.exists(key)
|
||||
|
||||
const beforeCB = registry.get(key)
|
||||
|
||||
assert.equal(beforeCB.toString(), cb.toString())
|
||||
|
||||
registry.remove(key)
|
||||
const afterCB = registry.get(key)
|
||||
|
||||
assert.isFunction(afterCB)
|
||||
assert.notEqual(afterCB.toString(), cb.toString())
|
||||
})
|
||||
})
|
1035
spec/package-lock.json
generated
Normal file
1035
spec/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue