Skip to content

Commit

Permalink
refactor: use Map for callbacks in CallbacksRegistry (#20565)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored and codebytere committed Oct 15, 2019
1 parent dceabf2 commit 5273930
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/renderer/remote/callbacks-registry.ts
Expand Up @@ -2,7 +2,7 @@ const v8Util = process.electronBinding('v8_util')

export class CallbacksRegistry {
private nextId: number = 0
private callbacks: Record<number, Function> = {}
private callbacks = new Map<number, Function>()

add (callback: Function) {
// The callback is already added.
Expand Down Expand Up @@ -31,25 +31,25 @@ export class CallbacksRegistry {
break
}

this.callbacks[id] = callback
this.callbacks.set(id, callback)
v8Util.setHiddenValue(callback, 'callbackId', id)
v8Util.setHiddenValue(callback, 'location', filenameAndLine)
return id
}

get (id: number) {
return this.callbacks[id] || function () {}
return this.callbacks.get(id) || function () {}
}

apply (id: number, ...args: any[]) {
return this.get(id).apply(global, ...args)
}

remove (id: number) {
const callback = this.callbacks[id]
const callback = this.callbacks.get(id)
if (callback) {
v8Util.deleteHiddenValue(callback, 'callbackId')
delete this.callbacks[id]
this.callbacks.delete(id)
}
}
}

0 comments on commit 5273930

Please sign in to comment.