unreact 0.7.0

A static site generation framework for Rust using Handlebars and Scss.
Documentation
<!-- *** This file is in DEVELOPMENT MODE! *** -->

<script>
    debug(
        "This document is in *development mode*. Document will reload automatically.",
    );

    // Console log a message with a debug flag
    function debug(message) {
        console.debug(
            "%cDEBUG",
            "color: #8F4; background-color: #6826;",
            message,
        );
    }

    /*** WEBSOCKET CODE BELOW ***/

    // Time to wait before attempting to reconnect, in seconds
    const RECONNECT_INTERVAL = 0.5;
    // Maximum allowed attempts to reconnect, before cancelling
    const MAX_RECONNECT_ATTEMPTS = 50;

    // Websocket handler
    let ws;
    connect();

    // Last time client was loaded, in seconds
    let last_client_load = Date.now() / 1000;

    // Try to connect to websockets
    function connect() {
        // If websockets already is initialized, close
        if (ws) {
            ws.close();
        }

        // Start websockets
        debug("WS: Connecting...");
        ws = new WebSocket("ws://localhost:{{PORT}}");

        // Open and close events
        ws.onopen = event => {
            debug("WS: Websocket open");
            // Stop trying to reconnect
            cancel_reconnect();
            // Clear reconnect attempt counter
            recent_reconnect_attempts = 0;
        };
        ws.onclose = error => {
            debug("WS: Websocket closed");
            // Start trying to reconnect
            reconnect();
        };

        // Websocket error (Usually server currently reloading, or crashed)
        ws.onerror = error => {
            console.error("[debug] WS: Failed to connect!");
            // Don't start trying to reconnect, this is handled by `ws.onclose`
        };

        // Message received from server
        ws.onmessage = event => {
            if (event.data === "reload") {
                // Reload request
                reload();
            } else {
                // Inform of last server start
                let last_server_load = parseInt(event.data);
                // If server is newer than the last client load, reload page
                if (last_server_load > last_client_load) {
                    reload();
                }
            }
        };
    }

    // Try to connect websockets at interval
    let reconnect_interval;
    let recent_reconnect_attempts = 0;
    function reconnect() {
        cancel_reconnect();
        reconnect_interval = setInterval(() => {
            // Increase attempt counter
            recent_reconnect_attempts += 1;
            // Check max attempts
            if (recent_reconnect_attempts > MAX_RECONNECT_ATTEMPTS) {
                // Stop attempting to reconnect
                console.warn(
                    "[debug] Maximum websocket reconnect attempts met. This page will no longer reload automatically.",
                );
                cancel_reconnect();
                return;
            }

            // Connect again
            connect();
        }, RECONNECT_INTERVAL * 1000);
    }
    // Stop trying to reconnect websockets, reset attempt counter
    function cancel_reconnect() {
        clearInterval(reconnect_interval);
    }

    // Reload page (due to server change)
    function reload() {
        debug("Reloading page!");
        location.reload();
    }
</script>