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 22:59:19 +00:00
|
|
|
import {S5Error} from "@lumeweb/s5-js/lib/client.js";
|
2024-03-22 22:30:58 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
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());
|
2024-03-22 22:59:19 +00:00
|
|
|
let inProgress = allStatuses.some(status => {
|
|
|
|
PinningProcess.checkStatus(status.id);
|
|
|
|
return status.status !== 'completed';
|
|
|
|
});
|
2024-03-22 03:15:56 +00:00
|
|
|
|
|
|
|
while (inProgress) {
|
|
|
|
yield allStatuses;
|
|
|
|
allStatuses = Array.from(PinningProcess.instances.values());
|
2024-03-22 22:59:19 +00:00
|
|
|
inProgress = allStatuses.some(status => {
|
|
|
|
PinningProcess.checkStatus(status.id);
|
|
|
|
return status.status !== 'completed';
|
|
|
|
});
|
2024-03-22 03:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
yield allStatuses ?? []; // Yield the final statuses
|
2024-03-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 22:59:19 +00:00
|
|
|
private static async checkStatus(id: string) {
|
|
|
|
const s5 = PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk()!;
|
|
|
|
try {
|
|
|
|
const ret = await s5.pinStatus(id);
|
|
|
|
const status = PinningProcess.instances.get(id);
|
|
|
|
if (!status) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status.progress = ret.progress;
|
|
|
|
status.status = ret.status as any;
|
|
|
|
} catch (e) {
|
|
|
|
if ((e as S5Error).statusCode == 404) {
|
|
|
|
PinningProcess.instances.delete(id);
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 22:30:58 +00:00
|
|
|
public static setupSdk(sdk: Sdk) {
|
|
|
|
PinningProcess.sdk = sdk;
|
|
|
|
}
|
|
|
|
}
|