From 2be73d1df25af1a75ec6a54eb146838c65437103 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 6 Jun 2017 02:10:56 +0700 Subject: [PATCH] Unsubscribe on unmount --- .../src/containers/Stream.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/client/coral-embed-stream/src/containers/Stream.js b/client/coral-embed-stream/src/containers/Stream.js index fa21c1ed2..893c15dc1 100644 --- a/client/coral-embed-stream/src/containers/Stream.js +++ b/client/coral-embed-stream/src/containers/Stream.js @@ -26,8 +26,10 @@ const {showSignInDialog} = authActions; const {addNotification} = notificationActions; class StreamContainer extends React.Component { - subscribeToUpdates = () => { - this.props.data.subscribeToMore({ + subscriptions = []; + + subscribeToUpdates() { + const sub1 = this.props.data.subscribeToMore({ document: COMMENTS_EDITED_SUBSCRIPTION, variables: { assetId: this.props.root.asset.id, @@ -50,7 +52,8 @@ class StreamContainer extends React.Component { } }, }); - this.props.data.subscribeToMore({ + + const sub2 = this.props.data.subscribeToMore({ document: COMMENTS_ADDED_SUBSCRIPTION, variables: { assetId: this.props.root.asset.id, @@ -78,7 +81,14 @@ class StreamContainer extends React.Component { return insertCommentIntoEmbedQuery(prev, commentAdded); } }); - }; + + this.subscriptions.push(sub1, sub2); + } + + unsubscribe() { + this.subscriptions.forEach((unsubscribe) => unsubscribe()); + this.subscriptions = []; + } loadNewReplies = (parent_id) => { const comment = this.props.root.comment @@ -123,6 +133,7 @@ class StreamContainer extends React.Component { } componentWillUnmount() { + this.unsubscribe(); clearInterval(this.countPoll); }