| 678 | // == watch |
| 679 | |
| 680 | const watchNode = (path: string, info: FileSystem.File.Info, options?: FileSystem.WatchOptions) => |
| 681 | Stream.callback<FileSystem.WatchEvent, Error.PlatformError>((queue) => |
| 682 | Effect.acquireRelease( |
| 683 | Effect.sync(() => { |
| 684 | const directory = info.type === "Directory" ? path : Path.dirname(path) |
| 685 | const watcher = NFS.watch(path, { |
| 686 | recursive: options?.recursive ?? false |
| 687 | }, (event, path) => { |
| 688 | if (!path) return |
| 689 | switch (event) { |
| 690 | case "rename": { |
| 691 | Effect.runFork(Effect.matchEffect(stat(Path.resolve(directory, path)), { |
| 692 | onSuccess: (_) => Queue.offer(queue, { _tag: "Create", path }), |
| 693 | onFailure: (_) => Queue.offer(queue, { _tag: "Remove", path }) |
| 694 | })) |
| 695 | return |
| 696 | } |
| 697 | case "change": { |
| 698 | Queue.offerUnsafe(queue, { _tag: "Update", path }) |
| 699 | return |
| 700 | } |
| 701 | } |
| 702 | }) |
| 703 | watcher.on("error", (error) => { |
| 704 | Queue.failCauseUnsafe( |
| 705 | queue, |
| 706 | Cause.fail( |
| 707 | Error.systemError({ |
| 708 | module: "FileSystem", |
| 709 | _tag: "Unknown", |
| 710 | method: "watch", |
| 711 | pathOrDescriptor: path, |
| 712 | cause: error |
| 713 | }) |
| 714 | ) |
| 715 | ) |
| 716 | }) |
| 717 | watcher.on("close", () => { |
| 718 | Queue.endUnsafe(queue) |
| 719 | }) |
| 720 | return watcher |
| 721 | }), |
| 722 | (watcher) => Effect.sync(() => watcher.close()) |
| 723 | ).pipe(Effect.tap(() => Effect.sync(() => options?.onReady?.()))) |
| 724 | ) |
| 725 | |
| 726 | const watch = ( |
| 727 | backend: Option.Option<FileSystem.WatchBackend["Service"]>, |