-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
107 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// RepeatingTimer.swift | ||
// Pods | ||
// | ||
// Created by jeromy on 4/27/22. | ||
// | ||
import Foundation | ||
|
||
/// RepeatingTimer mimics the API of DispatchSourceTimer but in a way that prevents | ||
/// crashes that occur from calling resume multiple times on a timer that is | ||
/// already resumed (noted by https://github.com/SiftScience/sift-ios/issues/52 | ||
class RepeatingTimer { | ||
|
||
let timeInterval: TimeInterval | ||
|
||
init(timeInterval: TimeInterval) { | ||
self.timeInterval = timeInterval | ||
} | ||
|
||
private lazy var timer: DispatchSourceTimer = { | ||
let t = DispatchSource.makeTimerSource() | ||
t.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval) | ||
t.setEventHandler(handler: { [weak self] in | ||
self?.eventHandler?() | ||
}) | ||
return t | ||
}() | ||
|
||
var eventHandler: (() -> Void)? | ||
|
||
private enum State { | ||
case suspended | ||
case resumed | ||
} | ||
|
||
private var state: State = .suspended | ||
|
||
deinit { | ||
timer.setEventHandler {} | ||
timer.cancel() | ||
/* | ||
If the timer is suspended, calling cancel without resuming | ||
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902 | ||
*/ | ||
resume() | ||
eventHandler = nil | ||
} | ||
|
||
func resume() { | ||
if state == .resumed { | ||
return | ||
} | ||
state = .resumed | ||
timer.resume() | ||
} | ||
|
||
func suspend() { | ||
if state == .suspended { | ||
return | ||
} | ||
state = .suspended | ||
timer.suspend() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.