MCPcopy Create free account
hub / github.com/Effect-TS/effect / watchNode

Function watchNode

packages/platform/node-shared/src/NodeFileSystem.ts:680–724  ·  view source on GitHub ↗
(path: string, info: FileSystem.File.Info, options?: FileSystem.WatchOptions)

Source from the content-addressed store, hash-verified

678// == watch
679
680const 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
726const watch = (
727 backend: Option.Option<FileSystem.WatchBackend["Service"]>,

Callers 1

watchFunction · 0.85

Calls 10

resolveMethod · 0.80
offerUnsafeMethod · 0.80
onReadyMethod · 0.80
pipeMethod · 0.65
onMethod · 0.65
failMethod · 0.65
closeMethod · 0.65
statFunction · 0.50
syncMethod · 0.45
offerMethod · 0.45

Tested by

no test coverage detected