Skip to content

Commit

Permalink
Horrible hack to cancel in-flight Extra execution
Browse files Browse the repository at this point in the history
  • Loading branch information
PimCoumans committed Apr 16, 2024
1 parent 6d443c1 commit 003b57b
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Sources/AnimationPlanner/Animations/Extra.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import UIKit
/// Performs the provided handler in between your actual animations.
/// Typically used for setting up state before an animation or creating side-effects like haptic feedback.
public struct Extra: SequenceAnimatable, GroupAnimatable {
private let id = UUID()
public let duration: TimeInterval = 0

public var perform: () -> Void
Expand All @@ -11,12 +12,21 @@ public struct Extra: SequenceAnimatable, GroupAnimatable {
}
}

extension Extra {
private static var allowedAnimations: Set<UUID> = []
}

extension Extra: PerformsAnimations {
public func animate(delay leadingDelay: TimeInterval, completion: ((Bool) -> Void)?) {
let timing = timingParameters(leadingDelay: leadingDelay)

Self.allowedAnimations.insert(id)
let animation: () -> Void = {
guard Self.allowedAnimations.contains(id) else {
completion?(false)
return
}
self.perform()
Self.allowedAnimations.remove(id)
completion?(true)
}
guard timing.delay > 0 else {
Expand All @@ -29,8 +39,6 @@ extension Extra: PerformsAnimations {
}

public func stop() {
// No-op:
// 1. Either fires immediately so can’t be stopped
// 2. Is invoked with a delay and queue can’t be stopped
Self.allowedAnimations.remove(id)
}
}

0 comments on commit 003b57b

Please sign in to comment.