1 | //
|
2 | // RNBranchEventEmitter.m
|
3 | // Pods
|
4 | //
|
5 | // Created by Jimmy Dee on 4/6/17.
|
6 | //
|
7 | //
|
8 |
|
9 | #import <React/RCTLog.h>
|
10 |
|
11 | #import "RNBranch.h"
|
12 | #import "RNBranchEventEmitter.h"
|
13 |
|
14 | // Notification/Event Names
|
15 | NSString * const kRNBranchInitSessionSuccess = @"RNBranch.initSessionSuccess";
|
16 | NSString * const kRNBranchInitSessionError = @"RNBranch.initSessionError";
|
17 |
|
18 | @interface RNBranchEventEmitter()
|
19 | @property (nonatomic) BOOL hasListeners;
|
20 | @end
|
21 |
|
22 | @implementation RNBranchEventEmitter
|
23 |
|
24 | RCT_EXPORT_MODULE();
|
25 |
|
26 | - (instancetype)init
|
27 | {
|
28 | self = [super init];
|
29 | if (self) {
|
30 | _hasListeners = NO;
|
31 | }
|
32 | return self;
|
33 | }
|
34 |
|
35 | + (BOOL)requiresMainQueueSetup {
|
36 | return YES;
|
37 | }
|
38 |
|
39 | - (NSArray<NSString *> *)supportedEvents {
|
40 | return @[kRNBranchInitSessionSuccess,
|
41 | kRNBranchInitSessionError
|
42 | ];
|
43 | }
|
44 |
|
45 | - (void)startObserving {
|
46 | self.hasListeners = YES;
|
47 | for (NSString *event in [self supportedEvents]) {
|
48 | [[NSNotificationCenter defaultCenter] addObserver:self
|
49 | selector:@selector(handleNotification:)
|
50 | name:event
|
51 | object:nil];
|
52 | }
|
53 | }
|
54 |
|
55 | - (void)stopObserving {
|
56 | [[NSNotificationCenter defaultCenter] removeObserver:self];
|
57 | self.hasListeners = NO;
|
58 | }
|
59 |
|
60 | # pragma mark - Public
|
61 |
|
62 | + (void)initSessionDidSucceedWithPayload:(NSDictionary *)payload
|
63 | {
|
64 | [self postNotificationName:kRNBranchInitSessionSuccess withPayload:payload];
|
65 | }
|
66 |
|
67 | + (void)initSessionDidEncounterErrorWithPayload:(NSDictionary *)payload
|
68 | {
|
69 | [self postNotificationName:kRNBranchInitSessionError withPayload:payload];
|
70 | }
|
71 |
|
72 | # pragma mark - Private
|
73 |
|
74 | + (void)postNotificationName:(NSString *)name withPayload:(NSDictionary<NSString *, id> *)payload {
|
75 | [[NSNotificationCenter defaultCenter] postNotificationName:name
|
76 | object:self
|
77 | userInfo:payload];
|
78 | }
|
79 |
|
80 | - (void)handleNotification:(NSNotification *)notification {
|
81 | if (!self.hasListeners) return;
|
82 | [self sendEventWithName:notification.name body:notification.userInfo];
|
83 | }
|
84 |
|
85 | @end
|