zng/fs_watcher.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
//! File system watcher service and other types.
//!
//! The [`WATCHER`] service can be used to get notifications when a file or directory is modified. It also provides
//! ways to bind a file to a variable, automatically synchronizing both.
//!
//! The example below binds the current content of a text file to at text variable using [`WATCHER.read`](WATCHER::read).
//! Any external change made to the text file updates the UI text.
//!
//! ```
//! use zng::{prelude::*, fs_watcher::WATCHER};
//!
//! # fn main() { }
//! # fn demo() {
//! # let _scope = APP.defaults();
//! # let _ =
//! Text!(WATCHER.read("dump.log", Txt::from(""), |f| f.ok()?.text().ok()))
//! # ; }
//! ```
//!
//! The next example created a read-write binding with the text file, any external change made to the text file updates the
//! `TextInput!` and any change made using the `TextInput!` updates the file contents.
//!
//! ```
//! use zng::{prelude::*, fs_watcher::WATCHER};
//!
//! # fn main() { }
//! # fn demo() {
//! # let _scope = APP.defaults();
//! # let _ =
//! TextInput!(zng::fs_watcher::WATCHER.sync(
//! "dump.log",
//! // initial value
//! Txt::from(""),
//! // read, only updates txt if returns Some
//! |f| f.ok()?.text().ok(),
//! // write, only change file if commit called.
//! |txt, f| {
//! if let Ok(mut f) = f {
//! if f.write_text(&txt).is_ok() {
//! // replace actual file with temp that was successfully written.
//! let _ = f.commit();
//! } else {
//! f.cancel();
//! }
//! }
//! },
//! ))
//! # ; }
//! ```
//!
//! The [`WATCHER`] service abstracts away most of the headache of interacting with the file system. This service
//! is used internally by the implementations of [`CONFIG`] and [`L10N`].
//!
//! [`CONFIG`]: crate::config::CONFIG
//! [`L10N`]: crate::l10n::L10N
//!
//! # Full API
//!
//! See [`zng_ext_fs_watcher`] for the full watcher API.
pub use zng_ext_fs_watcher::{
fs_event, FsChange, FsChangeNote, FsChangeNoteHandle, FsChangesArgs, WatchFile, WatcherHandle, WatcherReadStatus, WatcherSyncStatus,
WriteFile, FS_CHANGES_EVENT, WATCHER,
};