polly

Types

A filesystem event.

Polly is careful when handing you events, and makes sure that you always get them in the right order, so if a dictionary with some files was created, you will get the dictionary event first, before any children.

pub type Event {
  Created(String)
  Changed(String)
  Deleted(String)
}

Constructors

  • Created(String)

    A new file or dictionary was created!

  • Changed(String)

    A file got modified!

  • Deleted(String)

    A file or dictionary got deleted :(

Some POSIX file system errors.

Only a handful of errors are handled by Polly, and everything else is collapsed into EUnknown.

pub type FsError {
  Eacces
  Enoent
  Enotdir
  EUnknown(String)
}

Constructors

  • Eacces
  • Enoent
  • Enotdir
  • EUnknown(String)

Polly uses the builder pattern to construct a watcher.

let options: Options = polly.new()
    |> polly.add_dir("src")
    |> polly.interval(3000)
    |> polly.max_depth(10)
    |> polly.filter(polly.default_filter)
pub opaque type Options

A polling file system watcher, built from options.

Watchers are automatically started, and can by stopped by calling stop.

pub opaque type Watcher(state)

Functions

pub fn add_dir(options: Options, path: String) -> Options

Tell Polly which directory to watch. If it does not exist, watch will return an error.

Paths are not expanded by default, so the paths reported by events and passed to the filter function will be prefixed with whatever you specified here.

pub fn default_filter(path: String) -> Bool

The default filter function, ignoring hidden files starting with a colon "."

pub fn filter(
  options: Options,
  by filter: fn(String) -> Bool,
) -> Options

Filter files using the given predicate.

Polly will ignore files and directories for which the predicate returns False completely, and any event happening for them or for a contained file of them will not get reported.

By default, all hidden files are ignored.

pub fn interval(options: Options, interval: Int) -> Options

Set the interval in-between file-system polls, in milliseconds.

This options is passed through to repeatedly.call.

pub fn max_depth(options: Options, max_depth: Int) -> Options

Limit the maximum depth that Polly will walk each directory.

There is no limit by default, but setting a limit might be a good to better control resource usage of the watcher.

pub fn new() -> Options

Start creating a new configuration using the default options.

By default, and interval of 1 second is set, and the default_filter is used.

pub fn stop(watcher: Watcher(a)) -> Nil

Stop this watcher.

On Erlang, pending events may still be reported. See the documentation of repeatedly.stop for more information.

pub fn watch(
  options: Options,
  with callback: fn(Event) -> a,
) -> Result(Watcher(Nil), #(String, FsError))

Tell Polly to start watching all the specified directories for changes.

The callback is called synchronously after collecting all change events since the last run. It is adviseable to move heavier cpu-bound tasks from this callback into their own processes/threads.

On Erlang, the callback will be called in the same process as the watcher runs in.

pub fn watch_with(
  options: Options,
  from initial: a,
  with callback: fn(a, Event) -> a,
) -> Result(Watcher(a), #(String, FsError))

Like watch, but similar to list.fold Polly will also keep some state around for you and pass it back on each invocation.

Search Document