All Projects → soffes → Ratelimit

soffes / Ratelimit

Licence: mit
Simple utility for only executing code every so often.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Ratelimit

Rome
Carthage cache for S3, Minio, Ceph, Google Storage, Artifactory and many others
Stars: ✭ 724 (-21.13%)
Mutual labels:  tvos, watchos, carthage
Datez
📆 Breeze through Date, DateComponents, and TimeInterval with Swift!
Stars: ✭ 254 (-72.33%)
Mutual labels:  tvos, watchos, carthage
TermiNetwork
🌏 A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.
Stars: ✭ 80 (-91.29%)
Mutual labels:  tvos, watchos, carthage
Swiftframeworktemplate
A template for new Swift iOS / macOS / tvOS / watchOS Framework project ready with travis-ci, cocoapods, Carthage, SwiftPM and a Readme file
Stars: ✭ 527 (-42.59%)
Mutual labels:  tvos, watchos, carthage
Crypto
Swift CommonCrypto wrapper
Stars: ✭ 328 (-64.27%)
Mutual labels:  tvos, watchos, carthage
Columbus
A feature-rich country picker for iOS, tvOS and watchOS.
Stars: ✭ 23 (-97.49%)
Mutual labels:  tvos, watchos, carthage
clevertap-ios-sdk
CleverTap iOS SDK
Stars: ✭ 39 (-95.75%)
Mutual labels:  tvos, watchos, carthage
Cache
Swift caching library
Stars: ✭ 210 (-77.12%)
Mutual labels:  tvos, watchos, carthage
Functionkit
A framework for functional types and operations designed to fit naturally into Swift.
Stars: ✭ 302 (-67.1%)
Mutual labels:  tvos, watchos, carthage
Web3.swift
A pure swift Ethereum Web3 library
Stars: ✭ 295 (-67.86%)
Mutual labels:  tvos, watchos, carthage
Mechanica
A cross-platform library of Swift utils to ease your iOS | macOS | watchOS | tvOS and Linux development.
Stars: ✭ 27 (-97.06%)
Mutual labels:  tvos, watchos, carthage
Json
Micro framework for easily parsing JSON in Swift with rich error messages in less than 100 lines of code
Stars: ✭ 395 (-56.97%)
Mutual labels:  tvos, watchos, carthage
Dots
Lightweight Concurrent Networking Framework
Stars: ✭ 35 (-96.19%)
Mutual labels:  tvos, watchos, carthage
BlockiesSwift
Unique blocky identicons generator for Swift
Stars: ✭ 53 (-94.23%)
Mutual labels:  tvos, watchos, carthage
Iso8601
ISO8601 date parser and writer
Stars: ✭ 213 (-76.8%)
Mutual labels:  tvos, watchos, carthage
SwiftVer
Easily Manage Versioning in MacOS, iOS, watchOS, and tvOS projects.
Stars: ✭ 23 (-97.49%)
Mutual labels:  tvos, watchos, carthage
Cocoalumberjack
A fast & simple, yet powerful & flexible logging framework for Mac and iOS
Stars: ✭ 12,584 (+1270.81%)
Mutual labels:  tvos, watchos, carthage
L10n Swift
Localization of the application with ability to change language "on the fly" and support for plural form in any language.
Stars: ✭ 177 (-80.72%)
Mutual labels:  tvos, watchos, carthage
X
Easier cross platform Mac & iOS development with Swift
Stars: ✭ 270 (-70.59%)
Mutual labels:  tvos, watchos, carthage
Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+304.14%)
Mutual labels:  tvos, watchos, carthage

Rate Limit

Version Build Status Swift Version Carthage compatible CocoaPods compatible

Simple utility for only executing code every so often.

This will only execute the block passed for a given name if the last time it was called is greater than limit or it has never been called.

This is really handy for refreshing stuff in viewDidAppear: but preventing it from happening a ton if it was just refreshed.

Rate Limit is fully thread-safe. Released under the MIT license.

Usage

We’ll start out with a TimedLimiter:

// Initialize with a limit of 5, so you can only use this once every 5 seconds.
let refreshTimeline = TimedLimiter(limit: 5)

// Call the work you want to limit by passing a block to the execute method.
refreshTimeline.execute {
    // Do some work that runs a maximum of once per 5 seconds.
}

Limiters aren’t persisted across application launches.

Synchronous Limiters

TimedLimiter conforms to the SyncLimiter protocol. This means that the block you pass to execute will be called synchronously on the queue you called it from if it should fire. TimedLimiter uses time to limit.

CountedLimiter is also included. This works by taking a limit as a UInt for the maximum number of times to run the block.

The SyncLimiter protocol has a really neat extension that let’s you do things like this:

let funFactLimiter = CountedLimiter(limit: 2)
let funFact = funFactLimiter.execute { () -> String in
    // Do real things to get a fun fact from a list
    return "Hi"
}

Now funFact is a String?. It’s just an optional of whatever you return from the block. The returned value will be nil if the block didn’t run.

You can of course make your own SyncLimiters too!

Asynchronous Limiter

One AsyncLimiter is included. You can make your own too. The included async limiter is DebouncedLimiter. This is perfect for making network requests as a user types or other tasks that respond to very frequent events.

The interface is slightly different:

let searchLimiter = DebouncedLimiter(limit: 1, block: performSearch)

func textDidChange() {
  searchLimiter.execute()
}

You would have to setup the limiter in an initializer since it references an instance method, but you get the idea. The block will be called at most once per second in this configuration.

Pretty easy!

Open up the included Xcode project for an example app and tests.

Installation

Carthage

Carthage is the recommended way to install Rate Limit. Add the following to your Cartfile:

github "soffes/RateLimit"

CocoaPods

Add the following to your Podfile:

pod "RateLimit"

Then run pod install.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].