Skip to content

Commit

Permalink
fixed #104 Shoots list and Journal comments not refreshing after reco…
Browse files Browse the repository at this point in the history
…vering from lost websocket connection
  • Loading branch information
petersutter committed Jun 12, 2018
1 parent fd2c311 commit 98c9972
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions frontend/src/utils/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io from 'socket.io-client'
import forEach from 'lodash/forEach'
import map from 'lodash/map'
import isEqual from 'lodash/isEqual'
import Emitter from 'component-emitter'
import {ThrottledNamespacedEventEmitter} from './ThrottledEmitter'
import store from '../store'
Expand Down Expand Up @@ -117,11 +118,20 @@ class ShootsEmitter extends AbstractEmitter {
})

if (this.subscribeAfterAuthentication) {
this._subscribe({namespace: this.namespace, filter: undefined})
this._subscribe(this.subscribeAfterAuthentication)
this.subscribeAfterAuthentication = undefined
}
}

onDisconnect () {
super.onDisconnect()

if (this.subscribedTo) {
this.subscribeAfterAuthentication = this.subscribedTo
this.subscribedTo = undefined
}
}

setNamespace = function (namespace, filter) {
this.namespace = namespace
this.filter = filter
Expand All @@ -131,25 +141,23 @@ class ShootsEmitter extends AbstractEmitter {
return this._subscribe({namespace: this.namespace, filter: this.filter})
}

_subscribe = async function ({namespace, filter}) {
if (this.namespace) {
_subscribe = async function (subscribeTo) {
if (!this.subscribedTo || !isEqual(this.subscribedTo, subscribeTo)) {
const {namespace, filter} = subscribeTo
if (this.authenticated) {
store.dispatch('clearShoots')
store.dispatch('setShootsLoading')

// optimization, so that we do not subscribe again if the namespace did not change (by calling setNamespace)
this.namespace = undefined
this.filter = undefined

if (namespace === '_all') {
const allNamespaces = await store.getters.namespaces
const namespaces = map(allNamespaces, (namespace) => { return {namespace, filter} })
this.socket.emit('subscribe', {namespaces})
} else if (namespace) {
this.socket.emit('subscribe', {namespaces: [{namespace, filter}]})
}
this.subscribedTo = subscribeTo
} else {
this.subscribeAfterAuthentication = {namespace, filter}
this.subscribeAfterAuthentication = subscribeTo
}
}
}
Expand All @@ -172,6 +180,15 @@ class JournalsEmitter extends AbstractEmitter {
}
}

onDisconnect () {
super.onDisconnect()

if (this.subscribedToComments) {
this.subscribeCommentsAfterAuthentication = this.subscribedToComments
this.subscribedToComments = undefined
}
}

setUser (user) {
if (!store.getters.isAdmin) {
return
Expand All @@ -187,14 +204,15 @@ class JournalsEmitter extends AbstractEmitter {
}
}

subscribeComments ({name, namespace}) {
subscribeComments (subscribeToComments) {
if (this.authenticated) {
if (store.getters.isAdmin) {
const {name, namespace} = subscribeToComments
this.socket.emit('subscribeComments', {name, namespace})
this.subscribedComments = true
this.subscribedToComments = subscribeToComments
}
} else {
this.subscribeCommentsAfterAuthentication = {name, namespace}
this.subscribeCommentsAfterAuthentication = subscribeToComments
}
}

Expand All @@ -203,9 +221,9 @@ class JournalsEmitter extends AbstractEmitter {

if (this.authenticated) {
if (store.getters.isAdmin) {
if (this.subscribedComments) {
if (this.subscribedToComments) {
this.socket.emit('unsubscribeComments')
this.subscribedComments = false
this.subscribedToComments = undefined
}
}
}
Expand Down

0 comments on commit 98c9972

Please sign in to comment.