This is an example application that shows an example integration with Salsa, an incremental computation library, and notify, a cross-platform file-watcher API written in Rust. The application uses on-demand (or lazy) queries to read from the filesystem and invalidate paths on change. It is an expanded version of the example in the salsa book with additional changes to make it work with v0.15.2.
In the main function we create a sender and a receiver channel to bootstrap our file watcher with.
Then we bootstrap our watcher using the
Sender<DebouncedEvent>. It is important that we wrap this in Arc and Mutex because otherwise the mutation requirement will bubble up to our read query, which can't handle it (it requires the first argument to be
We bootstrap the Salsa database with the watcher and our storage, choosing to use the default value for the salsa storage.
Then the main function sets up a file to watch that comes from "somewhere else" (exercise left to the reader).
We use the
read query to read the file in and set up the watch on that file
and finally we loop forever, pulling file watcher event values off the
Receiver<DebouncedEvent>. Note that we've specified two files. One is in the
test directory named
something.txt and is set up earlier. The next is only set up after the original
something.txt is changed. This shows usage of the
read query again.
In the rest of the program, we specify that our
VfsDatabase must also implement the Supertraits
FileWatcher (we own
FileWatcher and salsa owns the
FileWatcher trait requires the implementation of a
watch function and a
did_change_file function. Only
did_change_file requires a mutable reference to the
read is the center of this example. We set up a query that takes a path as a key and returns a
String. The salsa runtime thinks this is a
HIGH durability action by default, so we override that with a
.watch the relevant path, which triggers the watcher, and return the contents of the file.
The database types also need to be set up. This is where we specify the
Arc<Mutex<>> that allows us to implement
and finally, we implement
FileWatcher, which pulls the watcher out of the
lock and watches the additional path.
did_change_file is used as a mechanism to invalidate the path key for the
ReadQuery we set up earlier. We need a mutable db for this.