1
0
mirror of https://github.com/nolanlawson/pinafore synced 2025-02-02 22:57:36 +01:00

47 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-11 13:46:57 -08:00
import { TimelineStream } from '../_api/TimelineStream'
import { mark, stop } from '../_utils/marks'
2018-02-16 19:38:21 -08:00
import { deleteStatus } from './deleteStatuses'
import { addStatusOrNotification } from './addStatusOrNotification'
2018-02-15 09:02:46 -08:00
function processMessage (instanceName, timelineName, message) {
mark('processMessage')
2018-02-11 13:46:57 -08:00
let { event, payload } = message
2018-02-15 09:02:46 -08:00
switch (event) {
case 'delete':
2018-03-10 16:21:10 -08:00
deleteStatus(instanceName, payload)
2018-02-15 09:02:46 -08:00
break
case 'update':
2018-03-10 16:21:10 -08:00
addStatusOrNotification(instanceName, timelineName, JSON.parse(payload))
2018-02-15 09:02:46 -08:00
break
case 'notification':
2018-03-10 16:21:10 -08:00
addStatusOrNotification(instanceName, 'notifications', JSON.parse(payload))
2018-02-15 09:02:46 -08:00
break
}
stop('processMessage')
2018-02-11 13:46:57 -08:00
}
export function createStream (streamingApi, instanceName, accessToken,
timelineName, onOpenStream) {
2018-02-11 14:11:03 -08:00
return new TimelineStream(streamingApi, accessToken, timelineName, {
onMessage (msg) {
2018-02-15 09:02:46 -08:00
if (msg.event !== 'update' && msg.event !== 'delete' && msg.event !== 'notification') {
2018-02-11 13:46:57 -08:00
console.error("don't know how to handle event", msg)
return
}
processMessage(instanceName, timelineName, msg)
2018-02-11 13:46:57 -08:00
},
2018-02-11 14:11:03 -08:00
onOpen () {
if (onOpenStream) {
onOpenStream()
}
2018-02-11 13:46:57 -08:00
console.log('opened stream for timeline', timelineName)
},
2018-02-11 14:11:03 -08:00
onClose () {
2018-02-11 13:46:57 -08:00
console.log('closed stream for timeline', timelineName)
},
2018-02-11 14:11:03 -08:00
onReconnect () {
2018-02-11 13:46:57 -08:00
console.log('reconnected stream for timeline', timelineName)
}
})
2018-02-13 19:35:46 -08:00
}