initial import
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import store from '@/store'
|
||||
|
||||
export default {
|
||||
handle(data) {
|
||||
store.setCreditsAction(data.content)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import credits from './credits'
|
||||
|
||||
var handlers = {
|
||||
'credits': credits
|
||||
}
|
||||
|
||||
export default {
|
||||
handle(data) {
|
||||
if(handlers[data.type]) {
|
||||
handlers[data.type].handle(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import store from "../store"
|
||||
import handler from './handlers'
|
||||
|
||||
var connection = null
|
||||
var onOpenCallback = null
|
||||
var onCloseCallback = null
|
||||
|
||||
const eventSource = {
|
||||
connect(channel) {
|
||||
if(connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
|
||||
connection = new EventSource('/feed/updates/' + channel)
|
||||
|
||||
connection.addEventListener("message", function(message) {
|
||||
var data = JSON.parse(message.data)
|
||||
handler.handle(data)
|
||||
store.setRawMessageAction(message.data)
|
||||
})
|
||||
|
||||
connection.addEventListener("open", function () {
|
||||
if(typeof onOpenCallback === 'function') {
|
||||
onOpenCallback()
|
||||
}
|
||||
})
|
||||
|
||||
connection.addEventListener("close", function () {
|
||||
if(typeof onCloseCallback === 'function') {
|
||||
onCloseCallback()
|
||||
}
|
||||
})
|
||||
|
||||
connection.addEventListener("error", function () {
|
||||
if(connection.readyState !== EventSource.CONNECTING) {
|
||||
if(typeof onCloseCallback === 'function') {
|
||||
onCloseCallback()
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
isConnected() {
|
||||
return connection.readyState === EventSource.OPEN
|
||||
},
|
||||
isConnecting() {
|
||||
return connection.readyState === EventSource.CONNECTING
|
||||
},
|
||||
onOpen(callback) {
|
||||
onOpenCallback = callback
|
||||
},
|
||||
onClose(callback) {
|
||||
onCloseCallback = callback
|
||||
}
|
||||
}
|
||||
|
||||
export default eventSource
|
||||
Reference in New Issue
Block a user