Просмотр исходного кода

Clean up `runSelected()` function.

Afshin Darian 6 лет назад
Родитель
Сommit
bb72e7f601
2 измененных файлов с 24 добавлено и 28 удалено
  1. 1 1
      packages/notebook-extension/src/index.ts
  2. 23 27
      packages/notebook/src/actions.tsx

+ 1 - 1
packages/notebook-extension/src/index.ts

@@ -35,7 +35,7 @@ import {
 
 import {
   CellTools, ICellTools, INotebookTracker, NotebookActions,
-  NotebookModelFactory,  NotebookPanel, NotebookTracker, NotebookWidgetFactory,
+  NotebookModelFactory, NotebookPanel, NotebookTracker, NotebookWidgetFactory,
   StaticNotebook
 } from '@jupyterlab/notebook';
 

+ 23 - 27
packages/notebook/src/actions.tsx

@@ -1296,38 +1296,34 @@ namespace Private {
    * Run the selected cells.
    */
   export
-  function runSelected(widget: Notebook, session?: IClientSession): Promise<boolean> {
-    widget.mode = 'command';
-    let selected: Cell[] = [];
-    let lastIndex = widget.activeCellIndex;
-    let i = 0;
-    each(widget.widgets, child => {
-      if (widget.isSelectedOrActive(child)) {
-        selected.push(child);
-        lastIndex = i;
+  function runSelected(notebook: Notebook, session?: IClientSession): Promise<boolean> {
+    notebook.mode = 'command';
+
+    let lastIndex = notebook.activeCellIndex;
+    const selected = notebook.widgets.filter((child, index) => {
+      const active = notebook.isSelectedOrActive(child);
+
+      if (active) {
+        lastIndex = index;
       }
-      i++;
-    });
-    widget.activeCellIndex = lastIndex;
-    widget.deselectAll();
 
-    let promises: Promise<boolean>[] = [];
-    each(selected, child => {
-      promises.push(runCell(widget, child, session));
+      return active;
     });
-    return Promise.all(promises).then(results => {
-      if (widget.isDisposed) {
-        return false;
-      }
-      // Post an update request.
-      widget.update();
-      for (let result of results) {
-        if (!result) {
+
+    notebook.activeCellIndex = lastIndex;
+    notebook.deselectAll();
+
+    return Promise.all(selected.map(child => runCell(notebook, child, session)))
+      .then(results => {
+        if (notebook.isDisposed) {
           return false;
         }
-      }
-      return true;
-    });
+
+        // Post an update request.
+        notebook.update();
+
+        return results.every(result => result);
+      });
   }
 
   /**