polly

Package Version Hex Docs

Polly is a portable, polling file-system watcher, supporting the Erlang and Node.js target.

While she may not be the fastest, she will help you in all situations where the fs package might not be suitable or supported.

gleam add polly@1
import gleam/io
import polly

pub fn main() {
  polly.new()
  |> polly.add_dir("src")
  |> polly.interval(1000)
  |> polly.watch(fn(event) {
    case event {
      Changed(path) -> io.println("CHANGED " <> path)
      Created(path) -> io.println("CREATED " <> path)
      Deleted(path) -> io.println("DELETED " <> path)
    }
  })
}

Performance

Compared to the fs package, Polly does not use specialised operating system APIs like inotify or ReadDirectoryChangesW. Instead, she recursively walks the specified directories every time the configured interval elapses, and compares the timestamps of all files with their previous timestamps that she needs to remember. Instead of having to keep around a single integer per directory, Polly stores all the file and directory names and their last timestamps, basically keeping around a “virtual” copy of the state of the filesystem.

This approach is fine if you’re planning to watch single files or configuration/source directories, but even just forgetting to filter out the build directory can significantly slow down Polly and increase her CPU and memory usage a lot. Never attempt to use Polly to watch the entire file system, or even just the home directory of the user.

Always make sure you can’t use other approaches first, and that you watch the smallest possible set of directories while having good filter and max_depth settings.

Search Document