Giter Site home page Giter Site logo

ratelimit's Introduction

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.

ratelimit's People

Contributors

basememara avatar calebd avatar carlj avatar hyperspacemark avatar javisoto avatar mz2 avatar piemonte avatar soffes avatar streeter avatar sw0906 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.