Skip to content

muccy/MUKSignal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MUKSignal

CI Status Version License Platform

MUKSignal provides a mechanism to dispatch a signal to various subscribers.

Main features

MUKSignal is a simple class which exposes three main features.

Dispatch

You can dispatch a signal with a payload.

MUKSignal<NSString *> *signal = [[MUKSignal alloc] init];
...
[signal dispatch:@"Hello"];

Subscribtion

You could add subscribers to a signal.

id const token = [signal subscribe:^(NSString *payload) {
    // Called after dispatch
}];

Suspension

A subscription could be temporarily suspended.

[signal suspend:token];
// Dispatches are not delivered
[signal resume:token];
// If a dispatch has occurred during suspension is delivered now (if more than one they are coalesced into one)

Specific signals

Library includes specific signals which are dispatched in particular conditions.

KVO

KVO signals observe an object and are dispatched when a change occurs.

MUKKVOSignal<NSString *> *signal = [[MUKKVOSignalChange alloc] initWithObject:self keyPath:@"name"];
[signal subscribe:^(MUKKVOSignalChange<NSString *> change) {
    NSLog(@"Name changed from '%@' to '%@'", change.oldValue, change.value);
}];

Notification

Notification signals are dispatched when a notification fires.

MUKNotificationSignal *signal = [[MUKNotificationSignal alloc] initWithName:name:UIApplicationWillEnterForegroundNotification object:nil];
[signal subscribe:^(NSNotification *notification) {
    // App will enter foreground
}];

UIControl target-action

Control action signals are dispatched when action for a control is triggered.

MUKControlActionSignal<UIButton *> *signal = [[MUKControlActionSignal alloc] initWithControl:self.button forEvents:UIControlEventTouchUpInside];
[signal subscribe:^(UIEvent *event) {
    // Called when button has been pressed
}];

Observation

Observation is the concept to couple a signal to a particular subscription. It is particular useful when you have many signals to observe not to bloat your view controller.

MUKSignal *signal = ...;
MUKSignalObservation *observation = [MUKSignalObservation observationWithSignal:signal token:[signal subscribe:^(id payload) 
{
    // Waiting for a dispatch
}]];

Requirements

  • iOS 7 SDK.
  • Minimum deployment target: iOS 7.

Installation

MUKSignal is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "MUKSignal"

Author

Marco Muccinelli, muccymac@gmail.com

License

MUKSignal is available under the MIT license. See the LICENSE file for more info.

About

Dispatch signals which could be subscribed by various subscribers

Resources

License

Stars

Watchers

Forks

Packages

No packages published