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 712e58d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 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
45 changes: 32 additions & 13 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 @@ -177,32 +178,50 @@ export class Git {
return Object.assign(parent, { children: children.filter(({ type, children }) => !(type === 'tree' && !children?.length)) });
}

private async copyDir(parentHandler: FileSystemDirectoryHandle, parentResult: WalkResult, state: { cur: number; total: number }) {
if (!parentResult.children) return;
private async copyDir(handler: FileSystemDirectoryHandle, result: WalkResult, state: { cur: number; total: number }) {
if (!result.children) return;
const tasks = await this.createCopyDirTasks(handler, result, state);
console.log(tasks.length);
await PromisePool.withConcurrency(navigator.hardwareConcurrency)
.for(tasks)
.handleError(console.error)
.process(func => func());
}

private async createCopyDirTasks(
parentHandler: FileSystemDirectoryHandle,
parentResult: WalkResult,
state: { cur: number; total: number },
): Promise<Array<() => Promise<void>>> {
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);
tasks.push(...(await this.createCopyDirTasks(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);
}
}),
);
return tasks;
}

private async emitUpdateCommits() {
Expand Down

0 comments on commit 712e58d

Please sign in to comment.