From 71e815bdca2fdfa70fe2d1321a06b6b3d307ecb7 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sun, 5 Aug 2018 18:26:40 +0800 Subject: [PATCH] feat: async tree plant --- src/utils/visibleNodesGenerator.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/utils/visibleNodesGenerator.js b/src/utils/visibleNodesGenerator.js index 06a400c..3f0526c 100644 --- a/src/utils/visibleNodesGenerator.js +++ b/src/utils/visibleNodesGenerator.js @@ -53,11 +53,27 @@ function debounce(func, delay) { export const debouncedSearch = debounce(search, 250) -function getNodes(root) { - if (!root.contents) return [] - return [].concat( - ...root.contents.map(node => [node, ...getNodes(node)]) - ) +function runIdle(func) { + return new Promise(resolve => { + window.requestIdleCallback(async () => resolve(await func())) + }) +} + +function promisifyAsyncFunc(func) { + return (...args) => new Promise(async resolve => resolve(await func(...args))) +} + +async function asyncMap(arr, asyncCallback) { + return await Promise.all(arr.map(promisifyAsyncFunc(asyncCallback))) +} + +async function getNodes(root) { + return await runIdle(async () => { + if (!root.contents) return [] + return [].concat( + ...await asyncMap(root.contents, async node => [node, ...await getNodes(node)]) + ) + }) } function compressTree(root, prefix = []) { @@ -92,7 +108,7 @@ export default class VisibleNodesGenerator { async plantTree(root) { this.root = root - this.nodes = getNodes(root) + this.nodes = await getNodes(root) this.compressedRoot = compressTree(root) await this.search()