ARNEventEmitter 0.1.0

ARNEventEmitter 0.1.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by airin.



  • By
  • xxxAIRINxxx

I aimed at the implementation of Node.js EventEmmiter.

and KVO.

Respect

It was inspired by the following products.

Requirements

Requires iOS 5.0 or later, and uses ARC.

How To Use

emitterOn

object A (On)

    [[self class] arn_emitterOn:@"test" owner:self needMainThreadCall:YES callbackBlock:^(id resultObject){
        // called MianThread
        NSLog(@"A");
    }];

object B (On)

  [[self class] arn_emitterOn:@"test" owner:self needMainThreadCall:NO callbackBlock:^(id resultObject){
      // called  not MianThread
      NSLog(@"B");
  }];

object C (Emit)

  [[self class] arn_emitterEmit:@"test" resultObject:nil];
  // called objectA emitterOn
  // called objectB emitterOn

  [[self class] arn_emitterEmit:@"test" resultObject:nil];
  // called objectA emitterOn
  // called objectB emitterOn

emitterOnce

object A (On)

    [[self class] arn_emitterOn:@"test" owner:self needMainThreadCall:YES callbackBlock:^(id resultObject){
        // called MianThread
        NSLog(@"A");
    }];

object B (Once)

  [[self class] arn_emitterOnce:@"test" owner:self needMainThreadCall:NO callbackBlock:^(id resultObject){
      // called  not MianThread
      NSLog(@"B");
  }];

object C (Emit)

  [[self class] arn_emitterEmit:@"test" resultObject:nil];
  // called objectA emitterOn
  // called objectB emitterOnce

  [[self class] arn_emitterEmit:@"test" resultObject:nil];
  // called objectA emitterOn

emitterOff

object A (On)

    [[self class] arn_emitterOn:@"test" owner:self needMainThreadCall:YES callbackBlock:^(id resultObject){
        // called MianThread
        NSLog(@"A");
    }];

object B (Off)

  [[self class] arn_emitterOnce:@"test" owner:self needMainThreadCall:NO callbackBlock:^(id resultObject){
      // called  not MianThread
      NSLog(@"B");
      [self class] arn_emitterOff:@"test" owner:self];
  }];

object C (Emit)

  [[self class] arn_emitterEmit:@"test" resultObject:nil];
  // called objectA emitterOn
  // called objectB emitterOn

  [[self class] arn_emitterEmit:@"test" resultObject:nil];
  // called objectA emitterOn

Key-Value Observing

Attention!!

Use exchange Implementations by the following

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

KVO

  MyObject *objA = MyObject.new;
  MyObject *objB = MyObject.new;

  objA.count = 0;
  objB.count = 0;

  [objA arn_emitterObserve:objB keyPath:@"count" isOnce:NO needMainThreadCall:NO callbackBlock:^(id resutObject) {
        objA.count++;
  }];

  objB.count++;
  // call arn_emitterObserve

  objB.count++;
  // call arn_emitterObserve

  // objA.count == 2

Licensing

The source code is distributed under the nonviral MIT License.

It's the simplest most permissive license available.

Japanese Note

Node.js界隈で広く使われているEventEmitterオブジェクトをObjective-Cで実装したものです。

(Node.jsのEventEmitterオブジェクトと同一の機能を有しているわけではありません)