1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-01-27 07:46:15 +01:00

refs #24 Listen local streaming in main process, and display local timeline

This commit is contained in:
AkiraFukushima 2018-03-14 12:24:54 +09:00
parent 4a7fc1fa0f
commit 2163f5018c
6 changed files with 123 additions and 12 deletions

View File

@ -258,6 +258,39 @@ ipcMain.on('stop-user-streaming', (event, _) => {
userStreaming = null
})
let localStreaming = null
ipcMain.on('start-local-streaming', (event, ac) => {
const account = new Account(db)
account.getAccount(ac._id)
.catch((err) => {
event.sender.send('error-start-local-streaming', err)
})
.then((account) => {
// Stop old local streaming
if (localStreaming !== null) {
localStreaming.stop()
localStreaming = null
}
localStreaming = new Streaming(account)
localStreaming.start(
'/streaming/public/local',
(update) => {
event.sender.send('update-start-local-streaming', update)
},
(err) => {
event.sender.send('error-start-local-streaming', err)
}
)
})
})
ipcMain.on('stop-local-streaming', (event, _) => {
localStreaming.stop()
localStreaming = null
})
/**
* Auto Updater
*

View File

@ -14,6 +14,7 @@ export default class Streaming {
startUserStreaming (updateCallback, notificationCallback, errCallback) {
this.listener = this.client.stream('/streaming/user')
console.log('/streaming/user started')
this.listener.on('message', (msg) => {
switch (msg.event) {
@ -34,7 +35,27 @@ export default class Streaming {
})
}
start (path, updateCallback, errCallback) {
this.listener = this.client.stream(path)
console.log(`${path} started`)
this.listener.on('message', (msg) => {
switch (msg.event) {
case 'update':
updateCallback(msg.data)
break
default:
break
}
})
this.listener.on('error', (err) => {
errCallback(err)
})
}
stop () {
this.listener.stop()
console.log('streaming stopped')
}
}

View File

@ -24,6 +24,7 @@ export default {
})
this.$store.dispatch('TimelineSpace/fetchAccount', this.$route.params.id)
.then((account) => {
this.$store.dispatch('TimelineSpace/watchShortcutEvents', account)
this.$store.dispatch('TimelineSpace/fetchHomeTimeline', account)
.then(() => {
loading.close()
@ -35,13 +36,6 @@ export default {
type: 'error'
})
})
this.$store.dispatch('TimelineSpace/startUserStreaming', account)
.catch(() => {
this.$message({
message: 'Could not start user streaming',
type: 'error'
})
})
this.$store.dispatch('TimelineSpace/username', account)
.catch(() => {
this.$message({
@ -56,7 +50,13 @@ export default {
type: 'error'
})
})
this.$store.dispatch('TimelineSpace/watchShortcutEvents', account)
this.$store.dispatch('TimelineSpace/startUserStreaming', account)
.catch(() => {
this.$message({
message: 'Could not start user streaming',
type: 'error'
})
})
})
.catch(() => {
loading.close()

View File

@ -1,11 +1,35 @@
<template>
<div id="local">
local
<div class="local-timeline" v-for="message in timeline" v-bind:key="message.id">
<toot :message="message"></toot>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
import Toot from './Cards/Toot'
export default {
name: 'local'
name: 'local',
components: { Toot },
computed: {
...mapState({
account: state => state.TimelineSpace.account,
timeline: state => state.TimelineSpace.Local.timeline
})
},
created () {
this.$store.dispatch('TimelineSpace/Local/startLocalStreaming', this.account)
},
beforeDestroy () {
this.$store.dispatch('TimelineSpace/Local/stopLocalStreaming')
}
}
</script>
<style lang="scss" scoped>
#local {
margin-left: 16px;
}
</style>

View File

@ -2,13 +2,15 @@ import { ipcRenderer } from 'electron'
import Mastodon from 'mastodon-api'
import SideMenu from './TimelineSpace/SideMenu'
import Favourites from './TimelineSpace/Favourites'
import Local from './TimelineSpace/Local'
import router from '../router'
const TimelineSpace = {
namespaced: true,
modules: {
SideMenu,
Favourites
Favourites,
Local
},
state: {
account: {
@ -84,7 +86,7 @@ const TimelineSpace = {
return new Promise((resolve, reject) => {
ipcRenderer.send('start-user-streaming', account)
ipcRenderer.once('error-start-userstreaming', (event, err) => {
ipcRenderer.once('error-start-user-streaming', (event, err) => {
reject(err)
})
})

View File

@ -0,0 +1,31 @@
import { ipcRenderer } from 'electron'
const Local = {
namespaced: true,
state: {
timeline: []
},
mutations: {
appendTimeline (state, update) {
state.timeline = [update].concat(state.timeline)
}
},
actions: {
startLocalStreaming ({ commit }, account) {
ipcRenderer.on('update-start-local-streaming', (event, update) => {
commit('appendTimeline', update)
})
return new Promise((resolve, reject) => {
ipcRenderer.send('start-local-streaming', account)
ipcRenderer.once('error-start-local-streaming', (event, err) => {
reject(err)
})
})
},
stopLocalStreaming ({ commit }) {
ipcRenderer.send('stop-local-streaming')
}
}
}
export default Local