refs #769 Switch user streaming method

This commit is contained in:
AkiraFukushima 2018-12-18 23:00:46 +09:00
parent 8c1cdd4ddb
commit c210bea4d9
4 changed files with 38 additions and 25 deletions

View File

@ -390,20 +390,21 @@ ipcMain.on('reset-badge', () => {
// streaming
let userStreaming = null
ipcMain.on('start-user-streaming', (event, ac) => {
accountManager.getAccount(ac._id)
ipcMain.on('start-user-streaming', (event, obj) => {
const { account, useWebsocket } = obj
accountManager.getAccount(account._id)
.catch((err) => {
log.error(err)
event.sender.send('error-start-user-streaming', err)
})
.then((account) => {
.then((acct) => {
// Stop old user streaming
if (userStreaming !== null) {
userStreaming.stop()
userStreaming = null
}
userStreaming = new StreamingManager(account)
userStreaming = new StreamingManager(acct, useWebsocket)
userStreaming.startUser(
(update) => {
event.sender.send('update-start-user-streaming', update)

View File

@ -4,11 +4,11 @@ import WebSocket from './websocket'
import log from 'electron-log'
export default class StreamingManager {
constructor (account) {
constructor (account, useWebsocket = false) {
this.account = account
this.streaming = new Streaming(account)
this.websocket = new WebSocket(account)
this.mastodon = true
this.useWebsocket = useWebsocket
}
/**
@ -17,18 +17,19 @@ export default class StreamingManager {
async detectPleroma () {
const data = await Mastodon.get('/instance', {}, this.account.baseURL + '/api/v1')
if (data.version.includes('Pleroma')) {
this.mastodon = false
log.info('detect Pleroma')
return true
}
return false
}
startUser (updateCallback, notificationCallback, errCallback) {
this.detectPleroma()
.then(() => {
if (this.mastodon) {
this._startUserStreaming(updateCallback, notificationCallback, errCallback)
} else {
.then((isPleroma) => {
if (isPleroma || this.useWebsocket) {
this._startUserSocket(updateCallback, notificationCallback, errCallback)
} else {
this._startUserStreaming(updateCallback, notificationCallback, errCallback)
}
})
.catch(err => errCallback(err))
@ -36,11 +37,11 @@ export default class StreamingManager {
start (path, params, updateCallback, errCallback) {
this.detectPleroma()
.then(() => {
if (this.mastodon) {
this._startStreaming(path, params, updateCallback, errCallback)
} else {
.then((isPleroma) => {
if (isPleroma || this.useWebsocket) {
this._startSocket(path, params, updateCallback, errCallback)
} else {
this._startStreaming(path, params, updateCallback, errCallback)
}
})
.catch(err => errCallback(err))

View File

@ -66,13 +66,15 @@ export default {
filter: '',
filterVisible: false,
showReblogs: true,
showReplies: true,
useWebsocket: false
showReplies: true
}
},
computed: {
...mapState({
title: state => state.TimelineSpace.HeaderMenu.title
...mapState('TimelineSpace/HeaderMenu', {
title: state => state.title
}),
...mapState('TimelineSpace', {
useWebsocket: state => state.useWebsocket
})
},
created () {
@ -134,7 +136,9 @@ export default {
}
},
switchStreaming () {
this.useWebsocket = !this.useWebsocket
this.$store.dispatch('TimelineSpace/stopStreamings')
this.$store.commit('TimelineSpace/changeUseWebsocket', !this.useWebsocket)
this.$store.dispatch('TimelineSpace/startStreamings')
},
openNewTootModal () {
this.$store.dispatch('TimelineSpace/Modals/NewToot/openModal')

View File

@ -29,7 +29,8 @@ const TimelineSpace = {
direct: unreadSettings.Direct.default,
local: unreadSettings.Local.default,
public: unreadSettings.Public.default
}
},
useWebsocket: false
},
mutations: {
updateAccount (state, account) {
@ -55,6 +56,9 @@ const TimelineSpace = {
},
updateUnreadNotification (state, settings) {
state.unreadNotification = settings
},
changeUseWebsocket (state, use) {
state.useWebsocket = use
}
},
actions: {
@ -200,8 +204,8 @@ const TimelineSpace = {
dispatch('bindPublicStreaming')
}
},
startStreamings ({ dispatch, state }, account) {
dispatch('startUserStreaming', account)
startStreamings ({ dispatch, state }) {
dispatch('startUserStreaming')
if (state.unreadNotification.direct) {
dispatch('startDirectMessagesStreaming')
}
@ -250,9 +254,12 @@ const TimelineSpace = {
commit('TimelineSpace/SideMenu/changeUnreadNotifications', true, { root: true })
})
},
startUserStreaming (_, account) {
startUserStreaming ({ state }) {
return new Promise((resolve, reject) => {
ipcRenderer.send('start-user-streaming', account)
ipcRenderer.send('start-user-streaming', {
account: state.account,
useWebsocket: state.useWebsocket
})
ipcRenderer.once('error-start-user-streaming', (event, err) => {
reject(err)
})