zng/
fs_watcher.rs

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