Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add v1 websocket events in local docs preview #3655

Merged
merged 1 commit into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/cli/docs-preview/src/runPreviewServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export async function runPreviewServer({
const wss = new WebSocketServer({ server: httpServer });

const connections = new Set<WebSocket>();
function sendData(data: unknown) {
for (const connection of connections) {
connection.send(JSON.stringify(data));
}
}

wss.on("connection", function connection(ws) {
connections.add(ws);
Expand Down Expand Up @@ -103,18 +108,19 @@ export async function runPreviewServer({
});
watcher.on("all", async (event: string, targetPath: string, _targetPathNext: string) => {
context.logger.info(chalk.dim(`[${event}] ${targetPath}`));
sendData({
version: 1,
type: "startReload"
});
// after the docsDefinition is reloaded, send a message to all connected clients to reload the page
const reloadedDocsDefinition = await reloadDocsDefinition();
if (reloadedDocsDefinition != null) {
docsDefinition = reloadedDocsDefinition;
}
for (const connection of connections) {
connection.send(
JSON.stringify({
reload: true
})
);
}
sendData({
version: 1,
type: "finishReload"
});
});

app.post("/v2/registry/docs/load-with-url", async (_, res) => {
Expand Down
Loading