Bug 1323845: Part 3 - Auto-create intermediate API namespace objects when necessary. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Tue, 09 Jan 2018 16:02:38 -0800
changeset 718300 2af921afb6bb3cdee920c7093d58caf09f8f4c4c
parent 718299 545a27f5130be2e6b2fb1db470a169c2bf76cac5
child 718301 becb0b9cc67b75c4d269fd1ce462c2af222cac8a
push id94869
push usermaglione.k@gmail.com
push dateWed, 10 Jan 2018 01:49:31 +0000
reviewersaswan
bugs1323845
milestone59.0a1
Bug 1323845: Part 3 - Auto-create intermediate API namespace objects when necessary. r?aswan MozReview-Commit-ID: HA8WpynPOqp
toolkit/components/extensions/ExtensionCommon.jsm
--- a/toolkit/components/extensions/ExtensionCommon.jsm
+++ b/toolkit/components/extensions/ExtensionCommon.jsm
@@ -826,28 +826,32 @@ class CanOfAPIs {
   findAPIPath(path) {
     if (this.apiPaths.has(path)) {
       return this.apiPaths.get(path);
     }
 
     let obj = this.root;
     let modules = this.apiManager.modulePaths;
 
-    for (let key of path.split(".")) {
+    let parts = path.split(".");
+    for (let [i, key] of parts.entries()) {
       if (!obj) {
         return;
       }
       modules = getChild(modules, key);
 
       for (let name of modules.modules) {
         if (!this.apis.has(name)) {
           this.loadAPI(name);
         }
       }
 
+      if (!(key in obj) && i < parts.length - 1) {
+        obj[key] = {};
+      }
       obj = obj[key];
     }
 
     this.apiPaths.set(path, obj);
     return obj;
   }
 
   /**
@@ -862,28 +866,33 @@ class CanOfAPIs {
   async asyncFindAPIPath(path) {
     if (this.apiPaths.has(path)) {
       return this.apiPaths.get(path);
     }
 
     let obj = this.root;
     let modules = this.apiManager.modulePaths;
 
-    for (let key of path.split(".")) {
+    let parts = path.split(".");
+    for (let [i, key] of parts.entries()) {
       if (!obj) {
         return;
       }
       modules = getChild(modules, key);
 
       for (let name of modules.modules) {
         if (!this.apis.has(name)) {
           await this.asyncLoadAPI(name);
         }
       }
 
+      if (!(key in obj) && i < parts.length - 1) {
+        obj[key] = {};
+      }
+
       if (typeof obj[key] === "function") {
         obj = obj[key].bind(obj);
       } else {
         obj = obj[key];
       }
     }
 
     this.apiPaths.set(path, obj);