Skip to content

Commit

Permalink
fix: make Atom.hasAnimation side-effect free
Browse files Browse the repository at this point in the history
Previously, Atom.hasAnimation would call Atom.update, which would in
turn call the Atom's change listeners if the Atom was in an animation.
However, This would cause infinite recursion loop in cases where
Atom.hasAnimation is called inside of or as a result of a change
listener.
This method simply does not need to call change listeners, as neither
its name nor its documentation implies it has side-effects, it doesn't
even lock the Atom as by design, TOCTOU errors can't happen on Atoms.
  • Loading branch information
zenith391 committed Nov 11, 2024
1 parent bbdeb32 commit cfdc1dc
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/data.zig
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,17 @@ pub fn Atom(comptime T: type) type {
}
}

/// Returns true if there is currently an animation playing.
pub fn hasAnimation(self: *Self) bool {
/// Returns true if there is currently an animation playing. This method doesn't lock the
/// Atom.
pub fn hasAnimation(self: *const Self) bool {
if (!isAnimatable) return false;
return self.update();
switch (self.value) {
.Animated => |animation| {
const now = std.time.Instant.now() catch return false;
return now.since(animation.start) < @as(u64, animation.duration) * std.time.ns_per_ms;
},
.Single => return false,
}
}

/// Starts an animation on the atom, from the current value to the `target` value. The
Expand Down

0 comments on commit cfdc1dc

Please sign in to comment.