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 kRNBranchInitSessionStart = @"RNBranch.initSessionStart";
|
16 | NSString * const kRNBranchInitSessionSuccess = @"RNBranch.initSessionSuccess";
|
17 | NSString * const kRNBranchInitSessionError = @"RNBranch.initSessionError";
|
18 |
|
19 | @interface RNBranchEventEmitter()
|
20 | @property (nonatomic) BOOL hasListeners;
|
21 | @end
|
22 |
|
23 | @implementation RNBranchEventEmitter
|
24 |
|
25 | RCT_EXPORT_MODULE();
|
26 |
|
27 | - (instancetype)init
|
28 | {
|
29 | self = [super init];
|
30 | if (self) {
|
31 | _hasListeners = NO;
|
32 | }
|
33 | return self;
|
34 | }
|
35 |
|
36 | + (BOOL)requiresMainQueueSetup {
|
37 | return YES;
|
38 | }
|
39 |
|
40 | - (NSArray<NSString *> *)supportedEvents {
|
41 | return @[
|
42 | kRNBranchInitSessionStart,
|
43 | kRNBranchInitSessionSuccess,
|
44 | kRNBranchInitSessionError
|
45 | ];
|
46 | }
|
47 |
|
48 | - (void)startObserving {
|
49 | self.hasListeners = YES;
|
50 | for (NSString *event in [self supportedEvents]) {
|
51 | [[NSNotificationCenter defaultCenter] addObserver:self
|
52 | selector:@selector(handleNotification:)
|
53 | name:event
|
54 | object:nil];
|
55 | }
|
56 | }
|
57 |
|
58 | - (void)stopObserving {
|
59 | [[NSNotificationCenter defaultCenter] removeObserver:self];
|
60 | self.hasListeners = NO;
|
61 | }
|
62 |
|
63 | # pragma mark - Public
|
64 |
|
65 | + (void)initSessionWillStartWithURI:(NSURL *)uri
|
66 | {
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | [self postNotificationName:kRNBranchInitSessionStart withPayload:@{
|
81 | RNBranchLinkOpenedNotificationUriKey: uri.absoluteString ?: NSNull.null
|
82 | }];
|
83 | }
|
84 |
|
85 | + (void)initSessionDidSucceedWithPayload:(NSDictionary *)payload
|
86 | {
|
87 | [self postNotificationName:kRNBranchInitSessionSuccess withPayload:payload];
|
88 | }
|
89 |
|
90 | + (void)initSessionDidEncounterErrorWithPayload:(NSDictionary *)payload
|
91 | {
|
92 | [self postNotificationName:kRNBranchInitSessionError withPayload:payload];
|
93 | }
|
94 |
|
95 | # pragma mark - Private
|
96 |
|
97 | + (void)postNotificationName:(NSString *)name withPayload:(NSDictionary<NSString *, id> *)payload {
|
98 | [[NSNotificationCenter defaultCenter] postNotificationName:name
|
99 | object:self
|
100 | userInfo:payload];
|
101 | }
|
102 |
|
103 | - (void)handleNotification:(NSNotification *)notification {
|
104 | if (!self.hasListeners) return;
|
105 | [self sendEventWithName:notification.name body:notification.userInfo];
|
106 | }
|
107 |
|
108 | @end
|