From ef71c1850bb9d8bf63caf484f42bf02f31cacdb4 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Thu, 18 Nov 2021 00:27:37 -0500 Subject: [PATCH] Fixes finding view nodes if view isn't loaded yet --- src/views/viewBase.ts | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/views/viewBase.ts b/src/views/viewBase.ts index 8cc5ef4..05408ac 100644 --- a/src/views/viewBase.ts +++ b/src/views/viewBase.ts @@ -349,26 +349,30 @@ export abstract class ViewBase< ): Promise { const cc = Logger.getCorrelationContext(); - // If we have no root (e.g. never been initialized) force it so the tree will load properly - if (this.root == null) { - await this.show(); + async function find(this: ViewBase) { + try { + const node = await this.findNodeCoreBFS( + typeof predicate === 'string' ? n => n.id === predicate : predicate, + this.ensureRoot(), + allowPaging, + canTraverse, + maxDepth, + token, + ); + + return node; + } catch (ex) { + Logger.error(ex, cc); + return undefined; + } } - try { - const node = await this.findNodeCoreBFS( - typeof predicate === 'string' ? n => n.id === predicate : predicate, - this.ensureRoot(), - allowPaging, - canTraverse, - maxDepth, - token, - ); - - return node; - } catch (ex) { - Logger.error(ex, cc); - return undefined; - } + if (this.root != null) return find.call(this); + + // If we have no root (e.g. never been initialized) force it so the tree will load properly + await this.show(); + // Since we have to show the view, let the callstack unwind before we try to find the node + return new Promise(resolve => setTimeout(() => resolve(find.call(this)), 0)); } private async findNodeCoreBFS(