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(path: String)
Changed(path: String)
Deleted(path: String)
Error(path: String, reason: FileError)
}
Constructors
-
Created(path: String)
A new file or dictionary was created!
-
Changed(path: String)
A file got modified!
-
Deleted(path: String)
A file or dictionary got deleted :(
-
Error(path: String, reason: FileError)
An unexpected error happened :(
Polly treats
Enoent
andEacces
errors as if the file got deleted, and will therefore never pass those to you.
A type that Polly uses to tag her options, indicating that you have
called add_dir
or add_file
.
pub type HasWatchedDirs
A type that Polly uses to tag her options, indicating that you haven’t
called add_dir
or add_file
yet.
pub type NoWatchedDirs
Polly uses the builder pattern to construct a watcher.
The has_watched_dirs
phantom type variable helps Polly keep track of
wether or not you’ve called add_dir
or add_file
yet. That way, it will
be a type error if you try to watch nothing!
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(has_watched_dirs)
Functions
pub fn add_dir(
options: Options(a),
path: String,
) -> Options(HasWatchedDirs)
Tell Polly which directory to watch. If it does not exist, watch
will return an error.
If the directory goes away after watching has started, Polly will continue to check on it to see if it came back.
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 add_file(
options: Options(a),
path: String,
) -> Options(HasWatchedDirs)
Tell Polly to watch a single file.
Polly doesn’t care if you tell her to watch a file or directory, but using this function makes your intent clearer!
pub fn default_filter(type: FileType, path: String) -> Bool
The default filter function, ignoring hidden files starting with a colon "."
pub fn filter(
options: Options(a),
by filter: fn(FileType, String) -> Bool,
) -> Options(a)
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(a), interval: Int) -> Options(a)
Set the interval in-between file-system polls, in milliseconds.
This is the time that Polly rests between calls, so if scanning your directory tree takes 100ms, and you configured 1000ms here, the total time between calls will roughly be 1100ms.
Doing it this way makes sure that Polly doesn’t stumble over herself.
pub fn max_depth(
options: Options(a),
max_depth: Int,
) -> Options(a)
Limit the maximum depth that Polly will walk each directory.
A limit of 0
would mean that Polly only watches the specified list of
files or directories. A limit of 1
means that she will also look at the
files inside the given directories, but not at any nested directories.
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(NoWatchedDirs)
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) -> Nil
Stop this watcher.
If Polly currently scans your directories, she might not hear you right away and may still report events for one run, after which she will stop.
pub fn watch(
options: Options(HasWatchedDirs),
with emit: fn(Event) -> a,
) -> Result(Watcher, List(#(String, FileError)))
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(HasWatchedDirs),
from initial: a,
with emit: fn(a, Event) -> a,
) -> Result(Watcher, List(#(String, FileError)))
Like watch
, but similar to list.fold
Polly will also keep some state
around for you and pass it back on each invocation.