MUKSignal
provides a mechanism to dispatch a signal to various subscribers.
MUKSignal
is a simple class which exposes three main features.
You can dispatch a signal with a payload.
MUKSignal<NSString *> *signal = [[MUKSignal alloc] init];
...
[signal dispatch:@"Hello"];
You could add subscribers to a signal.
id const token = [signal subscribe:^(NSString *payload) {
// Called after dispatch
}];
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)
Library includes specific signals which are dispatched in particular conditions.
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 signals are dispatched when a notification fires.
MUKNotificationSignal *signal = [[MUKNotificationSignal alloc] initWithName:name:UIApplicationWillEnterForegroundNotification object:nil];
[signal subscribe:^(NSNotification *notification) {
// App will enter foreground
}];
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 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
}]];
- iOS 7 SDK.
- Minimum deployment target: iOS 7.
MUKSignal
is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod "MUKSignal"
Marco Muccinelli, muccymac@gmail.com
MUKSignal
is available under the MIT license. See the LICENSE file for more info.