瀏覽代碼

HUE-8768 [editor] Implement status and progress update logic in notebook 2

Johan Ahlen 6 年之前
父節點
當前提交
b3e9a6dc7b

+ 13 - 0
desktop/core/src/desktop/js/apps/notebook2/execution/executableStatement.js

@@ -17,6 +17,7 @@
 import apiHelper from 'api/apiHelper';
 import { ExecutionResult } from 'apps/notebook2/execution/executionResult';
 import hueAnalytics from 'utils/hueAnalytics';
+import huePubSub from 'utils/huePubSub';
 
 /**
  * @type { { canceling: string, canceled: string, fail: string, ready: string, executing: string, done: string } }
@@ -34,6 +35,10 @@ const EXECUTION_STATUS = {
   closed: 'closed'
 };
 
+const notifyUpdates = executable => {
+  huePubSub.publish('hue.executable.updated', executable);
+};
+
 class ExecutableStatement {
   /**
    * @param options
@@ -98,6 +103,7 @@ class ExecutableStatement {
                 case 'running':
                 case 'starting':
                 case 'waiting':
+                  notifyUpdates(this);
                   checkStatusTimeout = window.setTimeout(
                     () => {
                       checkStatus()
@@ -117,11 +123,15 @@ class ExecutableStatement {
           this.lastCancellable.onCancel(() => {
             window.clearTimeout(checkStatusTimeout);
           });
+        }).finally(() => {
+          notifyUpdates(this);
         });
 
       hueAnalytics.log('notebook', 'execute/' + this.sourceType);
       this.status = EXECUTION_STATUS.running;
+      this.progress = 0;
 
+      notifyUpdates(this);
       this.lastCancellable = apiHelper
         .executeStatement({
           executable: this
@@ -149,8 +159,10 @@ class ExecutableStatement {
       if (this.lastCancellable && this.status === EXECUTION_STATUS.running) {
         hueAnalytics.log('notebook', 'cancel/' + this.sourceType);
         this.status = EXECUTION_STATUS.canceling;
+        notifyUpdates(this);
         this.lastCancellable.cancel().always(() => {
           this.status = EXECUTION_STATUS.canceled;
+          notifyUpdates(this);
           resolve();
         });
         this.lastCancellable = undefined;
@@ -169,6 +181,7 @@ class ExecutableStatement {
       }
     }).finally(() => {
       this.status = EXECUTION_STATUS.closed;
+      notifyUpdates(this);
     });
   }
 }

+ 13 - 17
desktop/core/src/desktop/js/apps/notebook2/execution/executor.js

@@ -84,18 +84,19 @@ class Executor {
       this.toExecute.push(new ExecutableStatement(options));
     }
 
-    this.setStatus(EXECUTION_STATUS.ready);
-    this.setProgress(0);
-  }
-
-  setStatus(status) {
-    this.status = status;
-    huePubSub.publish('hue.executor.status.updated', this);
-  }
-
-  setProgress(progress) {
-    this.progress = progress;
-    huePubSub.publish('hue.executor.progress.updated', this);
+    huePubSub.subscribe('hue.executable.updated', executable => {
+      if (
+        executable === this.currentExecutable ||
+        this.executed.some(executed => {
+          executed === executable;
+        })
+      ) {
+        huePubSub.publish('hue.executor.updated', {
+          executable: executable,
+          executor: this
+        });
+      }
+    });
   }
 
   isRunning() {
@@ -104,7 +105,6 @@ class Executor {
 
   async cancel() {
     if (this.isRunning()) {
-      this.setStatus(EXECUTION_STATUS.canceling);
       return await this.currentExecutable.cancel();
     }
   }
@@ -116,7 +116,6 @@ class Executor {
           reject();
         } else {
           this.currentExecutable = this.toExecute.shift();
-          this.setStatus(EXECUTION_STATUS.running);
           this.currentExecutable
             .execute()
             .then(executionResult => {
@@ -128,9 +127,6 @@ class Executor {
                   .then(executeBatch)
                   .catch(reject);
               } else {
-                this.setStatus(
-                  this.toExecute.length ? EXECUTION_STATUS.ready : EXECUTION_STATUS.success
-                );
                 resolve(executionResult);
               }
             })

+ 6 - 11
desktop/core/src/desktop/js/apps/notebook2/snippet.js

@@ -1099,18 +1099,13 @@ class Snippet {
 
     self.executor = undefined;
 
-    const updateExecutorObservable = (executor, name) => {
-      if (executor === self.executor && self[name]() !== executor[name]) {
-        self[name](executor[name]);
-      }
-    };
+    huePubSub.subscribe('hue.executor.updated', details => {
+      const executable = details.executable;
 
-    huePubSub.subscribe('hue.executor.status.updated', executor => {
-      updateExecutorObservable(executor, 'status');
-    });
-
-    huePubSub.subscribe('hue.executor.progress.updated', executor => {
-      updateExecutorObservable(executor, 'progress');
+      if (details.executor === self.executor) {
+        self.status(executable.status);
+        self.progress(executable.progress);
+      }
     });
   }