Latest 1.1.0
Homepage https://github.com/dechhay/Throttler
License MIT
Platforms ios 11.0
Authors

Throttler is a component object you can use for regulating the rate of performing a process.

Processes can easily be defined using a closure. You can also customize the delay time, and even change its dispatch queue.

Why not just use RxSwift?

This library was intended as a small component with simplicity in mind. You do not need to learn complicated syntaxes. Functions were designed to have few inputs, making your code simpler and cleaner.

How do I use it?

By creating a Throttler instance, you can define a process using a closure, and customize the timing if desired. After setting up, simply call throttle() on the Throttler instance, it’s that easy!

Example

Lets say you want to automatically send a search request as you type into a UISearchBar, but don’t want to send a request for every key pressed. Because you are a responsible developer, you want to regulate the search. This can be done easily using the Throttler as shown below.

func commonInit() {
    let delay: TimeInterval = 0.5
    let dispatchQueue = DispatchQueue.main
    let dispatchBlock = { [weak self] in
        guard let strongSelf = self,
              let searchString = strongSelf.searchBar.text else {
                  return
        }
        strongSelf.searchService.search(searchString)// send search request
     }
    throttler = Throttler(delay: delay, dispatchQueue: dispatchQueue, dispatchBlock: dispatchBlock)
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    // We don't want to send a search request for every character typed.
    // But using the throttler, it will fire off only after the delay time.
    throttler.throttle()
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    // But we can also fire off immediately
    throttler.fire()
}

Latest podspec

{
    "name": "Throttler",
    "version": "1.1.0",
    "summary": "A component you can use for regulating the rate of performing a process.",
    "description": "Throttler is a component you can use for regulating the ratenof performing a process.  Processes can easily be defined bynproviding a closure.  Processes are dispatched asychronouslynto the provided queue.nnThrottler can also be customized to change its delay time andndipatch queue.",
    "homepage": "https://github.com/dechhay/Throttler",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Dennis Chhay": "[email protected]"
    },
    "platforms": {
        "ios": "11.0"
    },
    "source": {
        "git": "https://github.com/dechhay/Throttler.git",
        "tag": "1.1.0"
    },
    "source_files": "Throttler/Code/*.swift",
    "swift_version": "4.0"
}

Pin It on Pinterest

Share This