UNPKG

convex

Version:

Client for the Convex Cloud

85 lines (84 loc) 2.13 kB
"use strict"; import chokidar from "chokidar"; import path from "path"; import { RecordingFs } from "../../bundler/fs.js"; import * as Sentry from "@sentry/node"; export class Watcher { constructor(observations) { this.bufferedEvents = []; this.waiters = []; const watch = chokidar.watch(observations.paths(), { persistent: true }); watch.on("all", (eventName, eventPath) => { const absPath = path.resolve(eventPath); this.bufferedEvents.push({ name: eventName, absPath }); for (const waiter of drain(this.waiters)) { waiter(); } }); this.readyCb = new Promise((resolve) => { watch.on("ready", () => resolve()); }); this.watch = watch; } update(observations) { const watchedDirs = new Set(Object.keys(this.watch.getWatched())); for (const newPath of observations.paths()) { if (!this.isWatched(watchedDirs, newPath)) { this.watch.add(newPath); } } } isWatched(watchedDirs, observedPath) { let curPath = observedPath; while (true) { const parsed = path.parse(curPath); if (parsed.dir === curPath) { break; } if (watchedDirs.has(curPath)) { return true; } curPath = parsed.dir; } return false; } async ready() { await this.readyCb; } async waitForEvent() { while (this.bufferedEvents.length === 0) { const newEvent = new Promise((resolve) => { this.waiters.push(resolve); }); await newEvent; } } drainEvents() { return drain(this.bufferedEvents); } async close() { await this.watch.close(); } } function drain(l) { return l.splice(0, l.length); } export class Crash extends Error { constructor(errorType, err) { super(err?.message); this.errorType = errorType; } } export class WatchContext { constructor(traceEvents) { this.fs = new RecordingFs(traceEvents); this.deprecationMessagePrinted = false; } crash(_exitCode, retry, err) { if (err) { Sentry.captureException(err); } throw new Crash(retry, err); } } //# sourceMappingURL=watch.js.map