Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add atomic file writing functionality and enhance project file handling

konstantin-paulus8346fcdcb249
diff --git a/apps/desktop/src/atomic.ts b/apps/desktop/src/atomic.ts
new file mode 100644
index 00000000..8f43265a
--- /dev/null
+++ b/apps/desktop/src/atomic.ts
@@ -0,0 +1,49 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Writes that land whole. Anything may be reading a project's files while the
+// app writes them — a compile, the user's editor, the watcher reading a file
+// back to see whether it really changed — and a reader that catches a file
+// halfway through a write sees source no one wrote. So a write goes to a temp
+// file beside its target and is renamed over it, which the filesystem does in
+// one step.
+
+import { rename, unlink, writeFile } from "node:fs/promises";
+import { basename, dirname, join } from "node:path";
+
+/**
+ * The prefix every temp file the app writes carries. Recognisable rather than
+ * unique, so the project watcher can ignore the whole class of them without
+ * anyone having to declare each one as it is written.
+ */
+export const TEMP_PREFIX = ".dstmp-";
+
+/** Whether `path` — of any form — names one of our temp files. */
+export const isTempPath = (path: string): boolean => basename(path).startsWith(TEMP_PREFIX);
+
+let sequence = 0;
+
+/**
+ * A free path beside `path` to write at until the content is whole. Beside it
+ * rather than in the system's temp folder, because a rename is only atomic
+ * within a filesystem.
+ */
+export function tempPathFor(path: string): string {
+ return join(dirname(path), `${TEMP_PREFIX}${basename(path)}.${process.pid}-${++sequence}`);
+}
+
+/**
+ * Writes `text` to `path` as one step: a temp file beside it, renamed over it.
+ * A failed write takes its temp file with it.
+ */
+export async function writeFileAtomic(path: string, text: string): Promise<void> {
+ const temp = tempPathFor(path);
+ try {
+ await writeFile(temp, text, "utf8");
+ await rename(temp, path);
+ } catch (error) {
+ await unlink(temp).catch(() => { });
+ throw error;
+ }
+}
diff --git a/apps/desktop/src/edit-types.ts b/apps/desktop/src/edit-types.ts
index 1523cc57..535df7d8 100644
--- a/apps/desktop/src/edit-types.ts
+++ b/apps/desktop/src/edit-types.ts
@@ -20,8 +20,12 @@ export type EditValue = PropValue | SerializedAssetRef;
export interface SourceContext {
/** Absolute path of the project folder. */
dir: string;
- /** Called with the project-relative path of every file written. */
- onWrite?: (file: string) => void;
+ /**
+ * Called with the project-relative path of every file about to be written
+ * and the exact text it is about to hold, so that whatever is watching the
+ * folder can tell the write apart from someone else's.
+ */
+ onWrite?: (file: string, text: string) => void;
}
/**
diff --git a/apps/desktop/src/edit.ts b/apps/desktop/src/edit.ts
index 16a830cf..bfad54b9 100644
--- a/apps/desktop/src/edit.ts
+++ b/apps/desktop/src/edit.ts
@@ -4,13 +4,15 @@
import { randomInt } from "node:crypto";
-import { readdir, readFile, writeFile } from "node:fs/promises";
+import { readdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { IndentationText, Project, SyntaxKind } from "ts-morph";
import { ID_ATTR, INSPECT_TAG, formatSource, isCompositionTag, isLoopTag, isSerializedAssetRef, isTransformType, parseSource } from "@diffusionstudio/jsx";
+import { writeFileAtomic } from "./atomic";
+
import type { PropValue, SerializedAssetRef } from "@diffusionstudio/jsx";
import type {
ArrowFunction,
@@ -646,9 +648,11 @@ class SourceWriter {
open.text = text;
// Claimed before the write lands: a watcher must never see this change
- // arrive without knowing whose it was.
- this.context.onWrite?.(path);
- await writeFile(absolute(this.context.dir, path), text, "utf8");
+ // arrive without knowing whose it was, and what says whose it is are the
+ // bytes themselves — so they go with the claim. Written whole, because
+ // the same watcher reads the file straight back.
+ this.context.onWrite?.(path, text);
+ await writeFileAtomic(absolute(this.context.dir, path), text);
}
}
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 0988aaf9..a8d26773 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -5,10 +5,11 @@
import { app, BrowserWindow, nativeImage, session, shell } from "electron";
import { dirname, join } from "node:path";
import { existsSync } from "node:fs";
-import { mkdir, open, unlink } from "node:fs/promises";
+import { mkdir, open, rename, unlink } from "node:fs/promises";
import { randomUUID } from "node:crypto";
import type { FileHandle } from "node:fs/promises";
import { updateElectronApp } from "update-electron-app";
+import { tempPathFor } from "./atomic";
import { DapiServer } from "./dapi/server";
import { agentChatEndpoint, deleteProjectChats, startAgentChat, stopAgentChat } from "./agent-chat";
import { cliStatus, installCli, uninstallCli } from "./cli-install";
@@ -35,7 +36,8 @@ import {
unwatchAll,
listEntries,
realPathEntry,
- markSelfWriteAbsolute,
+ noteContent,
+ noteRenamed,
readConfig,
readManifest,
removeEntry,
@@ -88,7 +90,7 @@ if (app.isPackaged && !process.argv.includes("--hidden")) {
updateElectronApp({ repo: "diffusionstudio/editor" });
}
-const openWrites = new Map<string, { handle: FileHandle; path: string }>();
+const openWrites = new Map<string, { handle: FileHandle; path: string; temp: string; reserved: boolean }>();
let mainWindow: BrowserWindow | null = null;
@@ -337,26 +339,30 @@ if (app.requestSingleInstanceLock()) {
);
mainBridge.handle(MAIN_CHANNELS.FILE_WRITE_OPEN, async ({ path, exclusive }) => {
- // The folders the write has to make are changes to the project like any
- // other, and the watcher must not hear about ours: a generated asset
- // landing in a project that had no `assets/` yet would otherwise come
- // back as an outside edit and reload the library mid-write.
- const created = await mkdir(dirname(path), { recursive: true });
- markSelfWriteAbsolute(path);
- for (let folder = dirname(path); created !== undefined; folder = dirname(folder)) {
- markSelfWriteAbsolute(folder);
- if (folder === created || folder === dirname(folder)) break;
+ await mkdir(dirname(path), { recursive: true });
+
+ // `exclusive` means the name must be free, so it is taken now rather than
+ // at the rename below — the empty file that reserves it is renamed over
+ // when the write finishes, and removed when it is abandoned.
+ if (exclusive) {
+ noteContent(path, "");
+ await (await open(path, "wx")).close();
}
- const handle = await open(path, exclusive ? "wx" : "w");
+
+ // The bytes go to a temp file beside the destination and are renamed into
+ // place once they are whole. Nothing ever sees half an asset — not the
+ // watcher, not a scan of the library, not an import — so there is no
+ // window anyone has to be kept out of.
+ const temp = tempPathFor(path);
+ const handle = await open(temp, "wx");
const id = randomUUID();
- openWrites.set(id, { handle, path });
+ openWrites.set(id, { handle, path, temp, reserved: exclusive === true });
return { id };
});
mainBridge.handle(MAIN_CHANNELS.FILE_WRITE_CHUNK, async ({ id, data, position }) => {
const entry = openWrites.get(id);
if (!entry) throw new Error(`No open file for write id ${id}`);
- markSelfWriteAbsolute(entry.path);
await entry.handle.write(data, 0, data.byteLength, position);
});
@@ -364,11 +370,21 @@ if (app.requestSingleInstanceLock()) {
const entry = openWrites.get(id);
if (!entry) return;
openWrites.delete(id);
- markSelfWriteAbsolute(entry.path);
- await entry.handle.close();
+ try {
+ await entry.handle.close();
+ // Claimed before the rename, which is the first and only moment the
+ // destination changes (see `noteRenamed`).
+ await noteRenamed(entry.temp, entry.path);
+ await rename(entry.temp, entry.path);
+ } catch (error) {
+ await unlink(entry.temp).catch(() => { });
+ throw error;
+ }
});
- // Abort: close the fd and delete the partial file (cancel / error cleanup).
+ // Abort: close the fd and drop the temp file (cancel / error cleanup). The
+ // destination is left alone — an abandoned write never reached it — bar the
+ // name an `exclusive` open reserved, which is given back.
mainBridge.handle(MAIN_CHANNELS.FILE_WRITE_ABORT, async ({ id }) => {
const entry = openWrites.get(id);
if (!entry) return;
@@ -376,7 +392,11 @@ if (app.requestSingleInstanceLock()) {
try {
await entry.handle.close();
} finally {
- await unlink(entry.path).catch(() => { });
+ await unlink(entry.temp).catch(() => { });
+ if (entry.reserved) {
+ noteContent(entry.path, null);
+ await unlink(entry.path).catch(() => { });
+ }
}
});
diff --git a/apps/desktop/src/projects.ts b/apps/desktop/src/projects.ts
index 58ecaa6a..1aa279eb 100644
--- a/apps/desktop/src/projects.ts
+++ b/apps/desktop/src/projects.ts
@@ -4,8 +4,9 @@
import { app, dialog, shell, type BrowserWindow } from "electron";
import { execFile } from "node:child_process";
+import { createHash } from "node:crypto";
import { watch, type FSWatcher } from "node:fs";
-import { cp, mkdir, readdir, readFile, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
+import { cp, mkdir, readdir, readFile, realpath, rename, rm, stat } from "node:fs/promises";
import { createRequire } from "node:module";
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { nanoid } from "nanoid";
@@ -15,6 +16,7 @@ import { parse as parseYaml, stringify as stringifyYaml } from "yaml";
import type { PluginItem, TransformOptions } from "@babel/core";
import type { BuildOptions, Plugin } from "esbuild";
+import { isTempPath, TEMP_PREFIX, writeFileAtomic } from "./atomic";
import { isHeadless } from "./headless";
import { mainBridge } from "./main-manager";
import { MAIN_CHANNELS } from "./main-channels";
@@ -82,7 +84,12 @@ async function readPackage(dir: string): Promise<PackageJson | null> {
}
async function writePackage(dir: string, pkg: PackageJson): Promise<void> {
- await writeFile(join(dir, "package.json"), JSON.stringify(pkg, null, 2) + "\n", "utf8");
+ const path = join(dir, "package.json");
+ const text = JSON.stringify(pkg, null, 2) + "\n";
+ // The record is read by the app, by the watcher and by whatever else has the
+ // folder open, so it is claimed and replaced whole (see `noteContent`).
+ noteContent(path, text);
+ await writeFileAtomic(path, text);
}
// ---------------------------------------------------------------------------
@@ -111,7 +118,6 @@ async function ensureProjectId(dir: string): Promise<string> {
if (existing) return existing;
const id = nanoid();
- markSelfWrite(dir, "package.json");
await writePackage(dir, { ...(pkg ?? packageJson(basename(dir), basename(dir))), projectId: id });
return id;
}
@@ -553,6 +559,7 @@ const TSCONFIG = `{
const GITIGNORE = `node_modules/
cache/
.diffusion/
+${TEMP_PREFIX}*
`;
const STARTER = `export default function Project() {
@@ -705,7 +712,8 @@ const APP_DIR = ".diffusion";
async function writeIfMissing(dir: string, name: string, content: string): Promise<void> {
const path = join(dir, name);
if (await exists(path)) return;
- await writeFile(path, content, "utf8");
+ noteContent(path, content);
+ await writeFileAtomic(path, content);
}
/** Adds the record fields to a package.json that predates them; leaves everything else alone. */
@@ -728,7 +736,6 @@ async function ensurePackage(dir: string, name: string, displayName: string, ent
next.main !== pkg.main ||
next.scripts !== pkg.scripts
) {
- markSelfWrite(dir, "package.json");
await writePackage(dir, next);
}
}
@@ -805,7 +812,6 @@ export async function createProject(root: string, displayName: string): Promise<
export async function renameProject(dir: string, displayName: string): Promise<ProjectInfo> {
const pkg = (await readPackage(dir)) ?? packageJson(basename(dir), displayName);
const name = displayName.trim() || basename(dir);
- markSelfWrite(dir, "package.json");
await writePackage(dir, { ...pkg, displayName: name });
const project = await describe(await renameFolder(dir, name));
@@ -932,10 +938,10 @@ function solidLoader(root: string, projectPlugins: PluginItem[]): Plugin {
};
}
-/** The `./edit` context for a project folder, wired to the watcher's self-write log. */
+/** The `./edit` context for a project folder, wired to what the watcher knows. */
const sourceContext = (dir: string): SourceContext => ({
dir,
- onWrite: (file) => markSelfWrite(dir, file),
+ onWrite: (file, text) => noteContent(join(dir, file), text),
});
export async function compileProject(dir: string): Promise<CompileResult> {
@@ -982,7 +988,8 @@ export async function compileProject(dir: string): Promise<CompileResult> {
* Writes values the editor arrived at back into the JSX that produced them.
* Deliberately not a compile: the canvas already shows these values, so this
* is the file catching up with the scene rather than the other way round, and
- * the watcher is told to keep quiet about it (see `markSelfWrite`).
+ * the watcher is told what the file will hold so that it keeps quiet about it
+ * (see `noteContent`).
*/
export async function writeProject(dir: string, edits: SourceEdit[]): Promise<WriteResult> {
try {
@@ -1023,18 +1030,15 @@ export async function readManifest(dir: string): Promise<unknown> {
/**
* Writes the manifest as YAML. Atomic (temp file + rename), so a crash
- * mid-write leaves the old manifest, and marked as ours so the watcher does
- * not hand it back as a change.
+ * mid-write leaves the old manifest and no reader ever sees half of one, and
+ * claimed first so the watcher does not hand it back as a change.
*/
export async function writeManifest(dir: string, manifest: unknown): Promise<void> {
const path = join(dir, MANIFEST_FILE);
- const temp = join(dir, `.${MANIFEST_FILE}.tmp`);
- const text = stringifyYaml(manifest, { lineWidth: 0 });
- markSelfWrite(dir, MANIFEST_FILE);
- markSelfWrite(dir, `.${MANIFEST_FILE}.tmp`);
- await writeFile(temp, `# Diffusion Studio asset library. Edited by the app; hand edits are read on the next load.
-${text}`, "utf8");
- await rename(temp, path);
+ const text = `# Diffusion Studio asset library. Edited by the app; hand edits are read on the next load.
+${stringifyYaml(manifest, { lineWidth: 0 })}`;
+ noteContent(path, text);
+ await writeFileAtomic(path, text);
}
// ---------------------------------------------------------------------------
@@ -1059,7 +1063,6 @@ export async function writeConfig(dir: string, config: unknown): Promise<void> {
const next: PackageJson = { ...pkg };
if (config === null || config === undefined) delete next[CONFIG_FIELD];
else next[CONFIG_FIELD] = config;
- markSelfWrite(dir, "package.json");
await writePackage(dir, next);
}
@@ -1147,7 +1150,7 @@ export async function removeEntry(dir: string, path: string): Promise<void> {
throw new Error(`Refusing to remove ${path}: it really lives outside the project`);
}
- markSelfWrite(dir, path.split(sep).join("/"));
+ noteContent(target, null);
await rm(target, { recursive: true, force: true });
}
@@ -1156,47 +1159,87 @@ export async function removeEntry(dir: string, path: string): Promise<void> {
const watchers = new Map<string, FSWatcher>();
-/** How long a file we wrote ourselves stays exempt from the watcher. */
-const SELF_WRITE_GRACE = 1000;
+/**
+ * What the app believes is on disk, by absolute path: a digest of the content
+ * of every file it has written, and of every change it has already been told
+ * about. The watcher stays on — anything else may be editing these files, and
+ * should still reach the canvas — but the app's own writes must not come back
+ * as a change, or every key stamp and every dragged rect would cost a
+ * recompile and a remount of the scene the user is looking at.
+ *
+ * Identity rather than timing, which is what makes this exact. A change is the
+ * app's own when the bytes on disk are the bytes it meant to put there, however
+ * long the event takes to arrive: an outside edit is never swallowed for
+ * landing too soon after one of ours, and one of ours never gets through for
+ * landing too late. A write that changes nothing — a formatter saving back what
+ * it was given, a checkout of the blob already there — costs nothing either.
+ */
+const known = new Map<string, string>();
+
+/** Digests for what has no content: a file that is not there, and a folder. */
+const ABSENT = "";
+const FOLDER = "folder";
/**
- * Writes made on behalf of the editor, by file and time. The watcher stays on
- * — anything else may be editing these files, and should still reach the
- * canvas — but our own writes must not come back as a change, or every key
- * stamp and every dragged rect would cost a recompile and a remount of the
- * scene the user is looking at.
+ * Above this a file is fingerprinted by size and mtime rather than by content:
+ * the library holds whole videos, and re-reading one to answer an event would
+ * cost more than the reload it saves. A rename carries both across untouched,
+ * so a fingerprint taken from a temp file still answers for the file it
+ * becomes (see `noteRenamed`).
*/
-const selfWrites = new Map<string, number>();
+const DIGEST_MAX = 8 * 1024 * 1024;
-const writeKey = (dir: string, path: string): string => `${dir}\n${path}`;
+const digest = (content: Buffer | string): string => createHash("sha1").update(content).digest("hex");
-export function markSelfWrite(dir: string, path: string): void {
- selfWrites.set(writeKey(dir, path), Date.now());
+/** What `path` holds now: a digest of its content, or `ABSENT` / `FOLDER`. */
+async function digestOf(path: string): Promise<string> {
+ try {
+ const info = await stat(path);
+ if (info.isDirectory()) return FOLDER;
+ if (info.size > DIGEST_MAX) return `${info.size}:${info.mtimeMs}`;
+ return digest(await readFile(path));
+ } catch {
+ return ABSENT;
+ }
}
-/** `markSelfWrite` for an absolute path, against whichever watched project holds it. */
-export function markSelfWriteAbsolute(path: string): void {
- for (const dir of watchers.keys()) {
- const rel = relative(dir, path);
- if (rel && !rel.startsWith("..") && !isAbsolute(rel)) {
- markSelfWrite(dir, rel.split(sep).join("/"));
- }
- }
+/**
+ * Claims the content a write is about to put at `path` — or, for null, that it
+ * is about to take what is there away. Before the write rather than after: the
+ * event can arrive the instant the bytes land, and it is answered by comparing
+ * them against this. A write that fails halfway leaves the claim wrong, which
+ * the next event corrects by reporting a change nobody made — the safe way
+ * round, and self-correcting either way.
+ */
+export function noteContent(path: string, text: string | null): void {
+ if (text === null) known.set(path, ABSENT);
+ else if (Buffer.byteLength(text) > DIGEST_MAX) known.delete(path);
+ else known.set(path, digest(text));
}
-function isSelfWrite(dir: string, path: string): boolean {
- const key = writeKey(dir, path);
- const at = selfWrites.get(key);
- if (at === undefined) return false;
- if (Date.now() - at > SELF_WRITE_GRACE) {
- selfWrites.delete(key);
- return false;
- }
- return true;
+/**
+ * Claims the content of the temp file `temp` as what will be found at `as`.
+ *
+ * The one write whose content the app never holds in one piece is a streamed
+ * one — an asset encoded straight to disk. It lands in a temp file, which the
+ * watcher ignores, and is renamed into place once it is whole; claiming it
+ * from the temp just before that rename is what leaves no window at all. A
+ * rename changes neither the bytes nor the mtime, so what is claimed here is
+ * exactly what an event will find at `as`.
+ */
+export async function noteRenamed(temp: string, as: string): Promise<void> {
+ known.set(as, await digestOf(temp));
}
export function watchProject(window: BrowserWindow | null, dir: string): void {
if (watchers.has(dir)) return;
+
+ // Events are answered one at a time: answering one means reading the file
+ // back, and two reads in flight could settle out of order, leaving `known`
+ // holding the older of two contents — and with it a change that would then
+ // never be reported.
+ let queue: Promise<void> = Promise.resolve();
+
const watcher = watch(dir, { recursive: true }, (_event, filename) => {
if (!filename) return;
// Project-relative and `/`-separated; installs churn node_modules constantly.
@@ -1204,8 +1247,18 @@ export function watchProject(window: BrowserWindow | null, dir: string): void {
if (path.startsWith("node_modules/") || path === "node_modules") return;
// The app's folder: a docs refresh writes the whole tree in one burst.
if (path.startsWith(`${APP_DIR}/`) || path === APP_DIR) return;
- if (isSelfWrite(dir, path)) return;
- mainBridge.emit(window, MAIN_CHANNELS.PROJECTS_CHANGED, { dir, path });
+ // Half a file by definition, and renamed away the moment it is whole.
+ if (isTempPath(path)) return;
+
+ const file = join(dir, filename);
+ queue = queue
+ .then(async () => {
+ const current = await digestOf(file);
+ if (known.get(file) === current) return;
+ known.set(file, current);
+ mainBridge.emit(window, MAIN_CHANNELS.PROJECTS_CHANGED, { dir, path });
+ })
+ .catch(() => { });
});
watcher.on("error", () => unwatchProject(dir));
watchers.set(dir, watcher);
@@ -1214,6 +1267,10 @@ export function watchProject(window: BrowserWindow | null, dir: string): void {
export function unwatchProject(dir: string): void {
watchers.get(dir)?.close();
watchers.delete(dir);
+ // What the folder holds while nobody is watching is not the app's to
+ // remember: the next watch starts from a fresh load of the project anyway.
+ const prefix = dir.endsWith(sep) ? dir : dir + sep;
+ for (const path of known.keys()) if (path.startsWith(prefix)) known.delete(path);
}
export function unwatchAll(): void {
diff --git a/apps/desktop/src/projects.watch.test.ts b/apps/desktop/src/projects.watch.test.ts
new file mode 100644
index 00000000..ccc15f6f
--- /dev/null
+++ b/apps/desktop/src/projects.watch.test.ts
@@ -0,0 +1,184 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// What the watcher must and must not report. The app writes the files it
+// watches, so the whole question is whether a change is its own — answered by
+// content (see `noteContent`), which is what these pin down.
+
+import { tmpdir } from "node:os";
+import { mkdir, mkdtemp, open, rename, rm, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { MAIN_CHANNELS, MAIN_WIRE } from "./main-channels";
+import { TEMP_PREFIX, tempPathFor, writeFileAtomic } from "./atomic";
+
+vi.mock("electron", () => ({
+ app: { isPackaged: false, getPath: () => tmpdir() },
+ dialog: {},
+ shell: {},
+ ipcMain: { on: () => { } },
+}));
+
+const { noteContent, noteRenamed, unwatchProject, watchProject, writeManifest } = await import("./projects");
+
+let dir: string;
+let changed: string[] = [];
+
+/** A window that only remembers which project files it was told about. */
+const window = {
+ isDestroyed: () => false,
+ webContents: {
+ isLoading: () => false,
+ send: (wire: string, envelope: { channel: string; data: { path: string } }) => {
+ if (wire === MAIN_WIRE.EVENT && envelope.channel === MAIN_CHANNELS.PROJECTS_CHANGED) {
+ changed.push(envelope.data.path);
+ }
+ },
+ },
+} as unknown as Parameters<typeof watchProject>[0];
+
+/** Long enough for an event to have arrived, for the cases where none may. */
+const settle = (ms = 400): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
+
+/** Waits for `path` to be reported, and fails the test when it never is. */
+async function waitFor(path: string, timeout = 4000): Promise<void> {
+ const until = Date.now() + timeout;
+ while (Date.now() < until) {
+ if (changed.includes(path)) return;
+ await settle(25);
+ }
+ expect(changed).toContain(path);
+}
+
+beforeEach(async () => {
+ dir = await mkdtemp(join(tmpdir(), "watch-test-"));
+ changed = [];
+ watchProject(window, dir);
+ await settle(100);
+});
+
+afterEach(async () => {
+ unwatchProject(dir);
+ await rm(dir, { recursive: true, force: true });
+});
+
+describe("watchProject", () => {
+ it("reports a file someone else writes", async () => {
+ await writeFile(join(dir, "index.tsx"), "export const stage = 1;\n", "utf8");
+ await waitFor("index.tsx");
+ });
+
+ // Without this the claim above proves nothing: a rename that never reached
+ // the watcher would look exactly like a claim that worked.
+ it("reports a file someone else replaces by rename", async () => {
+ await writeFileAtomic(join(dir, "index.tsx"), "export const stage = 1;\n");
+ await waitFor("index.tsx");
+ });
+
+ it("says nothing about a write the app claimed", async () => {
+ const file = join(dir, "index.tsx");
+ const text = "export const stage = 1;\n";
+ noteContent(file, text);
+ await writeFileAtomic(file, text);
+
+ await settle();
+ expect(changed).toEqual([]);
+ });
+
+ it("reports an outside edit that lands right after one of the app's own", async () => {
+ const file = join(dir, "index.tsx");
+ noteContent(file, "ours\n");
+ await writeFileAtomic(file, "ours\n");
+ // No claim: this one is someone else's, however close behind it comes.
+ await writeFile(file, "theirs\n", "utf8");
+
+ await waitFor("index.tsx");
+ });
+
+ it("says nothing about a write that changes nothing", async () => {
+ const file = join(dir, "notes.txt");
+ await writeFile(file, "same\n", "utf8");
+ await waitFor("notes.txt");
+
+ changed = [];
+ await writeFile(file, "same\n", "utf8");
+ await settle();
+ expect(changed).toEqual([]);
+ });
+
+ it("reports a file someone else deletes", async () => {
+ const file = join(dir, "notes.txt");
+ await writeFile(file, "here\n", "utf8");
+ await waitFor("notes.txt");
+
+ changed = [];
+ await rm(file);
+ await waitFor("notes.txt");
+ });
+
+ it("ignores the temp files an atomic write leaves in the folder", async () => {
+ await writeFile(join(dir, `${TEMP_PREFIX}index.tsx.1-1`), "half a file", "utf8");
+ await settle();
+ expect(changed).toEqual([]);
+ });
+
+ it("says nothing about an asset streamed to a temp file and renamed into place", async () => {
+ await mkdir(join(dir, "assets"), { recursive: true });
+ await waitFor("assets");
+ const file = join(dir, "assets", "clip.bin");
+
+ changed = [];
+ const temp = tempPathFor(file);
+ const handle = await open(temp, "wx");
+ // Positioned chunks, the way an encoder writes one.
+ await handle.write(Buffer.from("chunk one"), 0, 9, 0);
+ await settle();
+ await handle.write(Buffer.from(" and two"), 0, 8, 9);
+ await handle.close();
+ await settle();
+ // Nothing has appeared where the library looks, so there is nothing to say.
+ expect(changed).toEqual([]);
+
+ await noteRenamed(temp, file);
+ await rename(temp, file);
+ await settle();
+ expect(changed).toEqual([]);
+ });
+
+ it("reports an asset someone else renames into place", async () => {
+ await mkdir(join(dir, "assets"), { recursive: true });
+ await waitFor("assets");
+
+ changed = [];
+ const temp = tempPathFor(join(dir, "assets", "clip.bin"));
+ await writeFile(temp, "theirs", "utf8");
+ await rename(temp, join(dir, "assets", "clip.bin"));
+
+ await waitFor("assets/clip.bin");
+ });
+
+ // The claim for a file too big to hash is its size and mtime, which a rename
+ // has to carry across untouched for `noteRenamed` to mean anything.
+ it("says nothing about a file too big to hash, claimed before its rename", async () => {
+ const file = join(dir, "assets", "big.bin");
+ await mkdir(join(dir, "assets"), { recursive: true });
+ await waitFor("assets");
+
+ changed = [];
+ const temp = tempPathFor(file);
+ await writeFile(temp, Buffer.alloc(9 * 1024 * 1024, 7));
+ await noteRenamed(temp, file);
+ await rename(temp, file);
+
+ await settle();
+ expect(changed).toEqual([]);
+ });
+
+ it("says nothing about the manifest it writes itself", async () => {
+ await writeManifest(dir, { assets: [{ source: "assets/clip.mp4" }] });
+ await settle();
+ expect(changed).toEqual([]);
+ });
+});
diff --git a/apps/web/src/pages/editor.tsx b/apps/web/src/pages/editor.tsx
index 05214e92..68a3c8b9 100644
--- a/apps/web/src/pages/editor.tsx
+++ b/apps/web/src/pages/editor.tsx
@@ -156,20 +156,23 @@ export function EditorPage() {
load();
- const unwatch = watchProject(dir, (path) => {
- if (isCacheFile(path)) return;
- if (isLibraryFile(path)) {
- library.load();
- } else {
- // package.json is the config and the record (`main`, `displayName`)
- // in one, so a hand edit to it reloads both; the app's own config
- // writes never reach here (main keeps them from the watcher).
- if (isProjectConfigFile(path)) {
- config.load();
- void project.refresh();
- }
- load();
+ // A burst arrives as the whole set of files it touched, so a checkout that
+ // rewrites the library and the sources at once reloads both — reading only
+ // the last path of a burst would answer for one of them and drop the rest.
+ const unwatch = watchProject(dir, (paths) => {
+ const changed = paths.filter((path) => !isCacheFile(path));
+ if (changed.some(isLibraryFile)) library.load();
+
+ const source = changed.filter((path) => !isLibraryFile(path));
+ if (!source.length) return;
+ // package.json is the config and the record (`main`, `displayName`)
+ // in one, so a hand edit to it reloads both; the app's own config
+ // writes never reach here (main keeps them from the watcher).
+ if (source.some(isProjectConfigFile)) {
+ config.load();
+ project.refresh();
}
+ load();
});
onCleanup(() => {
diff --git a/apps/web/src/projects/host.ts b/apps/web/src/projects/host.ts
index 4519f86a..acb3c459 100644
--- a/apps/web/src/projects/host.ts
+++ b/apps/web/src/projects/host.ts
@@ -205,7 +205,7 @@ export function compileProject(dir: string): Promise<CompileResult> {
/**
* Writes changed props back into the project's JSX. No compile follows: the
* canvas is already showing these values, and main keeps the write from
- * reaching the watcher (see `markSelfWrite` in the desktop's projects.ts).
+ * reaching the watcher (see `noteContent` in the desktop's projects.ts).
*/
export function writeProject(dir: string, edits: SourceEdit[]): Promise<WriteResult> {
return mainBridge.call(MAIN_CHANNELS.PROJECTS_WRITE, { dir, edits });
@@ -222,25 +222,36 @@ export function writeProjectConfig(dir: string, config: unknown): Promise<void>
}
/**
- * Watches a project folder and calls `onChange` (debounced) when a file
- * inside it changes. Returns the unwatch function.
+ * Watches a project folder and calls `onChange` with every file that changed
+ * since the last call, coalescing a burst of them — an install, a checkout, a
+ * folder dropped into the library — into one answer.
+ *
+ * The delay buys throughput and nothing else: main keeps the app's own writes
+ * out of this stream by their content rather than by their timing (see
+ * `noteContent` in the desktop's projects.ts) and writes whole files, so no
+ * amount of waiting here is load-bearing.
*/
-export function watchProject(dir: string, onChange: (path: string) => void, debounceMs = 80): () => void {
+export function watchProject(dir: string, onChange: (paths: string[]) => void, debounceMs = 80): () => void {
if (!isDesktop()) return () => {};
let pending: ReturnType<typeof setTimeout> | undefined;
- let last = '';
+ let changed = new Set<string>();
const stop = mainBridge.handle(MAIN_CHANNELS.PROJECTS_CHANGED, (event) => {
if (event.dir !== dir) return;
- last = event.path;
+ changed.add(event.path);
clearTimeout(pending);
- pending = setTimeout(() => onChange(last), debounceMs);
+ pending = setTimeout(() => {
+ const paths = [...changed];
+ changed = new Set();
+ onChange(paths);
+ }, debounceMs);
});
- void mainBridge.call(MAIN_CHANNELS.PROJECTS_WATCH, { dir });
+
+ mainBridge.call(MAIN_CHANNELS.PROJECTS_WATCH, { dir });
return () => {
clearTimeout(pending);
stop();
- void mainBridge.call(MAIN_CHANNELS.PROJECTS_UNWATCH, { dir }).catch(() => {});
+ mainBridge.call(MAIN_CHANNELS.PROJECTS_UNWATCH, { dir }).catch(() => {});
};
}