Explorar o código

HUE-9263 [frontend] Prevent disposal of static components

Johan Ahlen %!s(int64=5) %!d(string=hai) anos
pai
achega
c254bba123

+ 95 - 109
desktop/core/src/desktop/js/ko/components/assist/ko.assistDashboardPanel.js

@@ -25,7 +25,6 @@ import { TEMPLATE, AssistantUtils } from 'ko/components/assist/ko.assistEditorCo
 
 class AssistDashboardPanel {
   constructor() {
-    this.disposals = [];
     this.isSolr = ko.observable(true);
 
     this.showRisks = ko.observable(false);
@@ -58,121 +57,108 @@ class AssistDashboardPanel {
     };
     const i18n = {};
 
-    const activeDashboardCollection = huePubSub.subscribe(
-      'set.active.dashboard.collection',
-      collection => {
-        const collectionName = collection.name();
-
-        if (!collectionName) {
-          return;
-        }
-
-        this.sourceType = ko.observable(collection.engine());
-
-        const assistDbSource = new AssistDbSource({
-          i18n: i18n,
-          initialNamespace: collection.activeNamespace,
-          initialCompute: collection.activeCompute,
-          type: collection.engine(),
-          name: collection.engine(),
-          nonSqlType: true,
-          navigationSettings: navigationSettings
-        });
+    huePubSub.subscribe('set.active.dashboard.collection', collection => {
+      const collectionName = collection.name();
 
-        const fakeParentName =
-          collectionName.indexOf('.') > -1 ? collectionName.split('.')[0] : 'default';
-
-        const sourceType =
-          collection.source() === 'query' ? collection.engine() + '-query' : collection.engine();
-
-        dataCatalog
-          .getEntry({
-            sourceType: sourceType,
-            namespace: collection.activeNamespace,
-            compute: collection.activeCompute,
-            connector: {}, // TODO: Use connectors in assist dashboard panel
-            path: [fakeParentName],
-            definition: { type: 'database' }
-          })
-          .done(fakeDbCatalogEntry => {
-            const assistFakeDb = new AssistDbEntry(
-              fakeDbCatalogEntry,
-              null,
-              assistDbSource,
-              this.filter,
-              i18n,
-              navigationSettings
-            );
-            dataCatalog
-              .getEntry({
-                sourceType: sourceType,
-                namespace: collection.activeNamespace,
-                compute: collection.activeCompute,
-                connector: {}, // TODO: Use connectors in assist dashboard panel
-                path: [
-                  fakeParentName,
-                  collectionName.indexOf('.') > -1 ? collectionName.split('.')[1] : collectionName
-                ],
-                definition: { type: 'table' }
-              })
-              .done(collectionCatalogEntry => {
-                const collectionEntry = new AssistDbEntry(
-                  collectionCatalogEntry,
-                  assistFakeDb,
-                  assistDbSource,
-                  this.filter,
-                  i18n,
-                  navigationSettings
-                );
-                this.activeTables([collectionEntry]);
-
-                if (
-                  !collectionEntry.loaded &&
-                  !collectionEntry.hasErrors() &&
-                  !collectionEntry.loading()
-                ) {
-                  collectionEntry.loadEntries(() => {
-                    collectionEntry.toggleOpen();
-                  });
-                }
-              });
-          });
+      if (!collectionName) {
+        return;
+      }
+
+      this.sourceType = ko.observable(collection.engine());
+
+      const assistDbSource = new AssistDbSource({
+        i18n: i18n,
+        initialNamespace: collection.activeNamespace,
+        initialCompute: collection.activeCompute,
+        type: collection.engine(),
+        name: collection.engine(),
+        nonSqlType: true,
+        navigationSettings: navigationSettings
+      });
+
+      const fakeParentName =
+        collectionName.indexOf('.') > -1 ? collectionName.split('.')[0] : 'default';
+
+      const sourceType =
+        collection.source() === 'query' ? collection.engine() + '-query' : collection.engine();
+
+      dataCatalog
+        .getEntry({
+          sourceType: sourceType,
+          namespace: collection.activeNamespace,
+          compute: collection.activeCompute,
+          connector: {}, // TODO: Use connectors in assist dashboard panel
+          path: [fakeParentName],
+          definition: { type: 'database' }
+        })
+        .done(fakeDbCatalogEntry => {
+          const assistFakeDb = new AssistDbEntry(
+            fakeDbCatalogEntry,
+            null,
+            assistDbSource,
+            this.filter,
+            i18n,
+            navigationSettings
+          );
+          dataCatalog
+            .getEntry({
+              sourceType: sourceType,
+              namespace: collection.activeNamespace,
+              compute: collection.activeCompute,
+              connector: {}, // TODO: Use connectors in assist dashboard panel
+              path: [
+                fakeParentName,
+                collectionName.indexOf('.') > -1 ? collectionName.split('.')[1] : collectionName
+              ],
+              definition: { type: 'table' }
+            })
+            .done(collectionCatalogEntry => {
+              const collectionEntry = new AssistDbEntry(
+                collectionCatalogEntry,
+                assistFakeDb,
+                assistDbSource,
+                this.filter,
+                i18n,
+                navigationSettings
+              );
+              this.activeTables([collectionEntry]);
 
-        this.autocompleteFromEntries = function(nonPartial, partial) {
-          const added = {};
-          const result = [];
-          const partialLower = partial.toLowerCase();
-          this.activeTables().forEach(table => {
-            if (
-              !added[table.catalogEntry.name] &&
-              table.catalogEntry.name.toLowerCase().indexOf(partialLower) === 0
-            ) {
-              added[table.catalogEntry.name] = true;
-              result.push(nonPartial + partial + table.catalogEntry.name.substring(partial.length));
-            }
-            table.entries().forEach(col => {
               if (
-                !added[col.catalogEntry.name] &&
-                col.catalogEntry.name.toLowerCase().indexOf(partialLower) === 0
+                !collectionEntry.loaded &&
+                !collectionEntry.hasErrors() &&
+                !collectionEntry.loading()
               ) {
-                added[col.catalogEntry.name] = true;
-                result.push(nonPartial + partial + col.catalogEntry.name.substring(partial.length));
+                collectionEntry.loadEntries(() => {
+                  collectionEntry.toggleOpen();
+                });
               }
             });
-          });
-          return result;
-        };
-      }
-    );
-
-    this.disposals.push(() => {
-      activeDashboardCollection.remove();
-    });
-  }
+        });
 
-  dispose() {
-    this.disposals.forEach(dispose => {
-      dispose();
+      this.autocompleteFromEntries = function(nonPartial, partial) {
+        const added = {};
+        const result = [];
+        const partialLower = partial.toLowerCase();
+        this.activeTables().forEach(table => {
+          if (
+            !added[table.catalogEntry.name] &&
+            table.catalogEntry.name.toLowerCase().indexOf(partialLower) === 0
+          ) {
+            added[table.catalogEntry.name] = true;
+            result.push(nonPartial + partial + table.catalogEntry.name.substring(partial.length));
+          }
+          table.entries().forEach(col => {
+            if (
+              !added[col.catalogEntry.name] &&
+              col.catalogEntry.name.toLowerCase().indexOf(partialLower) === 0
+            ) {
+              added[col.catalogEntry.name] = true;
+              result.push(nonPartial + partial + col.catalogEntry.name.substring(partial.length));
+            }
+          });
+        });
+        return result;
+      };
     });
   }
 }

+ 5 - 26
desktop/core/src/desktop/js/ko/components/assist/ko.assistEditorContextPanel.js

@@ -197,7 +197,6 @@ class AssistantUtils {
 
 class AssistEditorContextPanel {
   constructor(params) {
-    this.disposals = [];
     this.isSolr = ko.observable(false);
     this.activeTab = params.activeTab;
 
@@ -333,11 +332,7 @@ class AssistEditorContextPanel {
       return result;
     };
 
-    const activeTablesSub = this.activeTables.subscribe(loadEntries);
-    this.disposals.push(() => {
-      window.clearTimeout(loadEntriesTimeout);
-      activeTablesSub.dispose();
-    });
+    this.activeTables.subscribe(loadEntries);
 
     let updateOnVisible = false;
 
@@ -591,7 +586,7 @@ class AssistEditorContextPanel {
       }
     };
 
-    const entryRefreshedSub = huePubSub.subscribe('data.catalog.entry.refreshed', details => {
+    huePubSub.subscribe('data.catalog.entry.refreshed', details => {
       const sourceType = details.entry.getSourceType();
       if (sources[sourceType]) {
         let completeRefresh = false;
@@ -631,18 +626,13 @@ class AssistEditorContextPanel {
       updateOnVisible = true;
     }
 
-    const activeTabSub = this.activeTab.subscribe(activeTab => {
+    this.activeTab.subscribe(activeTab => {
       if (activeTab === 'editorAssistant' && updateOnVisible) {
         huePubSub.publish('get.active.editor.locations', handleLocationUpdate);
       }
     });
 
-    this.disposals.push(() => {
-      entryRefreshedSub.remove();
-      activeTabSub.dispose();
-    });
-
-    const activeLocationsSub = huePubSub.subscribe('editor.active.locations', activeLocations => {
+    huePubSub.subscribe('editor.active.locations', activeLocations => {
       if (this.activeTab() === 'editorAssistant') {
         handleLocationUpdate(activeLocations);
       } else {
@@ -650,7 +640,7 @@ class AssistEditorContextPanel {
       }
     });
 
-    const activeRisksSub = huePubSub.subscribe('editor.active.risks', details => {
+    huePubSub.subscribe('editor.active.risks', details => {
       if (details.risks !== this.activeRisks()) {
         this.activeRisks(details.risks);
         this.activeEditor(details.editor);
@@ -661,11 +651,6 @@ class AssistEditorContextPanel {
       this.activeRisks(details.risks);
       this.activeEditor(details.editor);
     });
-
-    this.disposals.push(() => {
-      activeLocationsSub.remove();
-      activeRisksSub.remove();
-    });
   }
 
   addFilter(riskId) {
@@ -732,12 +717,6 @@ class AssistEditorContextPanel {
       }
     });
   }
-
-  dispose() {
-    this.disposals.forEach(dispose => {
-      dispose();
-    });
-  }
 }
 
 componentUtils.registerStaticComponent(

+ 5 - 23
desktop/core/src/desktop/js/ko/components/assist/ko.assistSchedulePanel.js

@@ -62,12 +62,11 @@ const TEMPLATE = `
 
 class AssistSchedulePanel {
   constructor() {
-    this.disposals = [];
     this.selectedNotebook = ko.observable();
 
     // TODO: Move all the scheduler logic out of the notebook to here.
 
-    const selectedNotebookSub = this.selectedNotebook.subscribe(notebook => {
+    this.selectedNotebook.subscribe(notebook => {
       // Happening 4 times for each notebook loaded
       if (
         notebook &&
@@ -81,9 +80,8 @@ class AssistSchedulePanel {
         }
       }
     });
-    this.disposals.push(selectedNotebookSub.dispose.bind(selectedNotebookSub));
 
-    const setSelectedNotebookSub = huePubSub.subscribe('jobbrowser.schedule.data', jobs => {
+    huePubSub.subscribe('jobbrowser.schedule.data', jobs => {
       if (this.selectedNotebook() && this.selectedNotebook().viewSchedulerId()) {
         const job = jobs.filter(job => this.selectedNotebook().viewSchedulerId() === job.id);
         this.selectedNotebook().isSchedulerJobRunning(
@@ -91,24 +89,15 @@ class AssistSchedulePanel {
         );
       }
     });
-    this.disposals.push(setSelectedNotebookSub.remove.bind(setSelectedNotebookSub));
 
     // Hue 3
-    const setSelectedNotebookSub3 = huePubSub.subscribe(
-      'set.selected.notebook',
-      this.selectedNotebook
-    );
-    this.disposals.push(setSelectedNotebookSub3.remove.bind(setSelectedNotebookSub3));
+    huePubSub.subscribe('set.selected.notebook', this.selectedNotebook);
 
-    const selectedNotebookChangedSub = huePubSub.subscribe(
-      'selected.notebook.changed',
-      this.selectedNotebook
-    );
-    this.disposals.push(selectedNotebookChangedSub.remove.bind(selectedNotebookChangedSub));
+    huePubSub.subscribe('selected.notebook.changed', this.selectedNotebook);
     huePubSub.publish('get.selected.notebook');
 
     // Hue 4
-    const currentAppSub = huePubSub.subscribe('set.current.app.view.model', viewModel => {
+    huePubSub.subscribe('set.current.app.view.model', viewModel => {
       if (viewModel.selectedNotebook) {
         if (viewModel.selectedNotebook()) {
           this.selectedNotebook(viewModel.selectedNotebook());
@@ -122,13 +111,6 @@ class AssistSchedulePanel {
         this.selectedNotebook(null);
       }
     });
-    this.disposals.push(currentAppSub.remove.bind(currentAppSub));
-  }
-
-  dispose() {
-    this.disposals.forEach(dispose => {
-      dispose();
-    });
   }
 }
 

+ 1 - 13
desktop/core/src/desktop/js/ko/components/assist/ko.rightAssistPanel.js

@@ -108,8 +108,6 @@ const TEMPLATE = `
 
 class RightAssistPanel {
   constructor(params) {
-    this.disposals = [];
-
     this.activeTab = ko.observable();
     this.visible = params.visible;
     this.connector = ko.observable();
@@ -203,11 +201,7 @@ class RightAssistPanel {
       updateTabs();
     };
 
-    const snippetTypeSub = huePubSub.subscribe(
-      ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT,
-      updateContentsForConnector
-    );
-    this.disposals.push(snippetTypeSub.remove.bind(snippetTypeSub));
+    huePubSub.subscribe(ACTIVE_SNIPPET_CONNECTOR_CHANGED_EVENT, updateContentsForConnector);
 
     const onAppChange = appName => {
       if (appName === 'dashboard') {
@@ -255,12 +249,6 @@ class RightAssistPanel {
     this.lastActiveTabEditor(SCHEDULES_TAB);
     this.switchTab(SCHEDULES_TAB);
   }
-
-  dispose() {
-    this.disposals.forEach(dispose => {
-      dispose();
-    });
-  }
 }
 
 componentUtils.registerStaticComponent('right-assist-panel', RightAssistPanel, TEMPLATE);

+ 3 - 0
desktop/core/src/desktop/js/ko/components/componentUtils.js

@@ -48,6 +48,9 @@ class componentUtils {
               instances[name] = new model(params);
             }
           }
+          if (instances[name] && instances[name].dispose) {
+            console.warn('"dispose" function present on static component ' + name);
+          }
           return instances[name];
         }
       },