Giter Site home page Giter Site logo

bccoalescing's Introduction

BCCoalescing

This is a simple coalescing object that allows multiple observers to receive callbacks from a single source. It can be used to reproduce behavior you might otherwise get from Notification Center but without the hassle of tightly coupling your code to it.

Installation

Using Carthage add

github "bromas/bccoalescing"

to your Cartfile

How it works

It's extremely simple and for that reason you might just want to look at the code. However, the one sentence explanation is that you funnel callbacks into the coalescer in a 'perform operation' block, the coalescer performs any interpolation that you want to happen on the data, and then the callbacks are performed on each registered observer.

When to use it

You can coalesce all kinds of fun things. Think image downloads, disk access, location services, basically anything that is asyncronous and might be requested by multiple objects simultaneously.

Examples

Reading the unit tests should provide all of the information you need on using the provided objects, but here is a glimpse of the API.

__weak typeof(self) *weakself = self;
  [self.imageCoalescer addCallbackWithProgress:^(CGFloat percent) {
  } andCompletion:^(id result, NSURLResponse *response, NSError *error) {
      NSLog(@"completed!");
  } forIdentifier:@"uniqueIdentifier" withRequestPerformanceBlock:^{
      [weakself.session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"path to some image"]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        [weakself.imageCoalescer identifier:@"uniqueIdentifier" completedWithData:data response:response andError:error];
    }];
  }];

This framework is consumed in a sample project available on github.

Full Swift Example

Initialize:

let coalesce = BCCoalesce()
var userIDIconMap: [String: UIImage] = [:]
init () {
  coalesce.shouldPerformCallbacksOnMainThread = true
  coalesce.resultsInterpolator = { data in
    let target = data as! NSData
    return UIImage(data: target)
  }
}

Add callbacks:

func imageForUser(forUser: AppNetUser, completion: (UIImage?) -> Void) -> Void {
	self.coalesce.addCallbacksWithProgress({ (_) -> Void in }, andCompletion: { (data, _, error) -> Void in
		self.userIDIconMap[forUser.userID] = data as? UIImage
		completion(self.userIDIconMap[forUser.userID])
	}, forIdentifier: forUser.userID) { () -> Void in
		Alamofire.request(.GET, forUser.avatarUrl).response { (_, _, data, error) -> Void in
			self.coalesce.identifier(forUser.userID, completedWithData: data as? NSData, andError: error)
  		}
  	}
}

bccoalescing's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

niaorenhuaduo

bccoalescing's Issues

investigate unregister block api refactoring

Having the unregister block simply inject the identifier of the key with no further observers means that consumers need to manage a set of pointers to the operations existing under those keys in order to shut them down. ๐Ÿ‘Ž

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.