mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
feat: async tree plant
This commit is contained in:
parent
a0efbd47db
commit
71e815bdca
1 changed files with 22 additions and 6 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue