Skip to content

Commit

Permalink
feat: sidecarsubscriber
Browse files Browse the repository at this point in the history
(bugged: every listitem subscribes to it)
  • Loading branch information
Schlauer-Hax committed Oct 22, 2023
1 parent 5739284 commit 5588b0b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
6 changes: 6 additions & 0 deletions pages/hosting/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export type RemotePath = {
export const currentFiles = asPointer(<RemotePath[]>[]);
export const currentPath = asPointer("/");
export let messageQueueSidecar = <{ request: SidecarRequest, response: Deferred<SidecarResponse>; }[]>[];
export const subscriberSidecar: ([id: string, (message: SidecarResponse) => void])[] = []
let activeSideCar: Deferred<void> | undefined = undefined;
export const isSidecarConnect = asPointer(false);
export function stopSidecarConnection() {
Expand Down Expand Up @@ -156,6 +157,11 @@ export async function startSidecarConnection(id: string) {
break;
}
}
for (const iterator of subscriberSidecar) {
if (iterator[0] === id) {
iterator[1](msg);
}
}
};
ws.onopen = () => {
watcher = setInterval(() => {
Expand Down
38 changes: 12 additions & 26 deletions pages/hosting/views/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { dirname } from "std/path/mod.ts";
import { asPointer, BasicLabel, BIcon, Box, Button, ButtonStyle, Color, Component, Custom, Dialog, DropDownInput, Entry, Form, Grid, IconButton, IconButtonComponent, isMobile, Label, loadingWheel, MediaQuery, MIcon, ref, refMerge, State, StateHandler, TextInput, Vertical } from "webgen/mod.ts";
import locations from "../../../data/locations.json" assert { type: "json" };
import serverTypes from "../../../data/servers.json" assert { type: "json" };
import { AuditTypes, PowerState, Server, ServerDetails } from "../../../spec/music.ts";
import { AuditTypes, PowerState, Server } from "../../../spec/music.ts";
import { activeUser, showProfilePicture } from "../../_legacy/helper.ts";
import { MB, state } from "../data.ts";
import { currentDetailsSource, currentDetailsTarget, currentFiles, currentPath, isSidecarConnect, listFiles, messageQueueSidecar, RemotePath, startSidecarConnection, stopSidecarConnection, streamingPool, uploadFile } from "../loading.ts";
import { currentFiles, currentPath, isSidecarConnect, listFiles, messageQueueSidecar, RemotePath, startSidecarConnection, stopSidecarConnection, streamingPool, subscriberSidecar, uploadFile } from "../loading.ts";
import { profileView } from "../views/profile.ts";
import './details.css';
import { DropHandler } from "./dropHandler.ts";
Expand Down Expand Up @@ -338,30 +338,16 @@ export function serverDetails(server: StateHandler<Server>) {
subtitle: "disk",
});

terminal.connected.listen(val => {
if (val) {
currentDetailsTarget.setValue(server._id);

currentDetailsSource.setValue((data: ServerDetails) => {
if (data.type == "stdout") {
terminal.write(server.identifier ? `${data.chunk}\r\n` : data.chunk.replaceAll("\n", "\r\n"));
if (data.clearConsole)
terminal.reset();
}
else if (data.type == "stats") {
input.cpu = data.cpu;
input.disk = data.disk;
input.memory = data.memory;
}
else if (data.type == "features") {
// TODO remove this when backend sends clearConsole flag
terminal.reset();
}
else console.log("Unhandled Info", data);
});
} else
currentDetailsTarget.setValue(undefined);
});
subscriberSidecar.push([server._id, (data) => {
if (data.type == "log") {
terminal.write(`${data.chunk}\r\n`);
}
else if (data.type == "resources") {
input.cpu = data.cpu;
input.disk = data.used;
input.memory = data.memory;
}
}])

return isSidecarConnect.map((connected) => !server.identifier && !connected ? DisconnectedScreen() : HeavyReRender(isMobile, mobile => {
const items = Grid(
Expand Down

0 comments on commit 5588b0b

Please sign in to comment.