1
1
# import " RNBatchEventDispatcher.h"
2
-
2
+ # define MAX_QUEUE_SIZE 5
3
3
@implementation RNBatchEventDispatcher {
4
4
5
+ // / Whether NativeModule is ready or not
5
6
BOOL _moduleIsReady;
6
7
8
+ // / Event queue
7
9
NSMutableArray <RNBatchEvent*>* _events;
8
10
11
+ // / Block to send event from RNBatch
9
12
void (^_sendBlock)(RNBatchEvent *_Nonnull event);
10
13
}
11
14
@@ -17,53 +20,69 @@ -(instancetype)init {
17
20
return self;
18
21
}
19
22
23
+ // / Set event sender block
20
24
-(void )setSendBlock : (void (^)(RNBatchEvent* event))callback {
21
25
_sendBlock = callback;
22
- if (_sendBlock != nil ) {
26
+ if (_sendBlock != nil ) {
23
27
[self dequeueEvents ];
24
28
}
25
29
}
26
30
31
+ // / Set native module is ready
27
32
- (void )setModuleIsReady : (BOOL )ready {
28
33
_moduleIsReady = ready;
29
34
}
30
35
36
+ // / Send an event to the Js bridge throught the block
37
+ - (void )sendEvent : (RNBatchEvent*)event {
38
+ if (_sendBlock != nil ) {
39
+ _sendBlock (event);
40
+ }
41
+ }
31
42
32
- - (void )dequeueEvents {
33
- if (_sendBlock == nil ) {
34
- return ;
43
+ // / Put an event in queue
44
+ - (void )queueEvent : (RNBatchEvent*)event {
45
+ @synchronized (_events) {
46
+ if ([_events count ] >= MAX_QUEUE_SIZE) {
47
+ [_events removeAllObjects ];
48
+ }
49
+ [_events addObject: event];
35
50
}
51
+ }
52
+
53
+ // / Dequeue all events
54
+ - (void )dequeueEvents {
36
55
@synchronized (_events) {
56
+ if ([_events count ] == 0 ) {
57
+ return ;
58
+ }
37
59
NSArray *enqueuedEvents = [_events copy ];
38
60
[_events removeAllObjects ];
39
-
40
61
for (RNBatchEvent *event in enqueuedEvents) {
41
- _sendBlock ( event) ;
62
+ [ self sendEvent: event] ;
42
63
}
43
64
}
44
65
}
45
66
67
+ // / Batch event dispatcher callback
46
68
- (void )dispatchEventWithType : (BatchEventDispatcherType)type
47
69
payload : (nonnull id <BatchEventDispatcherPayload>)payload {
48
70
49
- if (_moduleIsReady && _sendBlock == nil ) {
50
- // RN Module is ready but no listener registered
51
- // not queuing up events
52
- return ;
53
- }
71
+
54
72
NSString * eventName = [RNBatchEventDispatcher mapBatchEventDispatcherTypeToRNEvent: type];
55
73
if (eventName != nil ) {
56
74
RNBatchEvent* event = [[RNBatchEvent alloc ] initWithName: eventName andBody: [self dictionaryWithEventDispatcherPayload: payload]];
57
- @synchronized (_events) {
58
- [_events addObject: event];
59
- }
60
- if (_sendBlock != nil ) {
61
- [self dequeueEvents ];
75
+ if (!_moduleIsReady || _sendBlock == nil ) {
76
+ NSLog (@" RNBatch: Module is not ready or no listener registered. Queueing event." );
77
+ [self queueEvent: event];
78
+ return ;
62
79
}
80
+ [self sendEvent: event];
63
81
}
64
82
}
65
83
66
- + (nullable NSString *) mapBatchEventDispatcherTypeToRNEvent : (BatchEventDispatcherType)type {
84
+ // / Mapping function
85
+ + (nullable NSString *)mapBatchEventDispatcherTypeToRNEvent : (BatchEventDispatcherType)type {
67
86
switch (type) {
68
87
case BatchEventDispatcherTypeNotificationOpen:
69
88
return @" notification_open" ;
@@ -82,6 +101,7 @@ + (nullable NSString *) mapBatchEventDispatcherTypeToRNEvent:(BatchEventDispatch
82
101
}
83
102
}
84
103
104
+ // / Build payload event
85
105
- (NSDictionary *) dictionaryWithEventDispatcherPayload : (id <BatchEventDispatcherPayload>)payload
86
106
{
87
107
NSMutableDictionary *output = [NSMutableDictionary dictionaryWithDictionary: @{
@@ -120,4 +140,4 @@ - (nonnull instancetype)initWithName:(nonnull NSString *)name andBody:(nullable
120
140
return self;
121
141
}
122
142
123
- @end
143
+ @end
0 commit comments