| 551 | // == watch |
| 552 | |
| 553 | const watchNode = (path: string, options?: FileSystem.WatchOptions) => |
| 554 | Stream.callback<FileSystem.WatchEvent, Error.PlatformError>((queue) => |
| 555 | Effect.acquireRelease( |
| 556 | Effect.sync(() => { |
| 557 | const watcher = NFS.watch(path, { |
| 558 | recursive: options?.recursive ?? false |
| 559 | }, (event, path) => { |
| 560 | if (!path) return |
| 561 | switch (event) { |
| 562 | case "rename": { |
| 563 | Effect.runFork(Effect.matchEffect(stat(path), { |
| 564 | onSuccess: (_) => Queue.offer(queue, { _tag: "Create", path }), |
| 565 | onFailure: (_) => Queue.offer(queue, { _tag: "Remove", path }) |
| 566 | })) |
| 567 | return |
| 568 | } |
| 569 | case "change": { |
| 570 | Queue.offerUnsafe(queue, { _tag: "Update", path }) |
| 571 | return |
| 572 | } |
| 573 | } |
| 574 | }) |
| 575 | watcher.on("error", (error) => { |
| 576 | Queue.failCauseUnsafe( |
| 577 | queue, |
| 578 | Cause.fail( |
| 579 | Error.systemError({ |
| 580 | module: "FileSystem", |
| 581 | _tag: "Unknown", |
| 582 | method: "watch", |
| 583 | pathOrDescriptor: path, |
| 584 | cause: error |
| 585 | }) |
| 586 | ) |
| 587 | ) |
| 588 | }) |
| 589 | watcher.on("close", () => { |
| 590 | Queue.endUnsafe(queue) |
| 591 | }) |
| 592 | return watcher |
| 593 | }), |
| 594 | (watcher) => Effect.sync(() => watcher.close()) |
| 595 | ) |
| 596 | ) |
| 597 | |
| 598 | const watch = ( |
| 599 | backend: Option.Option<FileSystem.WatchBackend["Service"]>, |