Skip to content
/ Only Public

Swift package that implements a throttler for running code only when once, or every `x` times.

Notifications You must be signed in to change notification settings

a-sarris/Only

Repository files navigation

Run unit tests

Only

A Swift throttler that runs a closure only if specific conditions are met.

Sample Usage

Create a key that serves as an identifier for the closure:

enum Key: String, CaseIterable {
    case showTutorial
}

At the appropriate place in your application's lifecycle, add the throttler:

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)")

Available options are described by OnlyFrequency:

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)
}

About

Swift package that implements a throttler for running code only when once, or every `x` times.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages