chore(dev): improve bulk change handling (#2234)
Fixed abandoned build processes sometimes still triggering a start (causing double-starts) made it more robust in regards to webui changes being intermixed: making the fetch a fire-and-forget to avoid errors, and waiting for the websocket connection to be re-established to avoid the browser attempting to reload when the server may not be up for a few seconds. Reviewed-on: OpenWF/SpaceNinjaServer#2234 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									b6f79c1e5c
								
							
						
					
					
						commit
						6cdd103c3d
					
				@ -28,8 +28,12 @@ function run(changedFile) {
 | 
				
			|||||||
        runproc = undefined;
 | 
					        runproc = undefined;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    buildproc = spawn("npm", ["run", "build:dev"], { stdio: "inherit", shell: true });
 | 
					    const thisbuildproc = spawn("npm", ["run", "build:dev"], { stdio: "inherit", shell: true });
 | 
				
			||||||
 | 
					    buildproc = thisbuildproc;
 | 
				
			||||||
    buildproc.on("exit", code => {
 | 
					    buildproc.on("exit", code => {
 | 
				
			||||||
 | 
					        if (buildproc !== thisbuildproc) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        buildproc = undefined;
 | 
					        buildproc = undefined;
 | 
				
			||||||
        if (code === 0) {
 | 
					        if (code === 0) {
 | 
				
			||||||
            runproc = spawn("npm", ["run", "start", "--", ...args], { stdio: "inherit", shell: true });
 | 
					            runproc = spawn("npm", ["run", "start", "--", ...args], { stdio: "inherit", shell: true });
 | 
				
			||||||
@ -44,6 +48,8 @@ run();
 | 
				
			|||||||
chokidar.watch("src").on("change", run);
 | 
					chokidar.watch("src").on("change", run);
 | 
				
			||||||
chokidar.watch("static/fixed_responses").on("change", run);
 | 
					chokidar.watch("static/fixed_responses").on("change", run);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
chokidar.watch("static/webui").on("change", () => {
 | 
					chokidar.watch("static/webui").on("change", async () => {
 | 
				
			||||||
    fetch("http://localhost/custom/webuiFileChangeDetected?secret=" + secret);
 | 
					    try {
 | 
				
			||||||
 | 
					        await fetch("http://localhost/custom/webuiFileChangeDetected?secret=" + secret);
 | 
				
			||||||
 | 
					    } catch (e) {}
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -1,11 +1,10 @@
 | 
				
			|||||||
import { args } from "@/src/helpers/commandLineArguments";
 | 
					import { args } from "@/src/helpers/commandLineArguments";
 | 
				
			||||||
import { config } from "@/src/services/configService";
 | 
					 | 
				
			||||||
import { sendWsBroadcast } from "@/src/services/webService";
 | 
					import { sendWsBroadcast } from "@/src/services/webService";
 | 
				
			||||||
import { RequestHandler } from "express";
 | 
					import { RequestHandler } from "express";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const webuiFileChangeDetectedController: RequestHandler = (req, res) => {
 | 
					export const webuiFileChangeDetectedController: RequestHandler = (req, res) => {
 | 
				
			||||||
    if (args.dev && args.secret && req.query.secret == args.secret) {
 | 
					    if (args.dev && args.secret && req.query.secret == args.secret) {
 | 
				
			||||||
        sendWsBroadcast({ ports: { http: config.httpPort, https: config.httpsPort } });
 | 
					        sendWsBroadcast({ reload: true });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    res.end();
 | 
					    res.end();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -110,6 +110,7 @@ interface IWsMsgFromClient {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface IWsMsgToClient {
 | 
					interface IWsMsgToClient {
 | 
				
			||||||
 | 
					    reload?: boolean;
 | 
				
			||||||
    ports?: {
 | 
					    ports?: {
 | 
				
			||||||
        http: number | undefined;
 | 
					        http: number | undefined;
 | 
				
			||||||
        https: number | undefined;
 | 
					        https: number | undefined;
 | 
				
			||||||
 | 
				
			|||||||
@ -9,9 +9,10 @@
 | 
				
			|||||||
/* eslint-disable @typescript-eslint/explicit-function-return-type */
 | 
					/* eslint-disable @typescript-eslint/explicit-function-return-type */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let auth_pending = false,
 | 
					let auth_pending = false,
 | 
				
			||||||
    did_initial_auth = false;
 | 
					    did_initial_auth = false,
 | 
				
			||||||
 | 
					    ws_is_open = false;
 | 
				
			||||||
const sendAuth = isRegister => {
 | 
					const sendAuth = isRegister => {
 | 
				
			||||||
    if (localStorage.getItem("email") && localStorage.getItem("password")) {
 | 
					    if (ws_is_open && localStorage.getItem("email") && localStorage.getItem("password")) {
 | 
				
			||||||
        auth_pending = true;
 | 
					        auth_pending = true;
 | 
				
			||||||
        window.ws.send(
 | 
					        window.ws.send(
 | 
				
			||||||
            JSON.stringify({
 | 
					            JSON.stringify({
 | 
				
			||||||
@ -28,10 +29,18 @@ const sendAuth = isRegister => {
 | 
				
			|||||||
function openWebSocket() {
 | 
					function openWebSocket() {
 | 
				
			||||||
    window.ws = new WebSocket("/custom/ws");
 | 
					    window.ws = new WebSocket("/custom/ws");
 | 
				
			||||||
    window.ws.onopen = () => {
 | 
					    window.ws.onopen = () => {
 | 
				
			||||||
 | 
					        ws_is_open = true;
 | 
				
			||||||
        sendAuth(false);
 | 
					        sendAuth(false);
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    window.ws.onmessage = e => {
 | 
					    window.ws.onmessage = e => {
 | 
				
			||||||
        const msg = JSON.parse(e.data);
 | 
					        const msg = JSON.parse(e.data);
 | 
				
			||||||
 | 
					        if ("reload" in msg) {
 | 
				
			||||||
 | 
					            setTimeout(() => {
 | 
				
			||||||
 | 
					                getWebSocket().then(() => {
 | 
				
			||||||
 | 
					                    location.reload();
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					            }, 100);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        if ("ports" in msg) {
 | 
					        if ("ports" in msg) {
 | 
				
			||||||
            location.port = location.protocol == "https:" ? msg.ports.https : msg.ports.http;
 | 
					            location.port = location.protocol == "https:" ? msg.ports.https : msg.ports.http;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -72,7 +81,7 @@ function openWebSocket() {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    window.ws.onclose = function () {
 | 
					    window.ws.onclose = function () {
 | 
				
			||||||
        window.ws = undefined;
 | 
					        ws_is_open = false;
 | 
				
			||||||
        setTimeout(openWebSocket, 3000);
 | 
					        setTimeout(openWebSocket, 3000);
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -82,7 +91,7 @@ function getWebSocket() {
 | 
				
			|||||||
    return new Promise(resolve => {
 | 
					    return new Promise(resolve => {
 | 
				
			||||||
        let interval;
 | 
					        let interval;
 | 
				
			||||||
        interval = setInterval(() => {
 | 
					        interval = setInterval(() => {
 | 
				
			||||||
            if (window.ws) {
 | 
					            if (ws_is_open) {
 | 
				
			||||||
                clearInterval(interval);
 | 
					                clearInterval(interval);
 | 
				
			||||||
                resolve(window.ws);
 | 
					                resolve(window.ws);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -117,7 +126,7 @@ function logout() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
function doLogout() {
 | 
					function doLogout() {
 | 
				
			||||||
    logout();
 | 
					    logout();
 | 
				
			||||||
    if (window.ws) {
 | 
					    if (ws_is_open) {
 | 
				
			||||||
        // Unsubscribe from notifications about nonce invalidation
 | 
					        // Unsubscribe from notifications about nonce invalidation
 | 
				
			||||||
        window.ws.send(JSON.stringify({ logout: true }));
 | 
					        window.ws.send(JSON.stringify({ logout: true }));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user