GitXplorerGitXplorer
s

RateLimit

public
913 stars
56 forks
5 issues

Commits

List of commits on branch master.
Unverified
dfda80c36d0309f4d3e299c8d418c21d5a51ddc5

Version 2.1.2

ssoffes committed 7 years ago
Verified
512b1999594d84fc7a9b9d47e068c23c1e47734c

Merge pull request #36 from piemonte/master

ssoffes committed 7 years ago
Unverified
dbff235292265c8927383db2e350ba703785004a

travis, update xcode version

ppiemonte committed 7 years ago
Unverified
fc12f6ef2591fffcd10837fca47904cecbe22f8b

update readme swift version

ppiemonte committed 7 years ago
Unverified
c2296b61eb71060fd14597420fa98be302a179ac

update to Swift 4

ppiemonte committed 7 years ago
Unverified
43924e605c8af08d1c64c202f08f5eef54f31754

Merge pull request #29 from basememara/safe-api

ssoffes committed 8 years ago

README

The README file for this repository.

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.