chore: refactor dev client (#2002)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
let ws: WebSocket;
|
let ws: WebSocket;
|
||||||
|
let revision = 0;
|
||||||
|
|
||||||
let reconnectTimer: number;
|
let reconnectTimer: number;
|
||||||
const backoff = [
|
const backoff = [
|
||||||
@@ -37,7 +38,7 @@ function reconnect() {
|
|||||||
backoffIdx++;
|
backoffIdx++;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
connect(true);
|
connect();
|
||||||
clearTimeout(reconnectTimer);
|
clearTimeout(reconnectTimer);
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
reconnect();
|
reconnect();
|
||||||
@@ -45,28 +46,19 @@ function reconnect() {
|
|||||||
}, backoff[Math.min(backoffIdx, backoff.length - 1)]);
|
}, backoff[Math.min(backoffIdx, backoff.length - 1)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function connect(forceReload?: boolean) {
|
function connect() {
|
||||||
const url = new URL("/_frsh/alive", location.origin.replace("http", "ws"));
|
const url = new URL("/_frsh/alive", location.origin.replace("http", "ws"));
|
||||||
ws = new WebSocket(
|
ws = new WebSocket(
|
||||||
url,
|
url,
|
||||||
);
|
);
|
||||||
|
|
||||||
addEventListener("unload", () => {
|
|
||||||
// Prevent form submits from triggering a reconnect
|
|
||||||
clearTimeout(reconnectTimer);
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.addEventListener("open", () => {
|
ws.addEventListener("open", () => {
|
||||||
if (forceReload) {
|
backoffIdx = 0;
|
||||||
location.reload();
|
console.log(
|
||||||
} else {
|
`%c Fresh %c Connected to development server.`,
|
||||||
backoffIdx = 0;
|
"background-color: #86efac; color: black",
|
||||||
console.log(
|
"color: inherit",
|
||||||
`%c Fresh %c Connected to development server.`,
|
);
|
||||||
"background-color: #86efac; color: black",
|
|
||||||
"color: inherit",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.addEventListener("close", () => {
|
ws.addEventListener("close", () => {
|
||||||
@@ -81,7 +73,16 @@ connect();
|
|||||||
|
|
||||||
function handleMessage(e: MessageEvent) {
|
function handleMessage(e: MessageEvent) {
|
||||||
const data = JSON.parse(e.data);
|
const data = JSON.parse(e.data);
|
||||||
console.log(data);
|
switch (data.type) {
|
||||||
|
case "initial-state": {
|
||||||
|
if (revision === 0) {
|
||||||
|
revision = data.revision;
|
||||||
|
} else if (revision < data.revision) {
|
||||||
|
// Needs reload
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleError(e: Event) {
|
function handleError(e: Event) {
|
||||||
|
|||||||
+18
-1
@@ -98,6 +98,7 @@ export class ServerContext {
|
|||||||
#state: InternalFreshState;
|
#state: InternalFreshState;
|
||||||
#extractResult: FsExtractResult;
|
#extractResult: FsExtractResult;
|
||||||
#dev: boolean;
|
#dev: boolean;
|
||||||
|
#revision = 0;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
state: InternalFreshState,
|
state: InternalFreshState,
|
||||||
@@ -150,6 +151,13 @@ export class ServerContext {
|
|||||||
const isDev = this.#dev;
|
const isDev = this.#dev;
|
||||||
const bundleAssetRoute = this.#bundleAssetRoute();
|
const bundleAssetRoute = this.#bundleAssetRoute();
|
||||||
|
|
||||||
|
if (this.#dev) {
|
||||||
|
this.#revision = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
// deno-lint-ignore no-this-alias
|
||||||
|
const _self = this;
|
||||||
|
|
||||||
return async function handler(
|
return async function handler(
|
||||||
req: Request,
|
req: Request,
|
||||||
connInfo: ServeHandlerInfo = DEFAULT_CONN_INFO,
|
connInfo: ServeHandlerInfo = DEFAULT_CONN_INFO,
|
||||||
@@ -168,7 +176,16 @@ export class ServerContext {
|
|||||||
// the client to know when the server is back up. Once we
|
// the client to know when the server is back up. Once we
|
||||||
// have HMR we'll actively start sending messages back
|
// have HMR we'll actively start sending messages back
|
||||||
// and forth.
|
// and forth.
|
||||||
const { response } = Deno.upgradeWebSocket(req);
|
const { response, socket } = Deno.upgradeWebSocket(req);
|
||||||
|
|
||||||
|
socket.addEventListener("open", () => {
|
||||||
|
socket.send(
|
||||||
|
JSON.stringify({
|
||||||
|
type: "initial-state",
|
||||||
|
revision: _self.#revision,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
} else if (url.pathname === DEV_CLIENT_URL) {
|
} else if (url.pathname === DEV_CLIENT_URL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user