Node.js inspired EventEmitter for objective c.
Enable NSObject connect to a specific Event Emitter
- (void)on:(NSString*)event sender:(id)sender notify:(EventEmitterNotifyCallback)callback;
- (void)on:(NSString*)event sender:(id)sender callback:(EventEmitterDefaultCallback)callback;
- (void)on:(NSString*)event sender:(id)sender array:(EventEmitterArrayCallback)callback;
- (void)once:(NSString*)event sender:(id)sender notify:(EventEmitterNotifyCallback)callback;
- (void)once:(NSString*)event sender:(id)sender callback:(EventEmitterDefaultCallback)callback;
- (void)once:(NSString*)event sender:(id)sender array:(EventEmitterArrayCallback)callback;
Copy the EventEmitter class into your project or add this line to your Podfile
pod 'EventEmitter', '~> 0.1.1'
Register event listener on any object:
#import "EventEmitter.h"
NSObject* emitter = [[NSObject alloc] init];
__block BOOL ready = NO;
[emitter on:@"ready" notify:^() {
NSLog(@"Yepp! The object is ready now!");
ready = YES;
}];
[emitter on:@"event" callback:^(NSDictionary* data) {
if (ready) {
NSLog(@"Receive event with data: %@", data);
}
}];
And later fire an event to the same object:
#import "EventEmitter.h"
NSObject* emitter = ...;
[emitter emit:@"ready"];
[emitter emit:@"event" data:@{
@"type": @"somethinghappend",
@"another key": @"another value",
}];
- The "original" API: http://nodejs.org/docs/latest/api/events.html
- use ARC (could be also enabled per file if your project does not use ARC)
- pure objective c, works on OSX and iOS
- Add a category to NSObject. More here: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html
- Use objc_setAssociatedObject and objc_getAssociatedObject to assign event listener to any object. More here: http://oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/