Skip to content

Commit

Permalink
wip: promise-based waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Apr 21, 2023
1 parent 7b9a611 commit 1165c26
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions packages/remix-dev/devServer_unstable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import prettyMs from "pretty-ms";
import execa from "execa";
import express from "express";

import * as Channel from "../channel";
import { type Manifest } from "../manifest";
import * as Compiler from "../compiler";
import { type RemixConfig } from "../config";
Expand All @@ -26,8 +27,8 @@ export let serve = async (
let websocket = Socket.serve({ port: options.websocketPort });

let state: {
appServerBuildHash?: string;
latestBuildHash?: string;
buildHashChannel?: Channel.Type<void>;
appServer?: execa.ExecaChildProcess;
prevManifest?: Manifest;
} = {};
Expand Down Expand Up @@ -74,6 +75,7 @@ export let serve = async (
websocket.log(`Rebuilt in ${prettyMs(durationMs)}`);

state.latestBuildHash = manifest.version;
state.buildHashChannel = Channel.create();
console.log(`Waiting (${state.latestBuildHash})`);
if (options.restart) {
console.log(`restarting: ${options.command}`);
Expand All @@ -82,15 +84,15 @@ export let serve = async (
state.appServer = startAppServer(options.command);
}
}
await wait(() => state.appServerBuildHash === state.latestBuildHash);

console.log("fasdfasd");
await state.buildHashChannel.result;

if (manifest.hmr && state.prevManifest) {
let updates = HMR.updates(config, manifest, state.prevManifest);
websocket.hmr(manifest, updates);
console.log("> HMR");
} else {
websocket.reload();
console.log("> Reload");
}
state.prevManifest = manifest;
},
Expand All @@ -107,10 +109,12 @@ export let serve = async (
.use(express.json())
.post("/ping", (req, res) => {
let { buildHash } = req.body;
if (typeof buildHash === "string") {
state.appServerBuildHash = buildHash;
} else {
if (typeof buildHash !== "string") {
console.warn(`Unrecognized payload: ${req.body}`);
res.sendStatus(400);
}
if (buildHash === state.latestBuildHash) {
state.buildHashChannel?.ok();
}
res.sendStatus(200);
})
Expand All @@ -133,12 +137,3 @@ let clean = (config: RemixConfig) => {
};

let relativePath = (file: string) => path.relative(process.cwd(), file);

let sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

let wait = async (condition: () => boolean) => {
while (!condition()) {
console.log("sleep...");
await sleep(2500);
}
};

0 comments on commit 1165c26

Please sign in to comment.