A Swift throttler that runs a closure only if specific conditions are met.
enum Key: String, CaseIterable {
case showTutorial
}
Only(.once(Key.showTutorial)) { [weak self] in
self?.showTutorial()
}
self?.showTutorial()
will only run once and never again until the application is uninstalled or the backing storage is cleared. For each key a record is created prefixed by com.execute.only.
(configurable)
The default backing storage is UserDefaults.standard
.
To reset the storage for a key, simply remove the record.
For UserDefaults.standard
:
UserDefaults.standard.removeObject(forKey: "\(com.execute.only).\(Key.showTutorial)")
enum OnlyFrequency<T: OnlyKey> where T.RawValue == String {
case once(T)
case oncePerSession(T)
case ifTimePassed(T, DispatchTimeInterval)
case `if`(() -> Bool)
case every(T, times: Int)
}