Browse Source

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

Milan Burda 4 years ago
parent
commit
8b74361b0c
2 changed files with 12 additions and 13 deletions
  1. 5 5
      lib/browser/remote/objects-registry.ts
  2. 7 8
      lib/renderer/remote/callbacks-registry.ts

+ 5 - 5
lib/browser/remote/objects-registry.ts

@@ -4,8 +4,6 @@ const getOwnerKey = (webContents: WebContents, contextId: string) => {
   return `${webContents.id}-${contextId}`;
 };
 
-const electronIds = new WeakMap<Object, number>();
-
 class ObjectsRegistry {
   private nextId: number = 0
 
@@ -17,6 +15,8 @@ class ObjectsRegistry {
   // (ownerKey) => { id: refCount }
   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
   // registered then the already assigned ID would be returned.
   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.
   saveToStorage (object: any) {
-    let id = electronIds.get(object);
+    let id = this.electronIds.get(object);
     if (!id) {
       id = ++this.nextId;
       this.storage[id] = {
         count: 0,
         object: object
       };
-      electronIds.set(object, id);
+      this.electronIds.set(object, id);
     }
     return id;
   }
@@ -101,7 +101,7 @@ class ObjectsRegistry {
     }
     pointer.count -= 1;
     if (pointer.count === 0) {
-      electronIds.delete(pointer.object);
+      this.electronIds.delete(pointer.object);
       delete this.storage[id];
     }
   }

+ 7 - 8
lib/renderer/remote/callbacks-registry.ts

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