Skip to content

Commit

Permalink
DataSync: fixed connections closed when it's still needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Nov 20, 2024
1 parent 5e58405 commit 611b881
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/IHP/DataSync/ihp-datasync.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ class DataSubscription {
return;
}

// Set isClosed early as we need to prevent a second close() from triggering another DeleteDataSubscription message
// also we don't want to receive any further messages, and onMessage will not process if isClosed == true
this.isClosed = true;
this.onClose();

const dataSyncController = DataSyncController.getInstance();
const { subscriptionId } = await dataSyncController.sendMessage({ tag: 'DeleteDataSubscription', subscriptionId: this.subscriptionId });

Expand All @@ -360,10 +365,7 @@ class DataSubscription {
dataSyncController.removeEventListener('reconnect', this.onDataSyncReconnect);
dataSyncController.dataSubscriptions.splice(dataSyncController.dataSubscriptions.indexOf(this), 1);

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

this.onClose();
}

onDataSyncClosed() {
Expand Down Expand Up @@ -425,7 +427,9 @@ class DataSubscription {
return () => {
this.subscribers.splice(this.subscribers.indexOf(callback), 1);

this.closeIfNotUsed();
// We delay the close as react could be re-rendering a component
// we garbage collect this connecetion once it's clearly not used anymore
setTimeout(this.closeIfNotUsed.bind(this), 1000);
}
}

Expand Down

0 comments on commit 611b881

Please sign in to comment.