Observable
@interface Observable : NSObject
An object that emits events.
Listeners can bind to specific events and be notified when the event is triggered.
-
A dictionary that contains all methods to be executed when an event is triggered
Declaration
Objective-C
@property (readwrite, strong, nonatomic) NSMutableDictionary *_Nonnull callbacks;
Swift
var callbacks: NSMutableDictionary { get set }
-
Binds to an event by providing a block that will receive the event payload as a parameter.
The block will be executed synchronously, for costly operations prefer the use of bindSync:callback:.
Declaration
Objective-C
- (nonnull Callback)bind:(nonnull NSString *)event callback:(nonnull void (^)(NSDictionary *_Nonnull))callback;
Swift
func bind(_ event: String, callback: @escaping ([AnyHashable : Any]) -> Void) -> Callback
Parameters
event
Name of the event to bind to
callback
Block to be executed synchronously when the event is triggered
Return Value
Returns the callback instance created, to be used when unbinding from the event
-
Binds to an event by providing a block that will receive the event payload as a parameter.
The block will be executed synchronously, for costly operations prefer the use of bindSync:callback:.
Declaration
Objective-C
- (nonnull Callback)bindSync:(nonnull NSString *)event callback:(nonnull void (^)(NSDictionary *_Nonnull))callback;
Swift
func bindSync(_ event: String, callback: @escaping ([AnyHashable : Any]) -> Void) -> Callback
Parameters
event
Name of the event to bind to
callback
Block to be executed synchronously when the event is triggered
Return Value
Returns the callback instance created, to be used when unbinding from the event
-
Binds to an event by providing a block that will receive the event payload as a parameter and return a Promise that will be resolved asynchronously.
Declaration
Parameters
event
Name of the event to bind to
callback
Block to be executed when the event is triggered
Return Value
Returns the provided callback, to be used when unbinding from the event
-
Unbinds the provided callback from an event.
Declaration
Objective-C
- (void)unbind:(nonnull NSString *)event callback:(nonnull Callback)callback;
Swift
func unbind(_ event: String, callback: @escaping Callback)
Parameters
event
The name of the event
callback
The callback that was returned when the bind method was called.
-
Triggers the event by calling all its bound callbacks with args as parameters.
have finished processing the triggered event
Declaration
Objective-C
- (nonnull Promise *)trigger:(nonnull NSString *)event args:(NSDictionary *_Nullable)args;
Swift
func trigger(_ event: String, args: [AnyHashable : Any]?) -> Promise
Parameters
event
The name of the event to be triggered
args
The payload of the event, to be passed to the callbacks
Return Value
Returns a promise that will be resolved when all of the callbacks
-
Triggers the event by calling all its bound callbacks.
have finished processing the triggered event
Declaration
Objective-C
- (nonnull Promise *)trigger:(nonnull NSString *)event;
Swift
func trigger(_ event: String) -> Promise
Parameters
event
The name of the event
Return Value
Returns a promise that will be resolved when all of the callbacks