WatchBackendService key for file system watch backend implementations.
Details
This service provides the low-level file watching capabilities that can be implemented differently on various platforms (e.g., inotify on Linux, FSEvents on macOS, etc.).
Example (Providing a custom watch backend)
import { Effect, FileSystem, Option, Stream } from "effect"
// Custom watch backend implementation
const customWatchBackend = {
register: (path: string, stat: FileSystem.File.Info) => {
// Implementation would depend on platform
return Option.some(Stream.empty) // Placeholder implementation
}
}
// Provide custom watch backend
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// File watching will use the custom backend
const watcher = fs.watch("./directory")
})
const withCustomBackend = Effect.provideService(
program,
FileSystem.WatchBackend,
customWatchBackend
)export class class WatchBackendclass WatchBackend {
key: Identifier;
Service: {
register: (path: string, stat: File.Info) => Option.Option<Stream.Stream<WatchEvent, PlatformError>>;
};
}
Service key for file system watch backend implementations.
Details
This service provides the low-level file watching capabilities that can be
implemented differently on various platforms (e.g., inotify on Linux,
FSEvents on macOS, etc.).
Example (Providing a custom watch backend)
import { Effect, FileSystem, Option, Stream } from "effect"
// Custom watch backend implementation
const customWatchBackend = {
register: (path: string, stat: FileSystem.File.Info) => {
// Implementation would depend on platform
return Option.some(Stream.empty) // Placeholder implementation
}
}
// Provide custom watch backend
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// File watching will use the custom backend
const watcher = fs.watch("./directory")
})
const withCustomBackend = Effect.provideService(
program,
FileSystem.WatchBackend,
customWatchBackend
)
WatchBackend extends import ContextContext.Service<class WatchBackendclass WatchBackend {
key: Identifier;
Service: {
register: (path: string, stat: File.Info) => Option.Option<Stream.Stream<WatchEvent, PlatformError>>;
};
}
Service key for file system watch backend implementations.
Details
This service provides the low-level file watching capabilities that can be
implemented differently on various platforms (e.g., inotify on Linux,
FSEvents on macOS, etc.).
Example (Providing a custom watch backend)
import { Effect, FileSystem, Option, Stream } from "effect"
// Custom watch backend implementation
const customWatchBackend = {
register: (path: string, stat: FileSystem.File.Info) => {
// Implementation would depend on platform
return Option.some(Stream.empty) // Placeholder implementation
}
}
// Provide custom watch backend
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// File watching will use the custom backend
const watcher = fs.watch("./directory")
})
const withCustomBackend = Effect.provideService(
program,
FileSystem.WatchBackend,
customWatchBackend
)
WatchBackend, {
readonly register: (
path: string,
stat: File.Info
) => Option.Option<
Stream.Stream<WatchEvent, PlatformError>
>
register: (path: stringpath: string, stat: File.Info(parameter) stat: {
type: Type;
mtime: Option.Option<Date>;
atime: Option.Option<Date>;
birthtime: Option.Option<Date>;
dev: number;
ino: Option.Option<number>;
mode: number;
nlink: Option.Option<number>;
uid: Option.Option<number>;
gid: Option.Option<number>;
rdev: Option.Option<number>;
size: Size;
blksize: Option.Option<Size>;
blocks: Option.Option<number>;
}
stat: File.interface File.InfoComprehensive file information structure.
Details
Contains metadata about a file or directory including type, timestamps,
permissions, and size information. This structure is returned by file
stat operations.
Example (Inspecting file information)
import { Effect, FileSystem, Option } from "effect"
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
const path = yield* fs.makeTempFile({ prefix: "info-" })
yield* fs.writeFileString(path, "hello")
const info: FileSystem.File.Info = yield* fs.stat(path)
console.log(`File type: ${info.type}`) // "File type: File"
console.log(`File size: ${info.size} bytes`) // "File size: 5 bytes"
console.log(`Mode: ${info.mode.toString(8)}`) // Octal permissions
// Handle optional timestamps without inventing a fallback date
const modified = Option.match(info.mtime, {
onNone: () => "unavailable",
onSome: (mtime) => mtime.toISOString()
})
console.log(`Modified: ${modified}`)
// Check if it's a regular file
if (info.type === "File") {
console.log("Processing regular file...") // "Processing regular file..."
}
yield* fs.remove(path)
})
Info) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<import StreamStream.interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<type WatchEvent = WatchEvent.Create | WatchEvent.Update | WatchEvent.RemoveRepresents file system events emitted when watching files or directories.
When to use
Use when consuming file system watch streams and pattern matching on _tag
to handle created, updated, or removed paths.
Details
The union covers create, update, and remove events. Each event carries the
reported path.
Namespace containing the concrete event shapes emitted by FileSystem.watch.
WatchEvent, class PlatformErrorclass PlatformError {
message: string;
name: string;
stack: string;
cause: unknown;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
reason: BadArgument | SystemError;
}
Tagged error used by platform APIs to report either invalid arguments or
system-level failures.
When to use
Use as the shared error type for platform APIs that expose invalid arguments
and host or operating-system failures through a single Effect error
channel.
Details
The reason field contains the underlying BadArgument or SystemError.
When that reason has a cause, the cause is preserved on the wrapper.
PlatformError>>
}>()("effect/platform/FileSystem/WatchBackend") {}