Skip to content

Commit 78a39d5

Browse files
committed
Fixed several event listener leaks in DataSync and reduced memory usage
1 parent e4ef430 commit 78a39d5

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

lib/IHP/DataSync/ihp-datasync.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DataSyncController {
2525
}
2626

2727
constructor() {
28-
this.requests = [];
28+
this.pendingRequests = []; // Stores the pending requests, contains objects of shape { requestId, resolve, reject }
2929
this.connection = null;
3030
this.requestIdCounter = 0;
3131
this.receivedFirstResponse = false;
@@ -86,7 +86,11 @@ class DataSyncController {
8686
onMessage(event) {
8787
const payload = JSON.parse(event.data);
8888
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);
9094

9195
this.receivedFirstResponse = true;
9296

@@ -122,7 +126,7 @@ class DataSyncController {
122126
return new Promise((resolve, reject) => {
123127
payload.requestId = this.requestIdCounter++;
124128
connection.send(JSON.stringify(payload));
125-
this.requests.push({ requestId: payload.requestId, resolve, reject });
129+
this.pendingRequests.push({ requestId: payload.requestId, resolve, reject });
126130
});
127131
}
128132

@@ -176,6 +180,10 @@ class DataSubscription {
176180

177181
this.getRecords = this.getRecords.bind(this);
178182
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+
179187

180188
// When a new record is inserted, do we put it at the end or at the beginning?
181189
this.newRecordBehaviour = this.detectNewRecordBehaviour();
@@ -200,17 +208,9 @@ class DataSubscription {
200208

201209
this.subscriptionId = subscriptionId;
202210

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);
214214

215215
this.isConnected = true;
216216
this.records = result;
@@ -224,6 +224,15 @@ class DataSubscription {
224224
}
225225
}
226226

227+
onMessage(message) {
228+
if (this.isClosed) {
229+
return;
230+
}
231+
if (message.subscriptionId === this.subscriptionId) {
232+
this.receiveUpdate(message);
233+
}
234+
}
235+
227236
receiveUpdate(message) {
228237
const tag = message.tag;
229238
if (tag === 'DidUpdate') {
@@ -249,6 +258,10 @@ class DataSubscription {
249258

250259
const dataSyncController = DataSyncController.getInstance();
251260
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);
252265

253266
this.isClosed = true;
254267
this.isConnected = false;

lib/IHP/DataSync/transaction.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class Transaction {
2222
}
2323

2424
await this.dataSyncController.sendMessage({ tag: 'CommitTransaction', id: this.transactionId });
25+
this.onClose();
2526
}
2627

2728
async rollback() {
@@ -30,6 +31,7 @@ export class Transaction {
3031
}
3132

3233
await this.dataSyncController.sendMessage({ tag: 'RollbackTransaction', id: this.transactionId });
34+
this.onClose();
3335
}
3436

3537
onClose() {

0 commit comments

Comments
 (0)