You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2021/12/20 18:21:35 UTC

[GitHub] [netbeans] sdedic commented on a change in pull request #3382: Delay change events for node after the node's pending queries are resolved

sdedic commented on a change in pull request #3382:
URL: https://github.com/apache/netbeans/pull/3382#discussion_r772579567



##########
File path: java/java.lsp.server/vscode/src/explorer.ts
##########
@@ -154,42 +169,131 @@ class VisualizerProvider extends vscode.Disposable implements CustomizableTreeDa
   }
 
   getTreeItem(element: Visualizer): vscode.TreeItem | Thenable<vscode.TreeItem> {
-    const n = Number(element.id);
-    if (this.pendingRefresh.delete(n)) {
-      return this.fetchItem(n).then((newV) => {
-        element.update(newV);
-        return element;
-      });
+    const n : number = Number(element.id);
+    const self = this;
+    if (doLog) {
+      this.log.appendLine(`Doing getTreeItem on ${element.idstring()}`);
+    }
+
+    return this.wrap(async (arr) => {
+      let fetched = await this.queryVisualizer(element, arr, () => this.fetchItem(n));
+      element.update(fetched);
+      return self.getTreeItem2(fetched);
+    });
+  }
+
+  /**
+   * Wraps code that queries individual Visualizers so that blocked changes are fired after
+   * the code terminated.
+   * 
+   * Usage:
+   * wrap(() => { ... code ... ; queryVisualizer(vis, () => { ... })});
+   * @param fn the code to execute
+   * @returns value of the code function
+   */
+  async wrap<X>(fn : (pending : Visualizer[]) => Thenable<X>) : Promise<X> {
+    let arr : Visualizer[] = [];
+    try {
+      return await fn(arr);
+    } finally {
+      this.releaseVisualizersAndFire(arr);
+    }
+  }
+
+  /**
+   * Just creates a string list from visualizer IDs. Diagnostics only.
+   */
+  private visualizerList(arr : Visualizer[]) : string {
+    let s = "";
+    for (let v of arr) {
+      s += v.idstring() + " ";
+    }
+    return s;
+  }
+
+  /**
+   * Do not use directly, use wrap(). Fires delayed events for visualizers that have no pending queries.
+   */
+  private releaseVisualizersAndFire(list : Visualizer[] | undefined) {
+    if (!list) {
+      list = Array.from(this.delayedFire);
+    }
+    if (doLog) {
+      this.log.appendLine(`Done with ${this.visualizerList(list)}`);
+    }
+    // v can be in list several times, each push increased its counter, so we need to decrease it.
+    for (let v of list) {
+      if (this.treeData?.get(Number(v.id || -1)) === v) {
+        if (--v.pendingQueries) {
+          if (doLog) {
+            this.log.appendLine(`${v.idstring()} has pending ${v.pendingQueries} queries`);
+          }
+          continue;
+        }
+        if (v.pendingChange) {
+          if (doLog) {
+            this.log.appendLine(`Fire delayed change on ${v.idstring()}`);
+          }
+          this.fireItemChange(v);
+          v.pendingChange = false;
+        }
+      }
+      this.delayedFire.delete(v);
     }
+    if (doLog) {
+      this.log.appendLine("Pending queue: " + this.visualizerList(Array.from(this.delayedFire)));
+      this.log.appendLine("---------------");
+    }
+  }
+
+  /**
+   * Should wrap calls to NBLS for individual visualizers (info, children). Puts visualizer on the delayed fire list.
+   * Must be itself wrapped in wrap() -- wrap(... queryVisualizer()).
+   * @param element visualizer to be queried, possibly undefined (new item is expected)
+   * @param fn code to execute
+   * @returns code's result
+   */
+  async queryVisualizer<X>(element : Visualizer | undefined, pending : Visualizer[], fn : () => Promise<X>) : Promise<X> {
+    if (!element) {
+      return fn();
+    }
+    this.delayedFire.add(element);
+    pending.push(element);
+    element.pendingQueries++;
+    if (doLog) {
+      this.log.appendLine(`Delaying visualizer ${element.idstring()}, queries = ${element.pendingQueries}`)
+    }
+    return fn();
+  }
+
+  async getTreeItem2(element: Visualizer): Promise<vscode.TreeItem> {
+    const n = Number(element.id);
     if (this.decorators.length == 0) {
-      return element;
+     return element;
     }
     let list : TreeItemDecorator<Visualizer>[] = [...this.decorators];
     
-    function f(item : vscode.TreeItem) : vscode.TreeItem | Thenable<vscode.TreeItem> {
+    async function f(item : vscode.TreeItem) : Promise<vscode.TreeItem> {
       const deco = list.shift();
       if (!deco) {
-        return item;
+       return item;
       }
       const decorated = deco.decorateTreeItem(element, item);
       if (decorated instanceof vscode.TreeItem) {
           return f(decorated);
       } else {
-          return (decorated as Thenable<vscode.TreeItem>).then(f);
+         return (decorated as Thenable<vscode.TreeItem>).then(f);
       }
     }
-
     return f(element.copy());
   }
 
+  delayedFire : Set<Visualizer> = new Set<Visualizer>();
+
   async fetchItem(n : number) : Promise<Visualizer> {
+    let refreshVis = this.treeData.get(n);

Review comment:
       Right it is a leftover. Updates of the existing `Visualizer` instances are done in `fetchItem` callers.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists