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) {
|
add (callback) {
|
||||||
// The callback is already added.
|
// The callback is already added.
|
||||||
var filenameAndLine, id, location, match, ref, regexp, stackString
|
let id = v8Util.getHiddenValue(callback, 'callbackId')
|
||||||
id = v8Util.getHiddenValue(callback, 'callbackId')
|
if (id != null) return id
|
||||||
if (id != null) {
|
|
||||||
return id
|
id = this.nextId += 1
|
||||||
}
|
|
||||||
id = ++this.nextId
|
|
||||||
|
|
||||||
// Capture the location of the function and put it in the ID string,
|
// Capture the location of the function and put it in the ID string,
|
||||||
// so that release errors can be tracked down easily.
|
// so that release errors can be tracked down easily.
|
||||||
regexp = /at (.*)/gi
|
const regexp = /at (.*)/gi
|
||||||
stackString = (new Error()).stack
|
const stackString = (new Error()).stack
|
||||||
|
|
||||||
|
let filenameAndLine
|
||||||
|
let match
|
||||||
|
|
||||||
while ((match = regexp.exec(stackString)) !== null) {
|
while ((match = regexp.exec(stackString)) !== null) {
|
||||||
location = match[1]
|
const location = match[1]
|
||||||
if (location.indexOf('(native)') !== -1) {
|
if (location.includes('native')) continue
|
||||||
continue
|
if (location.includes('electron.asar')) continue
|
||||||
}
|
|
||||||
if (location.indexOf('electron.asar') !== -1) {
|
const ref = /([^/^)]*)\)?$/gi.exec(location)
|
||||||
continue
|
|
||||||
}
|
|
||||||
ref = /([^/^)]*)\)?$/gi.exec(location)
|
|
||||||
filenameAndLine = ref[1]
|
filenameAndLine = ref[1]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -40,8 +39,7 @@ class CallbacksRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
get (id) {
|
get (id) {
|
||||||
var ref
|
return this.callbacks[id] || function () {}
|
||||||
return (ref = this.callbacks[id]) != null ? ref : function () {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
apply (id, ...args) {
|
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…
Add table
Add a link
Reference in a new issue