GT2/GT2-iOS/node_modules/react-native-branch/ios/RNBranchEventEmitter.m

82 lines
2.1 KiB
Mathematica
Raw Normal View History

2018-02-12 17:26:06 +00:00
//
// RNBranchEventEmitter.m
// Pods
//
// Created by Jimmy Dee on 4/6/17.
//
//
#import <React/RCTLog.h>
#import "RNBranch.h"
#import "RNBranchEventEmitter.h"
// Notification/Event Names
NSString * const kRNBranchInitSessionSuccess = @"RNBranch.initSessionSuccess";
NSString * const kRNBranchInitSessionError = @"RNBranch.initSessionError";
@interface RNBranchEventEmitter()
@property (nonatomic) BOOL hasListeners;
@end
@implementation RNBranchEventEmitter
RCT_EXPORT_MODULE();
- (instancetype)init
{
self = [super init];
if (self) {
_hasListeners = NO;
}
return self;
}
- (NSArray<NSString *> *)supportedEvents {
return @[kRNBranchInitSessionSuccess,
kRNBranchInitSessionError
];
}
- (void)startObserving {
self.hasListeners = YES;
for (NSString *event in [self supportedEvents]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotification:)
name:event
object:nil];
}
}
- (void)stopObserving {
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.hasListeners = NO;
}
# pragma mark - Public
+ (void)initSessionDidSucceedWithPayload:(NSDictionary *)payload
{
[self postNotificationName:kRNBranchInitSessionSuccess withPayload:payload];
}
+ (void)initSessionDidEncounterErrorWithPayload:(NSDictionary *)payload
{
[self postNotificationName:kRNBranchInitSessionError withPayload:payload];
}
# pragma mark - Private
+ (void)postNotificationName:(NSString *)name withPayload:(NSDictionary<NSString *, id> *)payload {
[[NSNotificationCenter defaultCenter] postNotificationName:name
object:self
userInfo:payload];
}
- (void)handleNotification:(NSNotification *)notification {
if (!self.hasListeners) return;
[self sendEventWithName:notification.name body:notification.userInfo];
}
@end