codex-marketplace-ui-compon.../stories/sdk.ts

74 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-09-13 18:49:40 +00:00
import { CodexData, UploadResponse } from "@codex-storage/sdk-js";
class CodexDataMock extends CodexData {
override upload(
_: File,
onProgress?: (loaded: number, total: number) => void
): Promise<UploadResponse> {
return new Promise<UploadResponse>((resolve) => {
let timeout: number;
resolve({
abort: () => {
window.clearInterval(timeout);
},
result: new Promise((resolve) => {
let count = 0;
timeout = window.setInterval(() => {
count++;
onProgress?.(500 * count, 1500);
if (count === 3) {
window.clearInterval(timeout);
resolve({
error: false,
data: Date.now().toString(),
});
}
}, 1500);
}),
});
});
}
}
export const CodexDataSdk = new CodexDataMock("");
class CodexDataSlowMock extends CodexData {
override upload(
_: File,
onProgress?: (loaded: number, total: number) => void
): Promise<UploadResponse> {
return new Promise<UploadResponse>((resolve) => {
let timeout: number;
resolve({
abort: () => {
window.clearInterval(timeout);
},
result: new Promise((resolve) => {
let count = 0;
timeout = window.setInterval(() => {
count++;
onProgress?.(500 * count, 1500);
if (count === 3) {
window.clearInterval(timeout);
resolve({
error: false,
data: Date.now().toString(),
});
}
}, 1500);
}),
});
});
}
}
export const CodexDataSlowSdk = new CodexDataSlowMock("");