Skip to content

Conversation

@annieetang
Copy link
Contributor

@annieetang annieetang commented Nov 24, 2025

As part of the API audit/cleanup, we reimplement fs.watch as an iterator instead of passing in a callback so you can use fs.watch on a task cooperatively.

This will then have the issue that fs.walk has where for loops do not support yielding generalized iterators, so we can only implement with a while loop for now.

Usage could be something like

local watcher = fs.watch(tmpdir)

task.wait(0.1) -- Give some time for the watcher to start

fs.writestringtofile(watched, "x")

local foundChange = false
local startTime = os.clock()

local event = watcher:next()
while event and os.clock() - startTime < 2 do
	if event.change or event.rename then
		foundChange = true
	end
	event = watcher:next()
end

assert.eq(foundChange, true)
assert.eq(event == nil, true)

watcher:close()

See full examples in examples/watch_directory.luau or fs.test.luau:watch_iterator_on_change

@annieetang annieetang changed the title implement fs.watch as a generator stdlib: re-implement fs.watch as an iterator Nov 24, 2025
@annieetang annieetang marked this pull request as ready for review November 25, 2025 18:28
@annieetang annieetang marked this pull request as draft November 25, 2025 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants