refactor: use Map for callbacks in CallbacksRegistry (#20565)

This commit is contained in:
Milan Burda 2019-10-15 18:14:41 +02:00 коммит произвёл Shelley Vohr
Родитель dceabf231b
Коммит 5273930f76
1 изменённых файлов: 5 добавлений и 5 удалений

Просмотреть файл

@ -2,7 +2,7 @@ const v8Util = process.electronBinding('v8_util')
export class CallbacksRegistry { export class CallbacksRegistry {
private nextId: number = 0 private nextId: number = 0
private callbacks: Record<number, Function> = {} private callbacks = new Map<number, Function>()
add (callback: Function) { add (callback: Function) {
// The callback is already added. // The callback is already added.
@ -31,14 +31,14 @@ export class CallbacksRegistry {
break break
} }
this.callbacks[id] = callback this.callbacks.set(id, callback)
v8Util.setHiddenValue(callback, 'callbackId', id) v8Util.setHiddenValue(callback, 'callbackId', id)
v8Util.setHiddenValue(callback, 'location', filenameAndLine) v8Util.setHiddenValue(callback, 'location', filenameAndLine)
return id return id
} }
get (id: number) { get (id: number) {
return this.callbacks[id] || function () {} return this.callbacks.get(id) || function () {}
} }
apply (id: number, ...args: any[]) { apply (id: number, ...args: any[]) {
@ -46,10 +46,10 @@ export class CallbacksRegistry {
} }
remove (id: number) { remove (id: number) {
const callback = this.callbacks[id] const callback = this.callbacks.get(id)
if (callback) { if (callback) {
v8Util.deleteHiddenValue(callback, 'callbackId') v8Util.deleteHiddenValue(callback, 'callbackId')
delete this.callbacks[id] this.callbacks.delete(id)
} }
} }
} }