This project contains some extensions on top of NSNotificationCenter that allows the creation of strong-typed notifications. My goal was to have a type-safe, reactive and easier-to-use mechanism.
Create a struct, enum, or class, that will represent the notification using the type system. :
enum UserNotification : String, Notifiable {
typealias ParameterType = User
case userDidLogin
case userDidLogout
}
Subscribe to the notification and use the parameter directly.
UserNotification.userDidLogin
.addListener { [unowned self] (user: User) in
self.loadPreferences(of: user)
}
.disposed(by: disposeBag)
UserNotification.userDidLogin.asObservable()
.subscribe(onNext:{ [unowned self] (user:User) in
self.loadPreferences(of: user)
})
.disposed(by: disposeBag)
NoParamsNotifiable, lets you post notifications with no parameters:
enum CalendarNotification : NoParamsNotifiable {
case calendarDidSynchronize
}
CalendarNotification.caledarDidSynchronize
.addListener{
print("Calendar synchronized")
}
.disposed(by:disposeBag)
To run the example project, clone the repo, and run pod install
from the Example directory first.
It contains a set of unit tests that explain how to use other features.
RxSwiftNotifications is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RxSwiftNotifications'
Leandro Perez, leandromperez@gmail.com
RxSwiftNotifications is available under the MIT license. See the LICENSE file for more info.