chore: add facades refactor: core module export refactor: inference plugin - deprecate function registering (#537) * refactor: revamp inference plugin as class - deprecate function registering * refactor: monitoring plugin - deprecate service registering (#538) refactor: revamp inference plugin as class - deprecate function registering chore: update import refactor: plugin revamp - model management chore: update build steps and remove experimental plugins refactor: remove pluggable electron chore: add sorting for conversations chore: build plugins for testing chore: consistent plugin directory name chore: docs chore: fix CI chore: update conversation prefix
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { EventName, events } from "@janhq/core";
|
|
|
|
export async function pollDownloadProgress(fileName: string) {
|
|
if (
|
|
typeof window !== "undefined" &&
|
|
typeof (window as any).electronAPI === "undefined"
|
|
) {
|
|
const intervalId = setInterval(() => {
|
|
notifyProgress(fileName, intervalId);
|
|
}, 3000);
|
|
}
|
|
}
|
|
|
|
export async function notifyProgress(
|
|
fileName: string,
|
|
intervalId: NodeJS.Timeout
|
|
): Promise<string> {
|
|
const response = await fetch("/api/v1/downloadProgress", {
|
|
method: "POST",
|
|
body: JSON.stringify({ fileName: fileName }),
|
|
headers: { "Content-Type": "application/json", Authorization: "" },
|
|
});
|
|
|
|
if (!response.ok) {
|
|
events.emit(EventName.OnDownloadError, null);
|
|
clearInterval(intervalId);
|
|
return;
|
|
}
|
|
const json = await response.json();
|
|
if (isEmptyObject(json)) {
|
|
if (!fileName && intervalId) {
|
|
clearInterval(intervalId);
|
|
}
|
|
return Promise.resolve("");
|
|
}
|
|
if (json.success === true) {
|
|
events.emit(EventName.OnDownloadSuccess, json);
|
|
clearInterval(intervalId);
|
|
return Promise.resolve("");
|
|
} else {
|
|
events.emit(EventName.OnDownloadUpdate, json);
|
|
return Promise.resolve(json.fileName);
|
|
}
|
|
}
|
|
|
|
function isEmptyObject(ojb: any): boolean {
|
|
return Object.keys(ojb).length === 0;
|
|
}
|