Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/vertigo/src/computed/dependencies/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub fn get_dependencies() -> Rc<Dependencies> {
}

impl Dependencies {
pub(crate) fn is_refreshing(&self) -> bool {
self.transaction_state.is_refreshing()
}

pub fn transaction<R, F: FnOnce(&Context) -> R>(&self, func: F) -> R {
self.transaction_state.up();

Expand Down
5 changes: 5 additions & 0 deletions crates/vertigo/src/computed/dependencies/transaction_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ impl TransactionState {
}
}

/// True while the graph is running client/computed refresh (after the outer transaction commits).
pub fn is_refreshing(&self) -> bool {
self.state.map(|state| matches!(state, State::Refreshing))
}

pub fn up(&self) {
let TransactionState { state } = self;

Expand Down
18 changes: 16 additions & 2 deletions crates/vertigo/src/computed/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,29 @@ impl<T: Clone + PartialEq + 'static> Value<T> {
}

pub fn change(&self, change_fn: impl FnOnce(&mut T)) {
get_dependencies().transaction(|ctx| {
let deps = get_dependencies();
if deps.is_refreshing() {
log::error!(
"Ignoring Value::change while the dependency graph is refreshing; the change was not applied"
);
return;
}
deps.transaction(|ctx| {
let mut value = self.get(ctx);
change_fn(&mut value);
self.set(value);
});
}

pub fn set(&self, value: T) {
get_dependencies().transaction(|_| {
let deps = get_dependencies();
if deps.is_refreshing() {
log::error!(
"Ignoring Value::set while the dependency graph is refreshing (would corrupt scheduling); the write was not applied"
);
return;
}
deps.transaction(|_| {
let need_refresh = self.inner.set(value);
if need_refresh {
get_dependencies().report_set(self.inner.id);
Expand Down
Loading