feat: initial wiring of the pinning system

This commit is contained in:
Derrick Hammer 2024-03-22 18:30:58 -04:00
parent 9938beddbc
commit c1b4e668e2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 29 additions and 30 deletions

View File

@ -21,7 +21,7 @@ export const PinningNetworkBanner = () => {
const itemsLeft = useMemo( const itemsLeft = useMemo(
() => () =>
data?.items.filter((item: PinningStatus) => data?.items.filter((item: PinningStatus) =>
item.status.includes("inprogress"), item.status.includes("processing"),
) || [], ) || [],
[data], [data],
); );

View File

@ -1,45 +1,52 @@
import {PROTOCOL_S5, Sdk} from "@lumeweb/portal-sdk";
import {useSdk} from "~/components/lib/sdk-context.js";
import {S5Client} from "@lumeweb/s5-js";
export interface PinningStatus { export interface PinningStatus {
id: string; id: string;
progress: number; progress: number;
status: 'inprogress' | 'completed' | 'stale'; status: 'processing' | 'completed';
} }
// biome-ignore lint/complexity/noStaticOnlyClass: <explanation> // biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
export class PinningProcess { export class PinningProcess {
private static instances: Map<string, PinningStatus> = new Map(); private static instances: Map<string, PinningStatus> = new Map();
private static sdk?: Sdk;
static async pin(id: string): Promise<{ success: boolean; message: string }> { static async pin(id: string): Promise<{ success: boolean; message: string }> {
const s5 = PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk()!;
if (PinningProcess.instances.has(id)) { if (PinningProcess.instances.has(id)) {
return { success: false, message: "ID is already being processed" }; return {success: false, message: "ID is already being processed"};
} }
const pinningStatus: PinningStatus = { id, progress: 0, status: 'inprogress' }; const pinningStatus: PinningStatus = {id, progress: 0, status: 'processing'};
PinningProcess.instances.set(id, pinningStatus); PinningProcess.instances.set(id, pinningStatus);
// Simulate async progress await s5.pin(id);
(async () => { (async () => {
for (let progress = 1; progress <= 100; progress++) { const ret = await s5.pinStatus(id);
await new Promise(resolve => setTimeout(resolve, Math.floor(Math.random() * (500 - 100 + 1)) + 100)); // Simulate time passing with random duration between 100 and 500 pinningStatus.progress = ret.progress;
pinningStatus.progress = progress; if (ret.status === 'completed') {
if (progress === 100) { pinningStatus.status = 'completed';
pinningStatus.status = 'completed';
}
} }
})(); })();
return { success: true, message: "Pinning process started" }; return {success: true, message: "Pinning process started"};
} }
static async unpin(id: string): Promise<{ success: boolean; message: string }> { static async unpin(id: string): Promise<{ success: boolean; message: string }> {
if (!PinningProcess.instances.has(id)) { if (!PinningProcess.instances.has(id)) {
return { success: false, message: "ID not found or not being processed" }; return {success: false, message: "ID not found or not being processed"};
} }
PinningProcess.instances.delete(id); await PinningProcess.sdk?.protocols().get<S5Client>(PROTOCOL_S5)!.getSdk().unpin(id);
return { success: true, message: "Pinning process removed" }
return {success: true, message: "Pinning process removed"}
} }
static *pollAllProgress(): Generator<PinningStatus[], void, unknown> { static* pollAllProgress(): Generator<PinningStatus[], void, unknown> {
let allStatuses = Array.from(PinningProcess.instances.values()); let allStatuses = Array.from(PinningProcess.instances.values());
let inProgress = allStatuses.some(status => status.status !== 'completed'); let inProgress = allStatuses.some(status => status.status !== 'completed');
@ -51,18 +58,8 @@ export class PinningProcess {
yield allStatuses ?? []; // Yield the final statuses yield allStatuses ?? []; // Yield the final statuses
} }
}
// Example usage: public static setupSdk(sdk: Sdk) {
// (async () => { PinningProcess.sdk = sdk;
// const { success, message } = await PinningProcess.pin("123"); }
// console.log(message); }
// if (success) {
// const progressGenerator = PinningProcess.pollProgress("123");
// let result = progressGenerator.next();
// while (!result.done) {
// console.log(result.value); // Log the progress
// result = progressGenerator.next();
// }
// }
// })();

View File

@ -21,6 +21,7 @@ import { Sdk } from "@lumeweb/portal-sdk";
import resources from "~/data/resources.js"; import resources from "~/data/resources.js";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useMemo } from "react"; import { useMemo } from "react";
import {PinningProcess} from "~/data/pinning.js";
export const links: LinksFunction = () => [ export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet }, { rel: "stylesheet", href: stylesheet },
@ -50,6 +51,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
function App() { function App() {
const sdk = useSdk(); const sdk = useSdk();
const providers = useMemo(() => getProviders(sdk as Sdk), [sdk]); const providers = useMemo(() => getProviders(sdk as Sdk), [sdk]);
useMemo(() => PinningProcess.setupSdk(sdk as Sdk), [sdk]);
return ( return (
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<Refine <Refine