| 536 | // == watch |
| 537 | |
| 538 | const watchNode = (path: string, options?: FileSystem.WatchOptions) => |
| 539 | Stream.asyncScoped<FileSystem.WatchEvent, Error.PlatformError>((emit) => |
| 540 | Effect.acquireRelease( |
| 541 | Effect.sync(() => { |
| 542 | const watcher = NFS.watch(path, { recursive: options?.recursive }, (event, path) => { |
| 543 | if (!path) return |
| 544 | switch (event) { |
| 545 | case "rename": { |
| 546 | emit.fromEffect(Effect.matchEffect(stat(path), { |
| 547 | onSuccess: (_) => Effect.succeed(FileSystem.WatchEventCreate({ path })), |
| 548 | onFailure: (err) => |
| 549 | err._tag === "SystemError" && err.reason === "NotFound" |
| 550 | ? Effect.succeed(FileSystem.WatchEventRemove({ path })) |
| 551 | : Effect.fail(err) |
| 552 | })) |
| 553 | return |
| 554 | } |
| 555 | case "change": { |
| 556 | emit.single(FileSystem.WatchEventUpdate({ path })) |
| 557 | return |
| 558 | } |
| 559 | } |
| 560 | }) |
| 561 | watcher.on("error", (error) => { |
| 562 | emit.fail( |
| 563 | new Error.SystemError({ |
| 564 | module: "FileSystem", |
| 565 | reason: "Unknown", |
| 566 | method: "watch", |
| 567 | pathOrDescriptor: path, |
| 568 | cause: error |
| 569 | }) |
| 570 | ) |
| 571 | }) |
| 572 | watcher.on("close", () => { |
| 573 | emit.end() |
| 574 | }) |
| 575 | return watcher |
| 576 | }), |
| 577 | (watcher) => Effect.sync(() => watcher.close()) |
| 578 | ) |
| 579 | ) |
| 580 | |
| 581 | const watch = ( |
| 582 | backend: Option.Option<Context.Tag.Service<FileSystem.WatchBackend>>, |