Skip to content

Commit 3a6c0d7

Browse files
committed
ios: limit event queue
1 parent 4dc22f7 commit 3a6c0d7

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

ios/RNBatchEventDispatcher.m

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# import "RNBatchEventDispatcher.h"
2-
2+
# define MAX_QUEUE_SIZE 5
33
@implementation RNBatchEventDispatcher {
44

5+
/// Whether NativeModule is ready or not
56
BOOL _moduleIsReady;
67

8+
/// Event queue
79
NSMutableArray<RNBatchEvent*>* _events;
810

11+
/// Block to send event from RNBatch
912
void (^_sendBlock)(RNBatchEvent *_Nonnull event);
1013
}
1114

@@ -17,53 +20,69 @@ -(instancetype)init {
1720
return self;
1821
}
1922

23+
/// Set event sender block
2024
-(void)setSendBlock:(void (^)(RNBatchEvent* event))callback {
2125
_sendBlock = callback;
22-
if(_sendBlock != nil) {
26+
if (_sendBlock != nil) {
2327
[self dequeueEvents];
2428
}
2529
}
2630

31+
/// Set native module is ready
2732
- (void)setModuleIsReady:(BOOL)ready {
2833
_moduleIsReady = ready;
2934
}
3035

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+
}
3142

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];
3550
}
51+
}
52+
53+
/// Dequeue all events
54+
- (void)dequeueEvents {
3655
@synchronized(_events) {
56+
if ([_events count] == 0) {
57+
return;
58+
}
3759
NSArray *enqueuedEvents = [_events copy];
3860
[_events removeAllObjects];
39-
4061
for (RNBatchEvent *event in enqueuedEvents) {
41-
_sendBlock(event);
62+
[self sendEvent:event];
4263
}
4364
}
4465
}
4566

67+
/// Batch event dispatcher callback
4668
- (void)dispatchEventWithType:(BatchEventDispatcherType)type
4769
payload:(nonnull id<BatchEventDispatcherPayload>)payload {
4870

49-
if (_moduleIsReady && _sendBlock == nil) {
50-
// RN Module is ready but no listener registered
51-
// not queuing up events
52-
return;
53-
}
71+
5472
NSString* eventName = [RNBatchEventDispatcher mapBatchEventDispatcherTypeToRNEvent:type];
5573
if (eventName != nil) {
5674
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;
6279
}
80+
[self sendEvent:event];
6381
}
6482
}
6583

66-
+ (nullable NSString *) mapBatchEventDispatcherTypeToRNEvent:(BatchEventDispatcherType)type {
84+
/// Mapping function
85+
+ (nullable NSString *)mapBatchEventDispatcherTypeToRNEvent:(BatchEventDispatcherType)type {
6786
switch (type) {
6887
case BatchEventDispatcherTypeNotificationOpen:
6988
return @"notification_open";
@@ -82,6 +101,7 @@ + (nullable NSString *) mapBatchEventDispatcherTypeToRNEvent:(BatchEventDispatch
82101
}
83102
}
84103

104+
/// Build payload event
85105
- (NSDictionary*) dictionaryWithEventDispatcherPayload:(id<BatchEventDispatcherPayload>)payload
86106
{
87107
NSMutableDictionary *output = [NSMutableDictionary dictionaryWithDictionary:@{
@@ -120,4 +140,4 @@ - (nonnull instancetype)initWithName:(nonnull NSString *)name andBody:(nullable
120140
return self;
121141
}
122142

123-
@end
143+
@end

0 commit comments

Comments
 (0)