add callbacks registry spec
This commit is contained in:
parent
0bfbd9b3c4
commit
e5983eacd6
2 changed files with 58 additions and 17 deletions
|
@ -10,25 +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 ref
|
||||||
|
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.indexOf('(native)') !== -1) continue
|
||||||
continue
|
if (location.indexOf('electron.asar') !== -1) continue
|
||||||
}
|
|
||||||
if (location.indexOf('electron.asar') !== -1) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ref = /([^/^)]*)\)?$/gi.exec(location)
|
ref = /([^/^)]*)\)?$/gi.exec(location)
|
||||||
filenameAndLine = ref[1]
|
filenameAndLine = ref[1]
|
||||||
break
|
break
|
||||||
|
@ -40,8 +40,7 @@ class CallbacksRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
get (id) {
|
get (id) {
|
||||||
var ref
|
return this.callbacks[id] != null ? this.callbacks[id] : () => {}
|
||||||
return (ref = this.callbacks[id]) != null ? ref : function () {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
apply (id, ...args) {
|
apply (id, ...args) {
|
||||||
|
|
42
spec/api-callbacks-registry-spec.js
Normal file
42
spec/api-callbacks-registry-spec.js
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
const assert = require('assert')
|
||||||
|
const {CallbacksRegistry} = require('electron')
|
||||||
|
|
||||||
|
describe.only('CallbacksRegistry module', () => {
|
||||||
|
let registry = null
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
registry = new CallbacksRegistry()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('adds a callback to the registry', () => {
|
||||||
|
const cb = () => [1, 2, 3, 4, 5]
|
||||||
|
const id = registry.add(cb)
|
||||||
|
assert.equal(id, 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns a specified callback if it is in the registry', () => {
|
||||||
|
const cb = () => [1, 2, 3, 4, 5]
|
||||||
|
registry.add(cb)
|
||||||
|
|
||||||
|
const callback = registry.get(1)
|
||||||
|
assert.equal(callback.toString(), cb.toString())
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns an empty function if the cb doesnt exist', () => {
|
||||||
|
const callback = registry.get(1)
|
||||||
|
assert.equal(callback.toString(), 'function () {}')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('removes a callback to the registry', () => {
|
||||||
|
const cb = () => [1, 2, 3, 4, 5]
|
||||||
|
const id = registry.add(cb)
|
||||||
|
assert.equal(id, 1)
|
||||||
|
|
||||||
|
const beforeCB = registry.get(1)
|
||||||
|
assert.equal(beforeCB.toString(), cb.toString())
|
||||||
|
|
||||||
|
registry.remove(1)
|
||||||
|
const afterCB = registry.get(1)
|
||||||
|
assert.equal(afterCB.toString(), 'function () {}')
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue