+
@@ -16,17 +17,24 @@ export default {
components: { Toot },
computed: {
...mapState({
- timeline: state => state.TimelineSpace.Contents.Lists.timeline
+ timeline: state => state.TimelineSpace.Contents.Lists.timeline,
+ lazyLoading: state => state.TimelineSpace.Contents.Lists.lazyLoading
})
},
created () {
this.load()
+ document.getElementById('scrollable').addEventListener('scroll', this.onScroll)
},
watch: {
list_id: function () {
this.load()
}
},
+ destroyed () {
+ if (document.getElementById('scrollable') !== undefined && document.getElementById('scrollable') !== null) {
+ document.getElementById('scrollable').removeEventListener('scroll', this.onScroll)
+ }
+ },
methods: {
load () {
const loading = this.$loading({
@@ -49,10 +57,26 @@ export default {
},
updateToot (message) {
this.$store.commit('TimelineSpace/Contents/Lists/updateToot', message)
+ },
+ onScroll (event) {
+ console.log(document.getElementsByName('lists'))
+ if (((event.target.clientHeight + event.target.scrollTop) >= document.getElementsByName('lists')[0].clientHeight - 10) && !this.lazyloading) {
+ this.$store.dispatch('TimelineSpace/Contents/Lists/lazyFetchTimeline', {
+ list_id: this.list_id,
+ last: this.timeline[this.timeline.length - 1]
+ })
+ }
}
}
}
diff --git a/src/renderer/store/TimelineSpace/Contents/Lists.js b/src/renderer/store/TimelineSpace/Contents/Lists.js
index 7cb6e9c0..4f813c7b 100644
--- a/src/renderer/store/TimelineSpace/Contents/Lists.js
+++ b/src/renderer/store/TimelineSpace/Contents/Lists.js
@@ -3,12 +3,16 @@ import Mastodon from 'mastodon-api'
const Lists = {
namespaced: true,
state: {
- timeline: []
+ timeline: [],
+ lazyLoading: false
},
mutations: {
updateTimeline (state, timeline) {
state.timeline = timeline
},
+ insertTimeline (state, messages) {
+ state.timeline = state.timeline.concat(messages)
+ },
updateToot (state, message) {
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
@@ -24,6 +28,9 @@ const Lists = {
return toot
}
})
+ },
+ changeLazyLoading (state, value) {
+ state.lazyLoading = value
}
},
actions: {
@@ -40,6 +47,24 @@ const Lists = {
resolve(res)
})
})
+ },
+ lazyFetchTimeline ({ state, commit, rootState }, obj) {
+ return new Promise((resolve, reject) => {
+ if (state.lazyLoading) {
+ return resolve()
+ }
+ commit('changeLazyLoading', true)
+ const client = new Mastodon(
+ {
+ access_token: rootState.TimelineSpace.account.accessToken,
+ api_url: rootState.TimelineSpace.account.baseURL + '/api/v1'
+ })
+ client.get(`/timelines/list/${obj.list_id}`, { max_id: obj.last.id, limit: 40 }, (err, data, res) => {
+ if (err) return reject(err)
+ commit('insertTimeline', data)
+ commit('changeLazyLoading', false)
+ })
+ })
}
}
}
From 32b8ab9190b216ab4c7ee06045a6df63af945a77 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Mon, 9 Apr 2018 23:43:36 +0900
Subject: [PATCH 7/7] refs #167 Streaming update in list timeline
---
src/main/index.js | 30 ++++++++++++
.../TimelineSpace/Contents/Lists.vue | 46 +++++++++++++------
.../store/TimelineSpace/Contents/Lists.js | 26 +++++++++++
3 files changed, 89 insertions(+), 13 deletions(-)
diff --git a/src/main/index.js b/src/main/index.js
index c3a560e2..b3f0da66 100644
--- a/src/main/index.js
+++ b/src/main/index.js
@@ -452,6 +452,36 @@ ipcMain.on('stop-public-streaming', (event, _) => {
publicStreaming = null
})
+let listStreaming = null
+
+ipcMain.on('start-list-streaming', (event, obj) => {
+ const account = new Account(accountDB)
+ account.getAccount(obj.account._id)
+ .catch((err) => {
+ log.error(err)
+ event.sender.send('error-start-list-streaming', err)
+ })
+ .then((account) => {
+ // Stop old list streaming
+ if (listStreaming !== null) {
+ listStreaming.stop()
+ listStreaming = null
+ }
+
+ listStreaming = new Streaming(account)
+ listStreaming.start(
+ `/streaming/list?list=${obj.list_id}`,
+ (update) => {
+ event.sender.send('update-start-list-streaming', update)
+ },
+ (err) => {
+ log.error(err)
+ event.sendeer.send('error-start-list-streaming', err)
+ }
+ )
+ })
+})
+
// sounds
ipcMain.on('fav-rt-action-sound', (event, _) => {
const preferences = new Preferences(preferencesDBPath)
diff --git a/src/renderer/components/TimelineSpace/Contents/Lists.vue b/src/renderer/components/TimelineSpace/Contents/Lists.vue
index b6a54b8c..57522ccb 100644
--- a/src/renderer/components/TimelineSpace/Contents/Lists.vue
+++ b/src/renderer/components/TimelineSpace/Contents/Lists.vue
@@ -22,44 +22,64 @@ export default {
})
},
created () {
+ const loading = this.$loading({
+ lock: true,
+ text: 'Loading',
+ spinner: 'el-icon-loading',
+ background: 'rgba(0, 0, 0, 0.7)'
+ })
this.load()
+ .then(() => {
+ loading.close()
+ })
document.getElementById('scrollable').addEventListener('scroll', this.onScroll)
},
watch: {
list_id: function () {
+ const loading = this.$loading({
+ lock: true,
+ text: 'Loading',
+ spinner: 'el-icon-loading',
+ background: 'rgba(0, 0, 0, 0.7)'
+ })
this.load()
+ .then(() => {
+ loading.close()
+ })
}
},
+ beforeDestroy () {
+ this.$store.dispatch('TimelineSpace/Contents/Lists/stopStreaming')
+ },
destroyed () {
if (document.getElementById('scrollable') !== undefined && document.getElementById('scrollable') !== null) {
document.getElementById('scrollable').removeEventListener('scroll', this.onScroll)
}
},
methods: {
- load () {
- const loading = this.$loading({
- lock: true,
- text: 'Loading',
- spinner: 'el-icon-loading',
- background: 'rgba(0, 0, 0, 0.7)'
- })
- this.$store.dispatch('TimelineSpace/Contents/Lists/fetchTimeline', this.list_id)
- .then(() => {
- loading.close()
+ async load () {
+ await this.$store.dispatch('TimelineSpace/Contents/Lists/stopStreaming')
+ try {
+ await this.$store.dispatch('TimelineSpace/Contents/Lists/fetchTimeline', this.list_id)
+ } catch (err) {
+ this.$message({
+ message: 'Failed to get timeline',
+ type: 'error'
})
+ }
+ this.$store.dispatch('TimelineSpace/Contents/Lists/startStreaming', this.list_id)
.catch(() => {
- loading.close()
this.$message({
- message: 'Failed to get timeline',
+ message: 'Failed to start streaming',
type: 'error'
})
})
+ return 'started'
},
updateToot (message) {
this.$store.commit('TimelineSpace/Contents/Lists/updateToot', message)
},
onScroll (event) {
- console.log(document.getElementsByName('lists'))
if (((event.target.clientHeight + event.target.scrollTop) >= document.getElementsByName('lists')[0].clientHeight - 10) && !this.lazyloading) {
this.$store.dispatch('TimelineSpace/Contents/Lists/lazyFetchTimeline', {
list_id: this.list_id,
diff --git a/src/renderer/store/TimelineSpace/Contents/Lists.js b/src/renderer/store/TimelineSpace/Contents/Lists.js
index 4f813c7b..21cd93a5 100644
--- a/src/renderer/store/TimelineSpace/Contents/Lists.js
+++ b/src/renderer/store/TimelineSpace/Contents/Lists.js
@@ -1,3 +1,4 @@
+import { ipcRenderer } from 'electron'
import Mastodon from 'mastodon-api'
const Lists = {
@@ -7,6 +8,9 @@ const Lists = {
lazyLoading: false
},
mutations: {
+ appendTimeline (state, update) {
+ state.timeline = [update].concat(state.timeline)
+ },
updateTimeline (state, timeline) {
state.timeline = timeline
},
@@ -48,6 +52,28 @@ const Lists = {
})
})
},
+ startStreaming ({ state, commit, rootState }, listID) {
+ ipcRenderer.on('update-start-list-streaming', (event, update) => {
+ commit('appendTimeline', update)
+ })
+ return new Promise((resolve, reject) => {
+ ipcRenderer.send('start-list-streaming', {
+ list_id: listID,
+ account: rootState.TimelineSpace.account
+ })
+ ipcRenderer.once('error-start-list-streaming', (event, err) => {
+ reject(err)
+ })
+ })
+ },
+ stopStreaming ({ commit }) {
+ return new Promise((resolve, reject) => {
+ ipcRenderer.removeAllListeners('error-start-list-streaming')
+ ipcRenderer.removeAllListeners('update-start-list-streaming')
+ ipcRenderer.send('stop-list-streaming')
+ resolve()
+ })
+ },
lazyFetchTimeline ({ state, commit, rootState }, obj) {
return new Promise((resolve, reject) => {
if (state.lazyLoading) {