diff --git a/bun.lockb b/bun.lockb index 0e291b7..7748461 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index a56b56a..8d15492 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "dependencies": { "@isomorphic-git/lightning-fs": "^4.6.0", + "@supercharge/promise-pool": "^3.2.0", "@vueuse/core": "^11.0.0", "buffer": "^6.0.3", "isomorphic-git": "^1.27.1", diff --git a/src/workers/git.ts b/src/workers/git.ts index ef83d33..77c2d59 100644 --- a/src/workers/git.ts +++ b/src/workers/git.ts @@ -2,6 +2,7 @@ import LightningFS, { type PromisifiedFS } from '@isomorphic-git/lightning-fs'; import http from 'isomorphic-git/http/web'; import git, { TREE, WORKDIR, type WalkerEntry } from 'isomorphic-git'; import { Buffer } from 'buffer/'; +import { PromisePool } from '@supercharge/promise-pool'; (globalThis as any).Buffer = Buffer; @@ -179,6 +180,7 @@ export class Git { private async copyDir(parentHandler: FileSystemDirectoryHandle, parentResult: WalkResult, state: { cur: number; total: number }) { if (!parentResult.children) return; + const tasks: Array<() => Promise> = []; await Promise.all( parentResult.children.map(async result => { try { @@ -186,16 +188,18 @@ export class Git { const dirHandler = await parentHandler.getDirectoryHandle(result.name, { create: true }); await this.copyDir(dirHandler, result, state); } else { - const content = await result.entry.content(); - if (!content) return; - const fileHandler = await parentHandler.getFileHandle(result.name, { create: true }); - const writable = await fileHandler.createWritable(); - await writable.write(content); - await writable.close(); - state.cur++; - this.onProgress({ - value: state.total === 0 ? 1 : state.cur / state.total, - desc: `Write (${state.cur}/${state.total}): ${result.path}`, + tasks.push(async () => { + const content = await result.entry.content(); + if (!content) return; + const fileHandler = await parentHandler.getFileHandle(result.name, { create: true }); + const writable = await fileHandler.createWritable(); + await writable.write(content); + await writable.close(); + state.cur++; + this.onProgress({ + value: state.total === 0 ? 1 : state.cur / state.total, + desc: `Write (${state.cur}/${state.total}): ${result.path}`, + }); }); } } catch (error) { @@ -203,6 +207,10 @@ export class Git { } }), ); + await PromisePool.withConcurrency(navigator.hardwareConcurrency) + .for(tasks) + .handleError(console.error) + .process(func => func()); } private async emitUpdateCommits() {