Skip to content
FutureBlitz

    Install

    Import std.io.watcher

    std.io.watcher

    Watching files and folders for changes.

    std.io's file watcher: what happens to a file, or to what's in a folder, as the OS tells libuv, which a FileWatcher queues for the program to take in turn, waiting for it, awaiting it, or not.

    Examples

    Local folder = Try TempDir()
    Local watcher = Try FileWatcher(folder.Path)
    Try File.WriteText(folder.Path / "a.txt", "new")
    // The OS tells of a change soon after it; TryNext doesn't wait for it, as Next would.
    Local event = watcher.TryNext()
    Local tries = 0
    While event == Null && tries < 500
        Sleep(10.Milliseconds)
        event = watcher.TryNext()
        tries += 1
    End While
    
    Expect(event != Null)
    watcher.Close()
    Try folder.Close()

    std/io/watcher.fb

    Types

    FileEventKind enum

    Public Enum FileEventKind

    What happened at a path, which a watcher tells by the names it knows are there, since OSes call these differently.

    std/io/watcher.fb, line 45

    • CREATED enum member #

      Something came there, as the new name of a rename does.

    • CHANGED enum member #

      It changed, as a file that another is renamed over does.

    • REMOVED enum member #

      It went, as the old name of a rename does.

    FileEvent struct

    Public Struct FileEvent

    What happened, and where.

    std/io/watcher.fb, line 55

    • Public ReadOnly Field Kind: FileEventKind field #

      What happened.

    • Public ReadOnly Field Path: Path field #

      The file or folder it happened to.

    FileWatcher class

    Public Class FileWatcher

    Watches a file, or what's in a folder, but not the folders in it, from when it's made until it's closed or its last reference goes. An OS can tell of one change more than once, as when a write comes in parts, and of a change it makes itself, as of a folder's times. A watched file or folder that's removed may go unwatched on some OSes, even once it's back.

    Examples

    Local watcher = Try FileWatcher(Path("assets"))
    Local event = watcher.Next()
    While event != Null
        Print("{event.Kind}: {event.Path}")
        event = watcher.Next()
    End While

    std/io/watcher.fb, line 82

    • Public ReadOnly Field Path: Path field #

      The file or folder it watches.

    • Public New(path: Path) Throws IOError constructor #

      Starts watching what's at the path, which has to be there.

    • Public Method Next(): FileEvent? method #

      The next event, waiting for it, or Null once it's closed. Nothing else runs on this thread meanwhile, so one that nothing comes to waits forever.

    • Public Method TryNext(): FileEvent? method #

      The next event if one's come, or Null.

    • Public Async Method NextAsync(): FileEvent? method #

      Next's Async twin, which waits on the main thread instead of blocking it.

    • Public Method Close() method #

      Stops watching, dropping the events not taken, so what waits for one, and each Next, gets Null. A second Close does nothing.

    Report an issue with this page or with FutureBlitz on GitHub.