File size: 4,469 Bytes
0ad74ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
import { type Writable, writable, get } from "svelte/store";
export interface LoadingStatus {
eta: number | null;
status: "pending" | "error" | "complete" | "generating" | "streaming";
queue: boolean;
queue_position: number | null;
queue_size?: number;
fn_index: number;
message?: string | null;
scroll_to_output?: boolean;
show_progress?: "full" | "minimal" | "hidden";
time_limit?: number | null | undefined;
progress?: {
progress: number | null;
index: number | null;
length: number | null;
unit: string | null;
desc: string | null;
}[];
}
export type LoadingStatusCollection = Record<number, LoadingStatus>;
interface LoadingStatusStore {
update: (status: LoadingStatus) => void;
subscribe: Writable<LoadingStatusCollection>["subscribe"];
register: (index: number, inputs: number[], outputs: number[]) => void;
get_status_for_fn: (i: number) => LoadingStatus["status"];
get_inputs_to_update: () => Map<number, string>;
}
export function create_loading_status_store(): LoadingStatusStore {
const store = writable<LoadingStatusCollection>({});
const fn_inputs: Record<number, number[]> = {};
const fn_outputs: Record<number, number[]> = {};
const pending_outputs = new Map<number, number>();
const pending_inputs = new Map<number, number>();
const inputs_to_update = new Map<number, string>();
const fn_status: Record<number, LoadingStatus["status"]> = {};
function update({
fn_index,
status,
queue = true,
size,
position = null,
eta = null,
message = null,
progress,
time_limit = null
}: {
fn_index: LoadingStatus["fn_index"];
status: LoadingStatus["status"];
queue?: LoadingStatus["queue"];
size?: LoadingStatus["queue_size"];
position?: LoadingStatus["queue_position"];
eta?: LoadingStatus["eta"];
message?: LoadingStatus["message"];
progress?: LoadingStatus["progress"];
time_limit?: LoadingStatus["time_limit"];
}): void {
const outputs = fn_outputs[fn_index];
const inputs = fn_inputs[fn_index];
const last_status = fn_status[fn_index];
const outputs_to_update = outputs.map((id) => {
let new_status: LoadingStatus["status"];
const pending_count = pending_outputs.get(id) || 0;
// from (pending -> error) | complete - decrement pending count
if (last_status === "pending" && status !== "pending") {
let new_count = pending_count - 1;
pending_outputs.set(id, new_count < 0 ? 0 : new_count);
new_status = new_count > 0 ? "pending" : status;
// from pending -> pending - do nothing
} else if (last_status === "pending" && status === "pending") {
new_status = "pending";
// (error | complete) -> pending - - increment pending count
} else if (last_status !== "pending" && status === "pending") {
new_status = "pending";
pending_outputs.set(id, pending_count + 1);
} else {
new_status = status;
}
return {
id,
queue_position: position,
queue_size: size,
eta: eta,
status: new_status,
message: message,
progress: progress
};
});
inputs.forEach((id) => {
const pending_count = pending_inputs.get(id) || 0;
// from (pending -> error) | complete - decrement pending count
if (last_status === "pending" && status !== "pending") {
let new_count = pending_count - 1;
pending_inputs.set(id, new_count < 0 ? 0 : new_count);
inputs_to_update.set(id, status);
} else if (last_status !== "pending" && status === "pending") {
pending_inputs.set(id, pending_count + 1);
inputs_to_update.set(id, status);
} else {
inputs_to_update.delete(id);
}
});
store.update((outputs: LoadingStatusCollection) => {
outputs_to_update.forEach(
({
id,
queue_position,
queue_size,
eta,
status,
message,
progress
}) => {
outputs[id] = {
queue: queue,
queue_size: queue_size,
queue_position: queue_position,
eta: eta,
message: message,
progress,
status,
fn_index
};
}
);
return outputs;
});
fn_status[fn_index] = status;
}
function register(index: number, inputs: number[], outputs: number[]): void {
fn_inputs[index] = inputs;
fn_outputs[index] = outputs;
}
return {
update,
register,
subscribe: store.subscribe,
get_status_for_fn(i: number) {
return fn_status[i];
},
get_inputs_to_update() {
return inputs_to_update;
}
};
}
export type LoadingStatusType = ReturnType<typeof create_loading_status_store>;
|