@@ -25,7 +25,7 @@ class DataSyncController {
25
25
}
26
26
27
27
constructor ( ) {
28
- this . requests = [ ] ;
28
+ this . pendingRequests = [ ] ; // Stores the pending requests, contains objects of shape { requestId, resolve, reject }
29
29
this . connection = null ;
30
30
this . requestIdCounter = 0 ;
31
31
this . receivedFirstResponse = false ;
@@ -86,7 +86,11 @@ class DataSyncController {
86
86
onMessage ( event ) {
87
87
const payload = JSON . parse ( event . data ) ;
88
88
const requestId = payload . requestId ;
89
- const request = this . requests . find ( request => request . requestId === requestId ) ;
89
+ const request = this . pendingRequests . find ( request => request . requestId === requestId ) ;
90
+
91
+ // Remove request from array, as we don't need it anymore. If we don't remove it we will
92
+ // build up a lot of memory and slow down the app over time
93
+ this . pendingRequests . splice ( this . pendingRequests . indexOf ( request ) , 1 ) ;
90
94
91
95
this . receivedFirstResponse = true ;
92
96
@@ -122,7 +126,7 @@ class DataSyncController {
122
126
return new Promise ( ( resolve , reject ) => {
123
127
payload . requestId = this . requestIdCounter ++ ;
124
128
connection . send ( JSON . stringify ( payload ) ) ;
125
- this . requests . push ( { requestId : payload . requestId , resolve, reject } ) ;
129
+ this . pendingRequests . push ( { requestId : payload . requestId , resolve, reject } ) ;
126
130
} ) ;
127
131
}
128
132
@@ -176,6 +180,10 @@ class DataSubscription {
176
180
177
181
this . getRecords = this . getRecords . bind ( this ) ;
178
182
this . subscribe = this . subscribe . bind ( this ) ;
183
+ this . onDataSyncClosed = this . onDataSyncClosed . bind ( this ) ;
184
+ this . onDataSyncReconnect = this . onDataSyncReconnect . bind ( this ) ;
185
+ this . onMessage = this . onMessage . bind ( this ) ;
186
+
179
187
180
188
// When a new record is inserted, do we put it at the end or at the beginning?
181
189
this . newRecordBehaviour = this . detectNewRecordBehaviour ( ) ;
@@ -200,17 +208,9 @@ class DataSubscription {
200
208
201
209
this . subscriptionId = subscriptionId ;
202
210
203
- dataSyncController . addEventListener ( 'message' , message => {
204
- if ( this . isClosed ) {
205
- return ;
206
- }
207
- if ( message . subscriptionId === this . subscriptionId ) {
208
- this . receiveUpdate ( message ) ;
209
- }
210
- } ) ;
211
-
212
- dataSyncController . addEventListener ( 'close' , this . onDataSyncClosed . bind ( this ) ) ;
213
- dataSyncController . addEventListener ( 'reconnect' , this . onDataSyncReconnect . bind ( this ) ) ;
211
+ dataSyncController . addEventListener ( 'message' , this . onMessage ) ;
212
+ dataSyncController . addEventListener ( 'close' , this . onDataSyncClosed ) ;
213
+ dataSyncController . addEventListener ( 'reconnect' , this . onDataSyncReconnect ) ;
214
214
215
215
this . isConnected = true ;
216
216
this . records = result ;
@@ -224,6 +224,15 @@ class DataSubscription {
224
224
}
225
225
}
226
226
227
+ onMessage ( message ) {
228
+ if ( this . isClosed ) {
229
+ return ;
230
+ }
231
+ if ( message . subscriptionId === this . subscriptionId ) {
232
+ this . receiveUpdate ( message ) ;
233
+ }
234
+ }
235
+
227
236
receiveUpdate ( message ) {
228
237
const tag = message . tag ;
229
238
if ( tag === 'DidUpdate' ) {
@@ -249,6 +258,10 @@ class DataSubscription {
249
258
250
259
const dataSyncController = DataSyncController . getInstance ( ) ;
251
260
const { subscriptionId } = await dataSyncController . sendMessage ( { tag : 'DeleteDataSubscription' , subscriptionId : this . subscriptionId } ) ;
261
+
262
+ dataSyncController . removeEventListener ( 'message' , this . onMessage ) ;
263
+ dataSyncController . removeEventListener ( 'close' , this . onDataSyncClosed ) ;
264
+ dataSyncController . removeEventListener ( 'reconnect' , this . onDataSyncReconnect ) ;
252
265
253
266
this . isClosed = true ;
254
267
this . isConnected = false ;
0 commit comments