Compare commits

...

2 Commits

Author SHA1 Message Date
Yeicor
6944f69110 Automatically update version to 0.6.18 2024-03-16 15:23:23 +00:00
Yeicor
1d01c75448 more fixes 2024-03-16 16:22:08 +01:00
5 changed files with 34 additions and 43 deletions

View File

@@ -57,7 +57,7 @@ async function onModelUpdateRequest(event: NetworkUpdateEvent) {
} }
if (shutdownRequest !== null) { if (shutdownRequest !== null) {
console.log("Shutting down the connection as requested by the server"); console.log("Shutting down the connection as requested by the server");
event.disconnectForALittleBit(); event.disconnect();
} }
sceneDocument.value = doc sceneDocument.value = doc
triggerRef(sceneDocument); // Why not triggered automatically? triggerRef(sceneDocument); // Why not triggered automatically?

View File

@@ -19,12 +19,12 @@ class NetworkUpdateEventModel {
export class NetworkUpdateEvent extends Event { export class NetworkUpdateEvent extends Event {
models: NetworkUpdateEventModel[]; models: NetworkUpdateEventModel[];
disconnectForALittleBit: () => void; disconnect: () => void;
constructor(models: NetworkUpdateEventModel[], disconnectForALittleBit: () => void) { constructor(models: NetworkUpdateEventModel[], disconnect: () => void) {
super("update"); super("update");
this.models = models; this.models = models;
this.disconnectForALittleBit = disconnectForALittleBit; this.disconnect = disconnect;
} }
} }
@@ -59,45 +59,36 @@ export class NetworkManager extends EventTarget {
} }
} }
private async monitorDevServer(url: URL, pendingTimeout: { id: number } = {id: -1}) { private async monitorDevServer(url: URL, stop: () => boolean = () => false) {
try { while (!stop()) {
// WARNING: This will spam the console logs with failed requests when the server is down try {
const controller = new AbortController(); // WARNING: This will spam the console logs with failed requests when the server is down
let response = await fetch(url.toString(), {signal: controller.signal}); const controller = new AbortController();
// console.log("Monitoring", url.toString(), response); let response = await fetch(url.toString(), {signal: controller.signal});
if (response.status === 200) { // console.log("Monitoring", url.toString(), response);
let lines = readLinesStreamings(response.body!.getReader()); if (response.status === 200) {
for await (let line of lines) { let lines = readLinesStreamings(response.body!.getReader());
if (!line || !line.startsWith("data:")) continue; for await (let line of lines) {
let data: { name: string, hash: string, is_remove: boolean | null } = JSON.parse(line.slice(5)); if (stop()) break;
// console.debug("WebSocket message", data); if (!line || !line.startsWith("data:")) continue;
let urlObj = new URL(url); let data: { name: string, hash: string, is_remove: boolean | null } = JSON.parse(line.slice(5));
urlObj.searchParams.delete("api_updates"); // console.debug("WebSocket message", data);
urlObj.searchParams.set("api_object", data.name); let urlObj = new URL(url);
this.foundModel(data.name, data.hash, urlObj.toString(), data.is_remove, async () => { urlObj.searchParams.delete("api_updates");
console.log("Disconnecting for a little bit"); urlObj.searchParams.set("api_object", data.name);
controller.abort(); this.foundModel(data.name, data.hash, urlObj.toString(), data.is_remove, async () => {
clearTimeout(pendingTimeout.id!); controller.abort(); // Notify the server that we are done
pendingTimeout.id = -2; });
setTimeout(() => { }
console.log("Reconnecting after a little bit");
this.monitorDevServer(url, pendingTimeout)
}, settings.monitorEveryMs * 50);
});
} }
controller.abort();
} catch (e) { // Ignore errors (retry very soon)
} }
} catch (e) { // Ignore errors (retry very soon) await new Promise(resolve => setTimeout(resolve, settings.monitorEveryMs));
} }
if (pendingTimeout.id >= -1) {
pendingTimeout.id = setTimeout(() => {
console.log("Reconnecting fast");
this.monitorDevServer(url, pendingTimeout)
}, settings.monitorEveryMs);
}
return;
} }
private foundModel(name: string, hash: string | null, url: string, isRemove: boolean | null, disconnectForALittleBit: () => void = () => { private foundModel(name: string, hash: string | null, url: string, isRemove: boolean | null, disconnect: () => void = () => {
}) { }) {
let prevHash = this.knownObjectHashes[name]; let prevHash = this.knownObjectHashes[name];
// console.debug("Found model", name, "with hash", hash, "and previous hash", prevHash); // console.debug("Found model", name, "with hash", hash, "and previous hash", prevHash);
@@ -109,7 +100,7 @@ export class NetworkManager extends EventTarget {
if (!(name in this.knownObjectHashes)) return; // Nothing to remove... if (!(name in this.knownObjectHashes)) return; // Nothing to remove...
delete this.knownObjectHashes[name]; delete this.knownObjectHashes[name];
// Also update buffered updates if the model is removed // Also update buffered updates if the model is removed
//this.bufferedUpdates = this.bufferedUpdates.filter(m => m.name !== name); this.bufferedUpdates = this.bufferedUpdates.filter(m => m.name !== name);
} }
let newModel = new NetworkUpdateEventModel(name, url, hash, isRemove); let newModel = new NetworkUpdateEventModel(name, url, hash, isRemove);
this.bufferedUpdates.push(newModel); this.bufferedUpdates.push(newModel);
@@ -117,7 +108,7 @@ export class NetworkManager extends EventTarget {
// Optimization: try to batch updates automatically for faster rendering // Optimization: try to batch updates automatically for faster rendering
if (this.batchTimeout !== null) clearTimeout(this.batchTimeout); if (this.batchTimeout !== null) clearTimeout(this.batchTimeout);
this.batchTimeout = setTimeout(() => { this.batchTimeout = setTimeout(() => {
this.dispatchEvent(new NetworkUpdateEvent(this.bufferedUpdates, disconnectForALittleBit)); this.dispatchEvent(new NetworkUpdateEvent(this.bufferedUpdates, disconnect));
this.bufferedUpdates = []; this.bufferedUpdates = [];
}, batchTimeout); }, batchTimeout);
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "yet-another-cad-viewer", "name": "yet-another-cad-viewer",
"version": "0.6.17", "version": "0.6.18",
"description": "", "description": "",
"license": "MIT", "license": "MIT",
"private": true, "private": true,

View File

@@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "yacv-server" name = "yacv-server"
version = "0.6.17" version = "0.6.18"
description = "Yet Another CAD Viewer (server)" description = "Yet Another CAD Viewer (server)"
authors = ["Yeicor <4929005+Yeicor@users.noreply.github.com>"] authors = ["Yeicor <4929005+Yeicor@users.noreply.github.com>"]
license = "MIT" license = "MIT"

View File

@@ -75,7 +75,7 @@ class HTTPHandler(SimpleHTTPRequestHandler):
with self.yacv.frontend_lock.r_locked(): with self.yacv.frontend_lock.r_locked():
# Avoid accepting new connections while shutting down # Avoid accepting new connections while shutting down
if self.yacv.shutting_down.is_set() and not self.yacv.at_least_one_client.is_set(): if self.yacv.shutting_down.is_set() and self.yacv.at_least_one_client.is_set():
self.send_error(HTTPStatus.SERVICE_UNAVAILABLE, 'Server is shutting down') self.send_error(HTTPStatus.SERVICE_UNAVAILABLE, 'Server is shutting down')
return return
self.yacv.at_least_one_client.set() self.yacv.at_least_one_client.set()