Coder Social home page Coder Social logo

aiobservable's Introduction

AIObservable

The AIObservable class lets you to dispatch events to a list of observers. It's an implementation of the Observer design pattern. You can add and remove observers even within the notification callback and from multiple threads or queues.

This implementation has a few advantages over NSNotificationCenter:

  • You don't need to call removeObserver: on the observer's dealloc, AIObservable uses zeroing weak references.
  • When the observable is destroyed all observers are automatically removed.
  • Each observable object keeps its own set of observers. This makes it easier to debug.
  • I like having an observer protocol for each class that is observable. A protocol allows you to better document each event that the observable class can dispatch and what parameters it has. It also gives meaningul names to the methods. For instance collection:didAddObject:atIndex: as opposed to didAddObject:(NSNotification*)notification.

Known Issues

AIObservable is currently not (fully) compatible with Swift because Swift doesn't support invocations. I don't have any plans to fix this on this project as it would require a completly different approach. You can still use it in Swift but your classes (both observers and observables) need to inherit from NSObject.

Example

As an example suppose you have a chat room. The chat room object dispatches obseravable events when messages are added and people join and leave the room.

@class AIMessage;
@class AIUser;
@protocol AIChatRoomObserver;


@interface AIChatRoom : NSObject 

- (void)postMessage:(AIMessage*)message;
// ...

- (void)addObserver:(id<AIChatRoomObserver>)observer;
- (void)removeObserver:(id<AIChatRoomObserver>)observer;

@end


@protocol AIChatRoomObserver

@optional
- (void)chatRoom:(AIChatRoom*)room didGetNewMessage:(AIMessage*)message;
- (void)chatRoom:(AIChatRoom*)room userDidJoin:(AIUser*)user;
- (void)chatRoom:(AIChatRoom*)room userDidLeave:(AIUser*)user;

@end

As you can see everything has a clear intent and the observer methods are self-documenting. Here is the implementation:

#import <AIObservable.h>
#import <NSInvocation+AIConstructors.h>

@interface AIChatRoom ()

// We use composition but you could also use inheritance
@property (nonatomic, strong) AIObservable* observable;

@end


@implementation AIChatRoom

- (void)postMessage:(AIMessage*)message {
    // ...

    // Create an invocation that will be dispatched to every observer
    AIChatRoom* chatRoom = self;
    NSInvocation* invocation = [NSInvocation invocationWithProtocol:@protocol(AIChatRoomObserver)
                                                           selector:@selector(chatRoom:didGetNewMessage:)];
    [invocation setArgument:&chatRoom atIndex:2];
    [invocation setArgument:&message atIndex:3];
    [self.observable notifyObservers:invocation];
}

- (void)addObserver:(id<AIChatRoomObserver>)observer {
    [self.observable addObserver:observer];
}

- (void)removeObserver:(id<AIChatRoomObserver>)observer {
    [self.observable removeObserver:observer];
}

@end

Installation

Copy the source files to your project or add pod 'AIObservable' to your Podfile.

aiobservable's People

Contributors

alejandro-isaza avatar

Watchers

 avatar  avatar  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.