pub struct Transaction<'a, D: Clone, X: Clone = ()> { /* private fields */ }Expand description
A compound edit in progress (Store::begin_transaction); derefs to the Store.
While it is open the store’s history is a scratch one holding only the transaction’s own
steps (so undo inside it goes back no further than its start, and limits never push out an
older step); the real history is set aside untouched. Transaction::commit puts it back
and records one step label from the state at the start to the current one, or nothing if
the revision is the one at the start. Dropping it uncommitted puts back the state at the start
exactly: document, extra state, revision, history, merge key, saved revision.
Transactions nest: an inner one commits as one step into the outer one’s scratch history (folded again by the outer commit) and rolls back only its own edits (a savepoint).
Implementations§
Source§impl<D: Clone, X: Clone> Transaction<'_, D, X>
impl<D: Clone, X: Clone> Transaction<'_, D, X>
Methods from Deref<Target = Store<D, X>>§
Sourcepub fn snapshot(&self) -> Arc<D> ⓘ
pub fn snapshot(&self) -> Arc<D> ⓘ
The current document, to keep or send to another thread (an Arc clone).
pub fn revision(&self) -> Revision
pub fn extra(&self) -> &X
pub fn history(&self) -> &History<D, X>
Sourcepub fn history_mut(&mut self) -> &mut History<D, X>
pub fn history_mut(&mut self) -> &mut History<D, X>
The history, to change its limits or squash steps.
Sourcepub fn edit<R>(&mut self, label: &str, f: impl FnOnce(&mut D) -> R) -> R
pub fn edit<R>(&mut self, label: &str, f: impl FnOnce(&mut D) -> R) -> R
Apply an undoable edit called label.
Sourcepub fn try_edit<R, E>(
&mut self,
label: &str,
f: impl FnOnce(&mut D) -> Result<R, E>,
) -> Result<R, E>
pub fn try_edit<R, E>( &mut self, label: &str, f: impl FnOnce(&mut D) -> Result<R, E>, ) -> Result<R, E>
Apply an edit that may fail; on error (or a panic in f) nothing changes.
Sourcepub fn edit_merged<R>(
&mut self,
label: &str,
merge_key: &str,
f: impl FnOnce(&mut D) -> R,
) -> R
pub fn edit_merged<R>( &mut self, label: &str, merge_key: &str, f: impl FnOnce(&mut D) -> R, ) -> R
Store::edit that folds into the previous step when that step has the same label and
was made with the same merge_key with nothing in between (one undo step per drag).
Sourcepub fn try_edit_with<R, E>(
&mut self,
label: &str,
merge_key: Option<&str>,
f: impl FnOnce(&mut D, &mut X) -> Result<R, E>,
) -> Result<R, E>
pub fn try_edit_with<R, E>( &mut self, label: &str, merge_key: Option<&str>, f: impl FnOnce(&mut D, &mut X) -> Result<R, E>, ) -> Result<R, E>
The general edit: optional merge key, access to the extra state, may fail. On error (or a
panic in f) the document, the extra state, the revision and the history are unchanged.
Sourcepub fn commit(&mut self, label: &str, merge_key: Option<&str>, doc: Arc<D>)
pub fn commit(&mut self, label: &str, merge_key: Option<&str>, doc: Arc<D>)
Make doc, built elsewhere (a command’s working copy, a job’s result), the new state as
one undo step label, without copying it. merge_key as in Store::edit_merged.
Committing the current snapshot itself (Arc::ptr_eq) changes nothing.
Sourcepub fn transaction<T, E>(
&mut self,
label: &str,
f: impl FnOnce(&mut Store<D, X>) -> Result<T, E>,
) -> Result<T, E>
pub fn transaction<T, E>( &mut self, label: &str, f: impl FnOnce(&mut Store<D, X>) -> Result<T, E>, ) -> Result<T, E>
Run f, which may make any number of edits, as one undo step called label.
On Ok the edits fold into one step (none when they changed nothing); the steps before
the transaction are untouched and the redo line is dropped only if something changed. On
Err, or a panic in f, the document, extra state, revision, history (redo line and merge
key included) and saved state are put back as they were. See Transaction.
Sourcepub fn begin_transaction(&mut self, label: &str) -> Transaction<'_, D, X>
pub fn begin_transaction(&mut self, label: &str) -> Transaction<'_, D, X>
Start a Transaction called label: edit through it, then Transaction::commit;
dropping it without committing (an early return, a panic) rolls everything back.
Sourcepub fn redo_branch(&mut self, index: usize) -> Option<String>
pub fn redo_branch(&mut self, index: usize) -> Option<String>
Redo into branch index of History::branches (it must grow from the current state).
pub fn can_undo(&self) -> bool
pub fn can_redo(&self) -> bool
Sourcepub fn replace(&mut self, doc: D, extra: X)
pub fn replace(&mut self, doc: D, extra: X)
Replace the whole document (New, Open, Revert): clears the history, counts as saved.
Sourcepub fn mark_saved(&mut self)
pub fn mark_saved(&mut self)
The current state was saved.
Sourcepub fn mark_saved_at(&mut self, revision: Revision)
pub fn mark_saved_at(&mut self, revision: Revision)
Record that the state with revision was saved (a background save of an older snapshot).
pub fn saved_revision(&self) -> Revision
Sourcepub fn subscribe_snapshots(&mut self) -> Arc<SnapshotCell<D>> ⓘ
pub fn subscribe_snapshots(&mut self) -> Arc<SnapshotCell<D>> ⓘ
A cell other threads read the latest snapshot from without locking; the store publishes every change to it. The same cell is returned on every call.