Files
drop/composables/task.ts
DecDuck 63ac2b8ffc Depot API & v4 (#298)
* feat: nginx + torrential basics & services system

* fix: lint + i18n

* fix: update torrential to remove openssl

* feat: add torrential to Docker build

* feat: move to self hosted runner

* fix: move off self-hosted runner

* fix: update nginx.conf

* feat: torrential cache invalidation

* fix: update torrential for cache invalidation

* feat: integrity check task

* fix: lint

* feat: move to version ids

* fix: client fixes and client-side checks

* feat: new depot apis and version id fixes

* feat: update torrential

* feat: droplet bump and remove unsafe update functions

* fix: lint

* feat: v4 featureset: emulators, multi-launch commands

* fix: lint

* fix: mobile ui for game editor

* feat: launch options

* fix: lint

* fix: remove axios, use $fetch

* feat: metadata and task api improvements

* feat: task actions

* fix: slight styling issue

* feat: fix style and lints

* feat: totp backend routes

* feat: oidc groups

* fix: update drop-base

* feat: creation of passkeys & totp

* feat: totp signin

* feat: webauthn mfa/signin

* feat: launch selecting ui

* fix: manually running tasks

* feat: update add company game modal to use new SelectorGame

* feat: executor selector

* fix(docker): update rust to rust nightly for torrential build (#305)

* feat: new version ui

* feat: move package lookup to build time to allow for deno dev

* fix: lint

* feat: localisation cleanup

* feat: apply localisation cleanup

* feat: potential i18n refactor logic

* feat: remove args from commands

* fix: lint

* fix: lockfile

---------

Co-authored-by: Aden Lindsay <140392385+AdenMGB@users.noreply.github.com>
2026-01-13 15:32:39 +11:00

80 lines
2.3 KiB
TypeScript

import type { TaskMessage } from "~/server/internal/tasks";
import { WebSocketHandler } from "./ws";
const websocketHandler = new WebSocketHandler("/api/v1/task");
// const taskStates: { [key: string]: } = {};
const taskStates = new Map<string, Ref<TaskMessage | undefined>>();
function handleUpdateMessage(msg: TaskMessage) {
const taskStates = useTaskStates();
const state = taskStates.get(msg.id);
if (!state) return;
if (!state.value || msg.reset) {
state.value = msg;
return;
}
state.value.log.push(...msg.log);
Object.assign(state.value, { ...msg, log: state.value.log });
}
websocketHandler.listen((message) => {
try {
// If it's an object, it's an update message
const msg = JSON.parse(message) as TaskMessage;
handleUpdateMessage(msg);
} catch {
// Otherwise it's control message
const taskStates = useTaskStates();
const [action, ...data] = message.split("/");
switch (action) {
case "connect": {
const taskReady = useTaskReady();
taskReady.value = true;
break;
}
case "disconnect": {
const disconnectTaskId = data[0];
taskStates.delete(disconnectTaskId);
console.log(`disconnected from ${disconnectTaskId}`);
break;
}
case "error": {
const [taskId, title, description] = data;
const state = taskStates.get(taskId);
if (!state) break;
state.value ??= {
id: taskId,
name: "Unknown task",
success: false,
progress: 0,
error: undefined,
log: [],
actions: [],
};
state.value.error = { title, description };
break;
}
}
}
});
const useTaskStates = () => taskStates;
export const useTaskReady = () => useState("taskready", () => false);
export const useTask = (taskId: string): Ref<TaskMessage | undefined> => {
if (import.meta.server) return ref(undefined);
const taskStates = useTaskStates();
const task = taskStates.get(taskId);
if (task && task.value && !task.value.error) return task;
taskStates.set(taskId, ref(undefined));
console.log("connecting to " + taskId);
websocketHandler.send(`connect/${taskId}`);
// TODO: this may have changed behavior
return taskStates.get(taskId) ?? ref(undefined);
};