Skip to content

fs.watch

Lets you watch for changes to a path.

Signature

luau
function watch(path: string): Watcher

Types

luau
type WatchEvent = "created" | "modified" | "removed" | "renamed"

type WatchChange = {
    path: string,
    event: WatchEvent,
}

type Watcher = {
    onChange: Signal<WatchChange>,
    close: (self: Watcher) -> (),
}

Signal<T>.

Summary

Parameters

ParameterTypeDescription
pathstringThe path to watch

Properties

PropertyTypeDescription
onChangeSignal<WatchChange>Signal that fires when a change is detected

Methods

MethodSignatureDescription
close(self) -> ()Stops the watcher

Example

luau
local fs = require("@std/fs")

local watcher = fs.watch("./config")

watcher.onChange:Connect(function(change)
    print(change.event, change.path)
    -- e.g. "modified  config.json"
end)

-- Later, stop watching:
watcher:close()

One-time Change

luau
local fs = require("@std/fs")

local watcher = fs.watch("./data")

watcher.onChange:Once(function(change)
    print("First change:", change.event, change.path)
    watcher:close()
end)