Skip to content

Commit

Permalink
feat: use promise pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuk1ko committed Aug 16, 2024
1 parent 645530d commit cb8950e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 18 additions & 10 deletions src/workers/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -179,30 +180,37 @@ export class Git {

private async copyDir(parentHandler: FileSystemDirectoryHandle, parentResult: WalkResult, state: { cur: number; total: number }) {
if (!parentResult.children) return;
const tasks: Array<() => Promise<void>> = [];
await Promise.all(
parentResult.children.map(async result => {
try {
if (result.children) {
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) {
console.error(error);
}
}),
);
await PromisePool.withConcurrency(navigator.hardwareConcurrency)
.for(tasks)
.handleError(console.error)
.process(func => func());
}

private async emitUpdateCommits() {
Expand Down

0 comments on commit cb8950e

Please sign in to comment.