pub struct MruList<T: MruEntry> { /* private fields */ }Expand description
A persisted MRU list backed by PersistedListModel<T>.
Cheap to clone (Rc-shared internally). The reactive
ListModel<T> returned by model() is the same
handle the persistence bridge observes.
Implementations§
Source§impl<T: MruEntry> MruList<T>
impl<T: MruEntry> MruList<T>
Sourcepub fn open(
paths: &AppPaths,
name: &str,
max_items: usize,
) -> Result<Self, SettingsFileError>
pub fn open( paths: &AppPaths, name: &str, max_items: usize, ) -> Result<Self, SettingsFileError>
Open at <paths.config_dir()>/<name>.toml with the default debounce window.
Creates the file (and any missing parent directories) if it does not
yet exist. Use open_with_delay to override
the debounce in tests.
Sourcepub fn open_with_delay(
paths: &AppPaths,
name: &str,
max_items: usize,
delay: Duration,
) -> Result<Self, SettingsFileError>
pub fn open_with_delay( paths: &AppPaths, name: &str, max_items: usize, delay: Duration, ) -> Result<Self, SettingsFileError>
Open at <paths.config_dir()>/<name>.toml with a custom debounce window.
Pass Duration::ZERO in tests to flush
every mutation synchronously.
Sourcepub fn open_at(
path: PathBuf,
max_items: usize,
delay: Duration,
) -> Result<Self, SettingsFileError>
pub fn open_at( path: PathBuf, max_items: usize, delay: Duration, ) -> Result<Self, SettingsFileError>
Sourcepub fn model(&self) -> &ListModel<T>
pub fn model(&self) -> &ListModel<T>
The underlying reactive list; bind to UI widgets via clones of this handle.
Read-only for mutation purposes. Use add,
remove, touch,
set_pinned, clear to mutate —
those are what enqueue the matching persisted op. Mutating the
returned ListModel directly updates what’s on screen but is
never written to disk.
Sourcepub fn max_items(&self) -> usize
pub fn max_items(&self) -> usize
Returns the maximum number of unpinned entries kept in the list.
Pinned entries do not count toward this cap and are never evicted automatically.
Sourcepub fn add(&self, entry: T)
pub fn add(&self, entry: T)
Insert entry at the front, deduping by entry.key().
T::touch is invoked before insertion, so the freshly-added
entry reflects “now”. If a previously-pinned entry is re-added
without pinned, the pin state is preserved.
Sourcepub fn remove<Q>(&self, key: &Q)
pub fn remove<Q>(&self, key: &Q)
Remove the entry whose key matches, then schedule a debounced flush.
No-op when no entry with that key is present. Generic over Q so
callers can pass a borrowed form of the key (e.g. &Path when
T::Key = PathBuf, &str when T::Key = String) without having
to allocate an owned key just to look one up.
Sourcepub fn touch<Q>(&self, key: &Q)
pub fn touch<Q>(&self, key: &Q)
Mark the entry whose key matches as freshly used by calling
MruEntry::touch on a clone of it, then write it back and
schedule a debounced flush. No-op when no entry matches.
Sourcepub fn set_pinned<Q>(&self, key: &Q, pinned: bool)
pub fn set_pinned<Q>(&self, key: &Q, pinned: bool)
Set the pin flag of the entry whose key matches to exactly
pinned (idempotent — unlike a toggle, replaying this against an
already-applied peer change does not flip it back). No-op when no
entry matches.
Sourcepub fn is_pinned<Q>(&self, key: &Q) -> bool
pub fn is_pinned<Q>(&self, key: &Q) -> bool
Is the entry with this key currently pinned? false when no entry
matches.
The counterpart set_pinned deliberately takes the
desired value rather than toggling, because a toggle is not idempotent:
replayed against a peer process’s already-applied toggle it would flip
the value straight back, inverting their change. A pin button still
needs to toggle, though — so read the current value here and pass its
negation:
let pinned = mru.is_pinned(path);
mru.set_pinned(path, !pinned);Sourcepub fn flush_now(&self) -> Result<(), SettingsFileError>
pub fn flush_now(&self) -> Result<(), SettingsFileError>
Write the list to disk synchronously, bypassing the debounce window.
Useful at app shutdown or at the end of a test to guarantee the file reflects the in-memory state before the process exits.
Trait Implementations§
Source§impl<T> Reloadable for MruList<T>
impl<T> Reloadable for MruList<T>
Source§fn path(&self) -> &Path
fn path(&self) -> &Path
Reloadable handle.