@aprovan/patchwork-compiler 0.1.2-dev.99f9769 → 0.1.2-dev.9c336a0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -676,6 +676,8 @@ declare class IndexedDBBackend implements FSProvider {
676
676
 
677
677
  interface HttpBackendConfig {
678
678
  baseUrl: string;
679
+ /** How often to poll for external changes, in milliseconds. Default: 7000. */
680
+ pollIntervalMs?: number;
679
681
  }
680
682
  /**
681
683
  * HTTP-based FSProvider for connecting to remote VFS servers
@@ -695,8 +697,8 @@ declare class HttpBackend implements FSProvider {
695
697
  recursive?: boolean;
696
698
  }): Promise<void>;
697
699
  exists(path: string): Promise<boolean>;
698
- watch(path: string, callback: WatchCallback): () => void;
699
- private startWatch;
700
+ watch(_path: string, callback: WatchCallback): () => void;
701
+ private startPoll;
700
702
  private url;
701
703
  }
702
704
 
package/dist/index.d.ts CHANGED
@@ -676,6 +676,8 @@ declare class IndexedDBBackend implements FSProvider {
676
676
 
677
677
  interface HttpBackendConfig {
678
678
  baseUrl: string;
679
+ /** How often to poll for external changes, in milliseconds. Default: 7000. */
680
+ pollIntervalMs?: number;
679
681
  }
680
682
  /**
681
683
  * HTTP-based FSProvider for connecting to remote VFS servers
@@ -695,8 +697,8 @@ declare class HttpBackend implements FSProvider {
695
697
  recursive?: boolean;
696
698
  }): Promise<void>;
697
699
  exists(path: string): Promise<boolean>;
698
- watch(path: string, callback: WatchCallback): () => void;
699
- private startWatch;
700
+ watch(_path: string, callback: WatchCallback): () => void;
701
+ private startPoll;
700
702
  private url;
701
703
  }
702
704
 
package/dist/index.js CHANGED
@@ -2483,37 +2483,46 @@ var HttpBackend = class {
2483
2483
  const res = await fetch(this.url(path), { method: "HEAD" });
2484
2484
  return res.ok;
2485
2485
  }
2486
- watch(path, callback) {
2486
+ watch(_path, callback) {
2487
2487
  const controller = new AbortController();
2488
- this.startWatch(path, callback, controller.signal);
2488
+ this.startPoll(callback, controller.signal);
2489
2489
  return () => controller.abort();
2490
2490
  }
2491
- async startWatch(path, callback, signal) {
2492
- try {
2493
- const res = await fetch(this.url("", { watch: path }), { signal });
2494
- if (!res.ok) return;
2495
- const reader = res.body?.getReader();
2496
- if (!reader) return;
2497
- const decoder = new TextDecoder();
2498
- let buffer = "";
2499
- while (!signal.aborted) {
2500
- const { done, value } = await reader.read();
2501
- if (done) break;
2502
- buffer += decoder.decode(value, { stream: true });
2503
- const lines = buffer.split("\n");
2504
- buffer = lines.pop() ?? "";
2505
- for (const line of lines) {
2506
- if (line.startsWith("data: ")) {
2507
- try {
2508
- const event = JSON.parse(line.slice(6));
2509
- callback(event.type, event.path);
2510
- } catch {
2511
- }
2512
- }
2491
+ async startPoll(callback, signal) {
2492
+ const intervalMs = this.config.pollIntervalMs ?? 7e3;
2493
+ let since = (/* @__PURE__ */ new Date()).toISOString();
2494
+ const poll = async () => {
2495
+ if (signal.aborted) return;
2496
+ if (typeof document !== "undefined" && document.visibilityState !== "visible") return;
2497
+ const pollStart = (/* @__PURE__ */ new Date()).toISOString();
2498
+ try {
2499
+ const res = await fetch(this.url("", { since }), { signal });
2500
+ if (!res.ok) return;
2501
+ const changes = await res.json();
2502
+ since = pollStart;
2503
+ for (const change of changes) {
2504
+ callback("update", change.path);
2513
2505
  }
2506
+ } catch {
2514
2507
  }
2515
- } catch {
2508
+ };
2509
+ await poll();
2510
+ if (signal.aborted) return;
2511
+ const timer = setInterval(() => void poll(), intervalMs);
2512
+ const onVisibilityChange = () => {
2513
+ if (typeof document !== "undefined" && document.visibilityState === "visible") {
2514
+ void poll();
2515
+ }
2516
+ };
2517
+ if (typeof document !== "undefined") {
2518
+ document.addEventListener("visibilitychange", onVisibilityChange);
2516
2519
  }
2520
+ signal.addEventListener("abort", () => {
2521
+ clearInterval(timer);
2522
+ if (typeof document !== "undefined") {
2523
+ document.removeEventListener("visibilitychange", onVisibilityChange);
2524
+ }
2525
+ });
2517
2526
  }
2518
2527
  url(path, params) {
2519
2528
  const baseUrl = this.config.baseUrl.replace(/\/+$/, "");