2024-03-22 22:30:58 +00:00
|
|
|
import {PROTOCOL_S5, Sdk} from "@lumeweb/portal-sdk";
|
|
|
|
import {useSdk} from "~/components/lib/sdk-context.js";
|
|
|
|
import {S5Client} from "@lumeweb/s5-js";
|
|
|
|
|
2024-03-22 03:15:56 +00:00
|
|
|
export interface PinningStatus {
|
2024-03-18 15:07:11 +00:00
|
|
|
id: string;
|
|
|
|
progress: number;
|
2024-03-22 22:30:58 +00:00
|
|
|
status: 'processing' | 'completed';
|
2024-03-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
|
2024-03-21 05:15:12 +00:00
|
|
|
export class PinningProcess {
|
2024-03-18 15:07:11 +00:00
|
|
|
private static instances: Map<string, PinningStatus> = new Map();
|
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
private static sdk?: Sdk;
|
|
|
|
|
2024-03-18 15:07:11 +00:00
|
|
|
static async pin(id: string): Promise<{ success: boolean; message: string }> {
|
2024-03-22 22:30:58 +00:00
|
|
|
const s5 = PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk()!;
|
2024-03-18 15:07:11 +00:00
|
|
|
if (PinningProcess.instances.has(id)) {
|
2024-03-22 22:30:58 +00:00
|
|
|
return {success: false, message: "ID is already being processed"};
|
2024-03-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
const pinningStatus: PinningStatus = {id, progress: 0, status: 'processing'};
|
2024-03-18 15:07:11 +00:00
|
|
|
PinningProcess.instances.set(id, pinningStatus);
|
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
await s5.pin(id);
|
|
|
|
|
2024-03-18 15:07:11 +00:00
|
|
|
(async () => {
|
2024-03-22 22:30:58 +00:00
|
|
|
const ret = await s5.pinStatus(id);
|
|
|
|
pinningStatus.progress = ret.progress;
|
|
|
|
if (ret.status === 'completed') {
|
|
|
|
pinningStatus.status = 'completed';
|
2024-03-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
return {success: true, message: "Pinning process started"};
|
2024-03-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 03:15:56 +00:00
|
|
|
static async unpin(id: string): Promise<{ success: boolean; message: string }> {
|
|
|
|
if (!PinningProcess.instances.has(id)) {
|
2024-03-22 22:30:58 +00:00
|
|
|
return {success: false, message: "ID not found or not being processed"};
|
2024-03-18 15:07:11 +00:00
|
|
|
}
|
2024-03-22 03:15:56 +00:00
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
await PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk().unpin(id);
|
|
|
|
|
|
|
|
return {success: true, message: "Pinning process removed"}
|
2024-03-22 03:15:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
static* pollAllProgress(): Generator<PinningStatus[], void, unknown> {
|
2024-03-22 03:15:56 +00:00
|
|
|
let allStatuses = Array.from(PinningProcess.instances.values());
|
|
|
|
let inProgress = allStatuses.some(status => status.status !== 'completed');
|
|
|
|
|
|
|
|
while (inProgress) {
|
|
|
|
yield allStatuses;
|
|
|
|
allStatuses = Array.from(PinningProcess.instances.values());
|
|
|
|
inProgress = allStatuses.some(status => status.status !== 'completed');
|
|
|
|
}
|
|
|
|
|
|
|
|
yield allStatuses ?? []; // Yield the final statuses
|
2024-03-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
public static setupSdk(sdk: Sdk) {
|
|
|
|
PinningProcess.sdk = sdk;
|
|
|
|
}
|
|
|
|
}
|