Skip to content

Commit 3f84d03

Browse files
Incremental update code refinements
1 parent b523966 commit 3f84d03

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

public/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ export function open({hash} = {}) {
111111
break;
112112
case "update": {
113113
const root = document.querySelector("main");
114-
if (root.children.length !== message.length) {
114+
if (message.previousHash !== hash) {
115115
console.log("contents out of sync");
116116
location.reload();
117117
break;
118118
}
119+
hash = message.updatedHash;
119120
message.diff.forEach(({type, newPos, items}) => {
120121
switch (type) {
121122
case "add":

src/markdown.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import {join} from "path";
1313
import {canReadSync} from "./files.js";
1414
import {transpileJavaScript, type FileReference, type ImportReference, type Transpile} from "./javascript.js";
1515
import {computeHash} from "./hash.js";
16+
import {readFile} from "fs/promises";
17+
18+
export interface ReadMarkdownResult {
19+
contents: string;
20+
parse: ParseResult;
21+
hash: string;
22+
}
1623

1724
export interface HtmlPiece {
1825
type: "html";
@@ -423,8 +430,13 @@ function getCellsPatch(prevCells: CellPiece[], nextCells: CellPiece[]): Patch<Pa
423430
);
424431
}
425432

426-
export function diffMarkdown(prevParse: ParseResult, nextParse: ParseResult) {
433+
export function diffMarkdown({parse: prevParse}: ReadMarkdownResult, {parse: nextParse}: ReadMarkdownResult) {
427434
return getPatch<ParsePiece>(prevParse.pieces, nextParse.pieces, equal)
428435
.concat(getCellsPatch(prevParse.cells, nextParse.cells))
429436
.map(diffReducer);
430437
}
438+
439+
export async function readMarkdown(path: string, root: string): Promise<ReadMarkdownResult> {
440+
const contents = await readFile(path, "utf-8");
441+
return {contents, parse: parseMarkdown(contents, root), hash: computeHash(contents)};
442+
}

src/preview.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type {WatchEventType} from "node:fs";
12
import {watch, type FSWatcher} from "node:fs";
23
import {access, constants, readFile, stat} from "node:fs/promises";
34
import {createServer, type IncomingMessage, type RequestListener} from "node:http";
@@ -7,9 +8,8 @@ import {parseArgs} from "node:util";
78
import send from "send";
89
import {WebSocketServer, type WebSocket} from "ws";
910
import {HttpError, isHttpError, isNodeError} from "./error.js";
10-
import {computeHash} from "./hash.js";
1111
import type {ParseResult} from "./markdown.js";
12-
import {diffMarkdown, parseMarkdown} from "./markdown.js";
12+
import {diffMarkdown, readMarkdown} from "./markdown.js";
1313
import {readPages} from "./navigation.js";
1414
import {renderPreview} from "./render.js";
1515

@@ -135,19 +135,19 @@ function handleWatch(socket: WebSocket, root: string) {
135135
});
136136
}
137137

138-
async function readMarkdown(p: string, r: string) {
139-
const contents = await readFile(p, "utf-8");
140-
return {contents, parse: parseMarkdown(contents, r), hash: computeHash(contents)};
141-
}
142-
143138
async function refreshMarkdown(path: string) {
144139
let current = await readMarkdown(path, root);
145140
attachmentWatcher = new FileWatchers(root, current.parse.files, refreshAttachment(current.parse));
146-
return async () => {
147-
// TODO: Sometimes this gets an ENOENT as a transitional state.
141+
return async (event: WatchEventType) => {
142+
if (event !== "change") return; // TODO: decide how to handle a "rename" event
148143
const updated = await readMarkdown(path, root);
149144
if (current.hash !== updated.hash) {
150-
send({type: "update", length: current.parse.pieces.length, diff: diffMarkdown(current.parse, updated.parse)});
145+
send({
146+
type: "update",
147+
diff: diffMarkdown(current, updated),
148+
previousHash: current.hash,
149+
updatedHash: updated.hash
150+
});
151151
attachmentWatcher?.close();
152152
attachmentWatcher = new FileWatchers(root, updated.parse.files, refreshAttachment(updated.parse));
153153
current = updated;

0 commit comments

Comments
 (0)