9
9
import com .batch .android .BatchEventDispatcher ;
10
10
import com .batch .android .BatchPushPayload ;
11
11
import com .facebook .react .bridge .Arguments ;
12
- import com .facebook .react .bridge .LifecycleEventListener ;
13
12
import com .facebook .react .bridge .ReactApplicationContext ;
14
13
import com .facebook .react .bridge .WritableMap ;
15
14
import com .facebook .react .modules .core .DeviceEventManagerModule ;
21
20
*/
22
21
public class RNBatchEventDispatcher implements BatchEventDispatcher {
23
22
23
+
24
+ /**
25
+ * Max number of events we keep in the event queue
26
+ */
27
+ private static final int MAX_QUEUE_SIZE = 5 ;
28
+
24
29
/**
25
30
* React Context
26
31
*/
@@ -36,12 +41,21 @@ public class RNBatchEventDispatcher implements BatchEventDispatcher {
36
41
@ NonNull
37
42
private final LinkedList <RNBatchEvent > events = new LinkedList <>();
38
43
44
+ /**
45
+ * Whether we have a listener registered
46
+ * Default: false
47
+ */
48
+ private boolean hasListener ;
49
+
39
50
/**
40
51
* Send event to the JS bridge
41
52
* @param event dispatched event
42
53
*/
43
54
private void sendEvent (@ NonNull RNBatchEvent event ) {
44
55
if (reactContext == null || !reactContext .hasActiveCatalystInstance ()) {
56
+ Log .d (RNBatchModule .LOGGER_TAG ,
57
+ "Trying to send an event while react context is null" +
58
+ " or has no active catalyst instance. Aborting." );
45
59
return ;
46
60
}
47
61
reactContext .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
@@ -70,61 +84,69 @@ public void dispatchEvent(@NonNull Batch.EventDispatcher.Type type,
70
84
}
71
85
72
86
RNBatchEvent event = new RNBatchEvent (eventName , params );
73
- synchronized (events ) {
74
- events .add (event );
75
- }
76
-
77
- if (reactContext == null || !reactContext .hasActiveCatalystInstance ()) {
87
+ if (!isModuleReady () || !hasListener ) {
78
88
Log .d (RNBatchModule .LOGGER_TAG ,
79
- "React context is null or has no active catalyst instance. Queuing event." );
89
+ "Module is not ready or no listener registered yet. Queuing event: " .concat (eventName ));
90
+ queueEvent (event );
80
91
return ;
81
92
}
82
- dequeueEvents ( );
93
+ sendEvent ( event );
83
94
}
84
95
}
85
96
86
97
/**
87
98
* Dequeue the stored events
88
99
*/
89
- public void dequeueEvents () {
90
- if (events .isEmpty ()) {
91
- return ;
92
- }
100
+ private void dequeueEvents () {
93
101
synchronized (events ) {
94
- sendEvent (events .pop ());
102
+ if (events .isEmpty ()) {
103
+ return ;
104
+ }
105
+ while (events .size () != 0 ) {
106
+ sendEvent (events .pop ());
107
+ }
95
108
}
96
- dequeueEvents ();
97
109
}
98
110
99
111
/**
100
- * Set the react context instance
101
- *
102
- * Note: We are using a LifecycleEventListener to be notified when the react context
103
- * has a catalyst instance ready
112
+ * Put event in queue
113
+ * @param event event to queue
114
+ */
115
+ private void queueEvent (RNBatchEvent event ) {
116
+ synchronized (events ) {
117
+ if (events .size () >= MAX_QUEUE_SIZE ) {
118
+ events .clear ();
119
+ }
120
+ events .add (event );
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Set the react context instance used to emit event
104
126
* @param reactContext context
105
127
*/
106
- public void setReactContext (final ReactApplicationContext reactContext ) {
128
+ public void setReactContext (@ NonNull ReactApplicationContext reactContext ) {
107
129
this .reactContext = reactContext ;
108
- this .reactContext .addLifecycleEventListener (new LifecycleEventListener () {
109
- @ Override
110
- public void onHostResume () {
111
- // No we should have a catalyst instance ready
112
- if (reactContext .hasActiveCatalystInstance ()) {
113
- dequeueEvents ();
114
- reactContext .removeLifecycleEventListener (this );
115
- }
116
- }
130
+ }
117
131
118
- @ Override
119
- public void onHostPause () {
120
- // do noting
121
- }
132
+ /**
133
+ * Simple method to know if module is ready
134
+ * Meaning we have a react context and a catalyst instance ready
135
+ * @return true if ready
136
+ */
137
+ private boolean isModuleReady () {
138
+ return reactContext != null && reactContext .hasActiveCatalystInstance ();
139
+ }
122
140
123
- @ Override
124
- public void onHostDestroy () {
125
- // do noting
126
- }
127
- });
141
+ /**
142
+ * Indicate we have at least one registered listener
143
+ * @param hasListener if we have a listener registered
144
+ */
145
+ public void setHasListener (boolean hasListener ) {
146
+ if (!this .hasListener && hasListener ) {
147
+ this .dequeueEvents ();
148
+ }
149
+ this .hasListener = hasListener ;
128
150
}
129
151
130
152
/**
0 commit comments