From ef224e9fe92947157c6ba0cab0afc0689763ec7d Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Mon, 11 Jun 2018 22:47:00 +0200
Subject: [PATCH] Fixed #287: duplicated websocket connexion on timeline

---
 changes/changelog.d/287.bugfix        |  1 +
 front/src/views/instance/Timeline.vue | 12 ++++++++++++
 2 files changed, 13 insertions(+)
 create mode 100644 changes/changelog.d/287.bugfix

diff --git a/changes/changelog.d/287.bugfix b/changes/changelog.d/287.bugfix
new file mode 100644
index 00000000..ce14736a
--- /dev/null
+++ b/changes/changelog.d/287.bugfix
@@ -0,0 +1 @@
+Fixed duplicated websocket connexion on timeline (#287)
diff --git a/front/src/views/instance/Timeline.vue b/front/src/views/instance/Timeline.vue
index 2ab8b708..03bd5a53 100644
--- a/front/src/views/instance/Timeline.vue
+++ b/front/src/views/instance/Timeline.vue
@@ -34,6 +34,7 @@ export default {
   data () {
     return {
       isLoading: false,
+      bridge: null,
       components: {
         'Like': Like,
         'Listen': Listen
@@ -44,6 +45,9 @@ export default {
     this.openWebsocket()
     this.fetchEvents()
   },
+  destroyed () {
+    this.disconnect()
+  },
   computed: {
     ...mapState({
       events: state => state.instance.events
@@ -58,14 +62,22 @@ export default {
         self.$store.commit('instance/events', response.data.results)
       })
     },
+    disconnect () {
+      if (!this.bridge) {
+        return
+      }
+      this.bridge.socket.close(1000, 'goodbye', {keepClosed: true})
+    },
     openWebsocket () {
       if (!this.$store.state.auth.authenticated) {
         return
       }
+      this.disconnect()
       let self = this
       let token = this.$store.state.auth.token
       // let token = 'test'
       const bridge = new WebSocketBridge()
+      this.bridge = bridge
       bridge.connect(
         `/api/v1/instance/activity?token=${token}`,
         null,
-- 
GitLab