UNPKG

convex

Version:

Client for the Convex Cloud

115 lines (114 loc) 3.5 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var watch_exports = {}; __export(watch_exports, { FatalError: () => FatalError, WatchContext: () => WatchContext, Watcher: () => Watcher }); module.exports = __toCommonJS(watch_exports); var import_chokidar = __toESM(require("chokidar")); var import_path = __toESM(require("path")); var import_fs = require("../../bundler/fs.js"); var Sentry = __toESM(require("@sentry/node")); class Watcher { constructor(observations) { this.bufferedEvents = []; this.waiters = []; const watch = import_chokidar.default.watch(observations.paths(), { persistent: true }); watch.on("all", (eventName, eventPath) => { const absPath = import_path.default.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 = import_path.default.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); } class FatalError extends Error { constructor(reason, err) { super(err?.message); this.reason = reason; } } class WatchContext { constructor(traceEvents) { this.fs = new import_fs.RecordingFs(traceEvents); this.deprecationMessagePrinted = false; } fatalError(_exitCode, reason, err) { if (err) { Sentry.captureException(err); } throw new FatalError(reason, err); } } //# sourceMappingURL=watch.js.map