refactor: store WeakMaps in CallbacksRegistry / ObjectsRegistry (#27037)

This commit is contained in:
Milan Burda 2021-01-20 23:03:10 +01:00 committed by GitHub
parent 7f1e3ca3de
commit 8b74361b0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 13 deletions

View file

@ -4,8 +4,6 @@ const getOwnerKey = (webContents: WebContents, contextId: string) => {
return `${webContents.id}-${contextId}`; return `${webContents.id}-${contextId}`;
}; };
const electronIds = new WeakMap<Object, number>();
class ObjectsRegistry { class ObjectsRegistry {
private nextId: number = 0 private nextId: number = 0
@ -17,6 +15,8 @@ class ObjectsRegistry {
// (ownerKey) => { id: refCount } // (ownerKey) => { id: refCount }
private owners: Record<string, Map<number, number>> = {} private owners: Record<string, Map<number, number>> = {}
private electronIds = new WeakMap<Object, number>();
// Register a new object and return its assigned ID. If the object is already // Register a new object and return its assigned ID. If the object is already
// registered then the already assigned ID would be returned. // registered then the already assigned ID would be returned.
add (webContents: WebContents, contextId: string, obj: any) { add (webContents: WebContents, contextId: string, obj: any) {
@ -81,14 +81,14 @@ class ObjectsRegistry {
// Private: Saves the object into storage and assigns an ID for it. // Private: Saves the object into storage and assigns an ID for it.
saveToStorage (object: any) { saveToStorage (object: any) {
let id = electronIds.get(object); let id = this.electronIds.get(object);
if (!id) { if (!id) {
id = ++this.nextId; id = ++this.nextId;
this.storage[id] = { this.storage[id] = {
count: 0, count: 0,
object: object object: object
}; };
electronIds.set(object, id); this.electronIds.set(object, id);
} }
return id; return id;
} }
@ -101,7 +101,7 @@ class ObjectsRegistry {
} }
pointer.count -= 1; pointer.count -= 1;
if (pointer.count === 0) { if (pointer.count === 0) {
electronIds.delete(pointer.object); this.electronIds.delete(pointer.object);
delete this.storage[id]; delete this.storage[id];
} }
} }

View file

@ -1,13 +1,12 @@
const callbackIds = new WeakMap<Function, number>();
const locationInfo = new WeakMap<Function, string>();
export class CallbacksRegistry { export class CallbacksRegistry {
private nextId: number = 0 private nextId: number = 0
private callbacks = new Map<number, Function>() private callbacks = new Map<number, Function>()
private callbackIds = new WeakMap<Function, number>();
private locationInfo = new WeakMap<Function, string>();
add (callback: Function) { add (callback: Function) {
// The callback is already added. // The callback is already added.
let id = callbackIds.get(callback); let id = this.callbackIds.get(callback);
if (id != null) return id; if (id != null) return id;
id = this.nextId += 1; id = this.nextId += 1;
@ -33,8 +32,8 @@ export class CallbacksRegistry {
} }
this.callbacks.set(id, callback); this.callbacks.set(id, callback);
callbackIds.set(callback, id); this.callbackIds.set(callback, id);
locationInfo.set(callback, filenameAndLine!); this.locationInfo.set(callback, filenameAndLine!);
return id; return id;
} }
@ -43,7 +42,7 @@ export class CallbacksRegistry {
} }
getLocation (callback: Function) { getLocation (callback: Function) {
return locationInfo.get(callback); return this.locationInfo.get(callback);
} }
apply (id: number, ...args: any[]) { apply (id: number, ...args: any[]) {
@ -53,7 +52,7 @@ export class CallbacksRegistry {
remove (id: number) { remove (id: number) {
const callback = this.callbacks.get(id); const callback = this.callbacks.get(id);
if (callback) { if (callback) {
callbackIds.delete(callback); this.callbackIds.delete(callback);
this.callbacks.delete(id); this.callbacks.delete(id);
} }
} }