1
0
mirror of https://github.com/h3poteto/whalebird-desktop synced 2025-02-07 23:38:48 +01:00

refs #3301 Rewrite TimelineSpace/Contents/Home with composition API

This commit is contained in:
AkiraFukushima 2022-05-28 13:31:33 +09:00
parent a403c93208
commit ad6a16ca89
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
17 changed files with 314 additions and 291 deletions

View File

@ -30,7 +30,6 @@
<script> <script>
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import Toot from '@/components/organisms/Toot' import Toot from '@/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
export default { export default {
@ -42,7 +41,6 @@ export default {
}, },
name: 'bookmarks', name: 'bookmarks',
components: { Toot }, components: { Toot },
mixins: [reloadable],
computed: { computed: {
...mapState('TimelineSpace', { ...mapState('TimelineSpace', {
account: state => state.account account: state => state.account
@ -145,7 +143,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
const account = await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Bookmarks/fetchBookmarks', account).catch(() => { await this.$store.dispatch('TimelineSpace/Contents/Bookmarks/fetchBookmarks', account).catch(() => {
this.$message({ this.$message({
message: this.$t('message.bookmark_fetch_error'), message: this.$t('message.bookmark_fetch_error'),

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import moment from 'moment' import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot' import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
}, },
name: 'directmessages', name: 'directmessages',
components: { Toot }, components: { Toot },
mixins: [reloadable],
computed: { computed: {
...mapState('TimelineSpace/Contents/DirectMessages', { ...mapState('TimelineSpace/Contents/DirectMessages', {
timeline: state => state.timeline, timeline: state => state.timeline,
@ -220,7 +218,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
await this.reloadable()
} finally { } finally {
this.$store.commit('TimelineSpace/changeLoading', false) this.$store.commit('TimelineSpace/changeLoading', false)
} }

View File

@ -31,7 +31,6 @@
<script> <script>
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import Toot from '~/src/renderer/components/organisms/Toot' import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
export default { export default {
@ -43,7 +42,6 @@ export default {
}, },
name: 'favourites', name: 'favourites',
components: { Toot }, components: { Toot },
mixins: [reloadable],
computed: { computed: {
...mapState({ ...mapState({
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar, openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
@ -140,7 +138,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
const account = await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Favourites/fetchFavourites', account).catch(() => { await this.$store.dispatch('TimelineSpace/Contents/Favourites/fetchFavourites', account).catch(() => {
this.$message({ this.$message({
message: this.$t('message.favourite_fetch_error'), message: this.$t('message.favourite_fetch_error'),

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import moment from 'moment' import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot' import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
}, },
name: 'tag', name: 'tag',
components: { Toot }, components: { Toot },
mixins: [reloadable],
props: ['tag'], props: ['tag'],
computed: { computed: {
...mapState({ ...mapState({
@ -223,7 +221,6 @@ export default {
const tag = this.tag const tag = this.tag
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Hashtag/Tag/stopStreaming') await this.$store.dispatch('TimelineSpace/Contents/Hashtag/Tag/stopStreaming')
await this.$store.dispatch('TimelineSpace/Contents/Hashtag/Tag/fetch', tag).catch(() => { await this.$store.dispatch('TimelineSpace/Contents/Hashtag/Tag/fetch', tag).catch(() => {
this.$message({ this.$message({

View File

@ -37,254 +37,243 @@
</div> </div>
</template> </template>
<script> <script lang="ts">
import { mapState, mapGetters } from 'vuex' import { defineComponent, ref, computed, onMounted, onBeforeUpdate, onBeforeUnmount, watch } from 'vue'
import moment from 'moment' import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot' import { ElMessage } from 'element-plus'
import StatusLoading from '~/src/renderer/components/organisms/StatusLoading' import { Entity } from 'megalodon'
import reloadable from '~/src/renderer/components/mixins/reloadable' import { useI18next } from 'vue3-i18next'
import { EventEmitter } from '~/src/renderer/components/event' import { useRoute } from 'vue-router'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { useStore } from '@/store'
import Toot from '@/components/organisms/Toot.vue'
import StatusLoading from '@/components/organisms/StatusLoading.vue'
import { EventEmitter } from '@/components/event'
import { ScrollPosition } from '@/components/utils/scroll'
import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Home'
import { MUTATION_TYPES as SIDE_MENU_MUTATION } from '@/store/TimelineSpace/SideMenu'
import { MUTATION_TYPES as TIMELINE_MUTATION } from '@/store/TimelineSpace'
import { MUTATION_TYPES as HEADER_MUTATION } from '@/store/TimelineSpace/HeaderMenu'
import useReloadable from '@/components/utils/reloadable'
export default { export default defineComponent({
data() {
return {
focusedId: null,
scrollPosition: null,
observer: null,
scrollTime: null,
resizeTime: null,
loadingMore: false
}
},
name: 'home', name: 'home',
components: { Toot, StatusLoading }, components: { Toot, StatusLoading },
mixins: [reloadable], setup() {
computed: { const space = 'TimelineSpace/Contents/Home'
...mapState('TimelineSpace/Contents/Home', { const store = useStore()
timeline: state => state.timeline, const route = useRoute()
lazyLoading: state => state.lazyLoading, const i18n = useI18next()
heading: state => state.heading, const { reloadable } = useReloadable(store, route, i18n)
showReblogs: state => state.showReblogs,
showReplies: state => state.showReplies, const focusedId = ref<string | null>(null)
scrolling: state => state.scrolling const scrollPosition = ref<ScrollPosition | null>(null)
}), const observer = ref<ResizeObserver | null>(null)
...mapState({ const scrollTime = ref<moment.Moment | null>(null)
backgroundColor: state => state.App.theme.background_color, const resizeTime = ref<moment.Moment | null>(null)
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar, const loadingMore = ref(false)
startReload: state => state.TimelineSpace.HeaderMenu.reload const scroller = ref<any>()
}),
...mapGetters('TimelineSpace/Modals', ['modalOpened']), const timeline = computed(() => store.state.TimelineSpace.Contents.Home.timeline)
...mapGetters('TimelineSpace/Contents/Home', ['filters']), const lazyLoading = computed(() => store.state.TimelineSpace.Contents.Home.lazyLoading)
shortcutEnabled: function () { const heading = computed(() => store.state.TimelineSpace.Contents.Home.heading)
if (this.modalOpened) { const showReblogs = computed(() => store.state.TimelineSpace.Contents.Home.showReblogs)
return false const showReplies = computed(() => store.state.TimelineSpace.Contents.Home.showReplies)
} const scrolling = computed(() => store.state.TimelineSpace.Contents.Home.scrolling)
if (!this.focusedId) { const openSideBar = computed(() => store.state.TimelineSpace.Contents.SideBar.openSideBar)
const startReload = computed(() => store.state.TimelineSpace.HeaderMenu.reload)
const modalOpened = computed(() => store.getters[`TimelineSpace/Modals/modalOpened`])
const filters = computed(() => store.getters[`${space}/filters}`])
const currentFocusedIndex = computed(() => timeline.value.findIndex(toot => focusedId.value === toot.uri + toot.id))
// const shortcutEnabled = computed(() => {
// if (modalOpened.value) {
// return false
// }
// if (!focusedId.value) {
// return true
// }
// // Sometimes toots are deleted, so perhaps focused toot don't exist.
// return currentFocusedIndex.value === -1
// })
const filteredTimeline = computed(() => {
return timeline.value.filter(toot => {
if ('url' in toot) {
if (toot.in_reply_to_id) {
return showReplies.value
} else if (toot.reblog) {
return showReblogs.value
} else {
return true return true
} }
// Sometimes toots are deleted, so perhaps focused toot don't exist.
const currentIndex = this.timeline.findIndex(toot => this.focusedId === toot.uri + toot.id)
return currentIndex === -1
},
filteredTimeline() {
return this.timeline.filter(toot => {
if (toot.in_reply_to_id) {
return this.showReplies
} else if (toot.reblog) {
return this.showReblogs
} else { } else {
return true return true
} }
}) })
}
},
mounted() {
this.$store.commit('TimelineSpace/SideMenu/changeUnreadHomeTimeline', false)
document.getElementById('scroller').addEventListener('scroll', this.onScroll)
EventEmitter.on('focus-timeline', () => {
// If focusedId does not change, we have to refresh focusedId because Toot component watch change events.
const previousFocusedId = this.focusedId
this.focusedId = 0
this.$nextTick(function () {
this.focusedId = previousFocusedId
})
}) })
if (this.heading && this.timeline.length > 0) { onMounted(() => {
this.$store.dispatch('TimelineSpace/Contents/Home/saveMarker') store.commit(`TimelineSpace/SideMenu/${SIDE_MENU_MUTATION.CHANGE_UNREAD_HOME_TIMELINE}`, false)
document.getElementById('scroller')?.addEventListener('scroll', onScroll)
if (heading.value && timeline.value.length > 0) {
store.dispatch(`${space}/${ACTION_TYPES.SAVE_MARKER}`)
} }
const el = document.getElementById('scroller') const el = document.getElementById('scroller')
this.scrollPosition = new ScrollPosition(el) if (el) {
this.scrollPosition.prepare() scrollPosition.value = new ScrollPosition(el)
scrollPosition.value.prepare()
this.observer = new ResizeObserver(() => { observer.value = new ResizeObserver(() => {
if (this.loadingMore || (this.scrollPosition && !this.heading && !this.lazyLoading && !this.scrolling)) { if (loadingMore.value || (scrollPosition.value && !heading.value && !lazyLoading.value && !scrolling.value)) {
this.resizeTime = moment() resizeTime.value = moment()
this.scrollPosition.restore() scrollPosition.value?.restore()
} }
}) })
const scrollWrapper = el.getElementsByClassName('vue-recycle-scroller__item-wrapper')[0] const scrollWrapper = el.getElementsByClassName('vue-recycle-scroller__item-wrapper')[0]
this.observer.observe(scrollWrapper) observer.value.observe(scrollWrapper)
},
beforeUpdate() {
if (this.$store.state.TimelineSpace.SideMenu.unreadHomeTimeline && this.heading) {
this.$store.commit('TimelineSpace/SideMenu/changeUnreadHomeTimeline', false)
} }
if (this.scrollPosition) { })
this.scrollPosition.prepare() onBeforeUpdate(() => {
if (store.state.TimelineSpace.SideMenu.unreadHomeTimeline && heading.value) {
store.commit(`TimelineSpace/SideMenu/${SIDE_MENU_MUTATION.CHANGE_UNREAD_HOME_TIMELINE}`, false)
} }
}, if (scrollPosition.value) {
beforeUnmount() { scrollPosition.value.prepare()
EventEmitter.off('focus-timeline')
this.observer.disconnect()
},
unmounted() {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
this.$store.commit('TimelineSpace/Contents/Home/archiveTimeline')
if (document.getElementById('scroller') !== undefined && document.getElementById('scroller') !== null) {
document.getElementById('scroller').removeEventListener('scroll', this.onScroll)
document.getElementById('scroller').scrollTop = 0
} }
}, })
watch: { onBeforeUnmount(() => {
startReload: function (newState, oldState) { observer.value?.disconnect()
if (!oldState && newState) { })
this.reload().finally(() => { watch(startReload, (newVal, oldVal) => {
this.$store.commit('TimelineSpace/HeaderMenu/changeReload', false) if (!oldVal && newVal) {
reload().finally(() => {
store.commit(`TimelineSpace/HeaderMenu/${HEADER_MUTATION.CHANGE_RELOAD}`, false)
}) })
} }
}, })
focusedId: function (newState, _oldState) {
if (newState && this.heading) { const onScroll = (event: Event) => {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', false) if (moment().diff(resizeTime.value) < 500) {
} else if (newState === null && !this.heading) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true)
}
},
timeline: {
handler(newState, _oldState) {
if (this.heading && newState.length > 0) {
this.$store.dispatch('TimelineSpace/Contents/Home/saveMarker')
}
},
deep: true
}
},
methods: {
onScroll(event) {
if (moment().diff(this.resizeTime) < 500) {
return return
} }
this.scrollTime = moment() scrollTime.value = moment()
if (!this.scrolling) { if (!scrolling.value) {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', true) store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, true)
} }
// for lazyLoading // for lazyLoading
if ( if (
event.target.clientHeight + event.target.scrollTop >= document.getElementById('scroller').scrollHeight - 10 && (event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
!this.lazyloading document.getElementById('scroller')!.scrollHeight - 10 &&
!lazyLoading.value
) { ) {
this.$store store
.dispatch('TimelineSpace/Contents/Home/lazyFetchTimeline', this.timeline[this.timeline.length - 1]) .dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, timeline.value[timeline.value.length - 1])
.then(statuses => { .then(statuses => {
if (statuses === null) { if (statuses === null) {
return return
} }
if (statuses.length > 0) { if (statuses.length > 0) {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', true) store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, true)
setTimeout(() => { setTimeout(() => {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', false) store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, false)
}, 500) }, 500)
} }
}) })
.catch(() => { .catch(() => {
this.$message({ ElMessage({
message: this.$t('message.timeline_fetch_error'), message: i18n.t('message.timeline_fetch_error'),
type: 'error' type: 'error'
}) })
}) })
} }
if (event.target.scrollTop > 10 && this.heading) { if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', false) store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, false)
} else if (event.target.scrollTop <= 5 && !this.heading) { } else if ((event.target as HTMLElement)!.scrollTop <= 5 && !heading.value) {
this.$store.commit('TimelineSpace/Contents/Home/changeHeading', true) store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
} }
setTimeout(() => { setTimeout(() => {
const now = moment() const now = moment()
if (now.diff(this.scrollTime) >= 150) { if (now.diff(scrollTime.value) >= 150) {
this.scrollTime = null scrollTime.value = null
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', false) store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, false)
} }
}, 150) }, 150)
}, }
updateToot(message) { const updateToot = (message: Entity.Status) => {
this.$store.commit('TimelineSpace/Contents/Home/updateToot', message) store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
}, }
deleteToot(message) { const deleteToot = (message: Entity.Status) => {
this.$store.commit('TimelineSpace/Contents/Home/deleteToot', message.id) store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, message.id)
}, }
fetchTimelineSince(since_id) { const fetchTimelineSince = (since_id: string) => {
this.loadingMore = true loadingMore.value = true
this.$store.dispatch('TimelineSpace/Contents/Home/fetchTimelineSince', since_id).finally(() => { store.dispatch(`${space}/${ACTION_TYPES.FETCH_TIMELINE_SINCE}`, since_id).finally(() => {
setTimeout(() => { setTimeout(() => {
this.loadingMore = false loadingMore.value = false
}, 500) }, 500)
}) })
}, }
async reload() { const reload = async () => {
this.$store.commit('TimelineSpace/changeLoading', true) store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, true)
try { try {
await this.reloadable() reloadable()
} finally { } finally {
this.$store.commit('TimelineSpace/changeLoading', false) store.commit(`TimelineSpace/${TIMELINE_MUTATION.CHANGE_LOADING}`, false)
} }
},
upper() {
this.$refs.scroller.scrollToItem(0)
this.focusedId = null
},
focusNext() {
const currentIndex = this.timeline.findIndex(toot => this.focusedId === toot.uri + toot.id)
if (currentIndex === -1) {
this.focusedId = this.timeline[0].uri + this.timeline[0].id
} else if (currentIndex < this.timeline.length) {
this.focusedId = this.timeline[currentIndex + 1].uri + this.timeline[currentIndex + 1].id
} }
}, const upper = () => {
focusPrev() { scroller.value.scrollToItem(0)
const currentIndex = this.timeline.findIndex(toot => this.focusedId === toot.uri + toot.id) focusedId.value = null
if (currentIndex === 0) {
this.focusedId = null
} else if (currentIndex > 0) {
this.focusedId = this.timeline[currentIndex - 1].uri + this.timeline[currentIndex - 1].id
} }
}, const focusNext = () => {
focusToot(message) { if (currentFocusedIndex.value === -1) {
this.focusedId = message.uri + message.id focusedId.value = timeline.value[0].uri + timeline.value[0].id
}, } else if (currentFocusedIndex.value < timeline.value.length) {
focusSidebar() { focusedId.value = timeline.value[currentFocusedIndex.value + 1].uri + timeline.value[currentFocusedIndex.value + 1].id
}
}
const focusPrev = () => {
if (currentFocusedIndex.value === 0) {
focusedId.value = null
} else if (currentFocusedIndex.value > 0) {
focusedId.value = timeline.value[currentFocusedIndex.value - 1].uri + timeline.value[currentFocusedIndex.value - 1].id
}
}
const focusToot = (message: Entity.Status) => {
focusedId.value = message.uri + message.id
}
const focusSidebar = () => {
EventEmitter.emit('focus-sidebar') EventEmitter.emit('focus-sidebar')
},
handleKey(event) {
switch (event.srcKey) {
case 'next':
this.focusedId = this.timeline[0].uri + this.timeline[0].id
break
} }
}, const sizeChanged = () => {
sizeChanged() { store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, true)
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', true)
setTimeout(() => { setTimeout(() => {
this.$store.commit('TimelineSpace/Contents/Home/changeScrolling', false) store.commit(`${space}/${MUTATION_TYPES.CHANGE_SCROLLING}`, false)
}, 500) }, 500)
} }
return {
filteredTimeline,
scroller,
loadingMore,
fetchTimelineSince,
focusedId,
modalOpened,
filters,
updateToot,
deleteToot,
focusNext,
focusPrev,
focusSidebar,
focusToot,
sizeChanged,
openSideBar,
heading,
upper
} }
} }
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import moment from 'moment' import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot' import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -50,7 +49,6 @@ export default {
name: 'list', name: 'list',
props: ['list_id'], props: ['list_id'],
components: { Toot }, components: { Toot },
mixins: [reloadable],
computed: { computed: {
...mapState({ ...mapState({
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar, openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
@ -223,7 +221,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
await this.reloadable()
await this.$store.dispatch('TimelineSpace/Contents/Lists/Show/stopStreaming') await this.$store.dispatch('TimelineSpace/Contents/Lists/Show/stopStreaming')
await this.$store.dispatch('TimelineSpace/Contents/Lists/Show/fetchTimeline', this.list_id).catch(() => { await this.$store.dispatch('TimelineSpace/Contents/Lists/Show/fetchTimeline', this.list_id).catch(() => {
this.$message({ this.$message({

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import moment from 'moment' import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot' import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
}, },
name: 'local', name: 'local',
components: { Toot }, components: { Toot },
mixins: [reloadable],
computed: { computed: {
...mapState('TimelineSpace/Contents/Local', { ...mapState('TimelineSpace/Contents/Local', {
timeline: state => state.timeline, timeline: state => state.timeline,
@ -219,7 +217,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
await this.reloadable()
} finally { } finally {
this.$store.commit('TimelineSpace/changeLoading', false) this.$store.commit('TimelineSpace/changeLoading', false)
} }

View File

@ -40,7 +40,6 @@ import { mapState, mapGetters } from 'vuex'
import moment from 'moment' import moment from 'moment'
import Notification from '~/src/renderer/components/organisms/Notification' import Notification from '~/src/renderer/components/organisms/Notification'
import StatusLoading from '~/src/renderer/components/organisms/StatusLoading' import StatusLoading from '~/src/renderer/components/organisms/StatusLoading'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -57,7 +56,6 @@ export default {
}, },
name: 'mentions', name: 'mentions',
components: { Notification, StatusLoading }, components: { Notification, StatusLoading },
mixins: [reloadable],
computed: { computed: {
...mapState('App', { ...mapState('App', {
backgroundColor: state => state.theme.background_color backgroundColor: state => state.theme.background_color
@ -222,7 +220,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
await this.reloadable()
} finally { } finally {
this.$store.commit('TimelineSpace/changeLoading', false) this.$store.commit('TimelineSpace/changeLoading', false)
} }

View File

@ -39,7 +39,6 @@ import { mapState, mapGetters } from 'vuex'
import moment from 'moment' import moment from 'moment'
import Notification from '~/src/renderer/components/organisms/Notification' import Notification from '~/src/renderer/components/organisms/Notification'
import StatusLoading from '~/src/renderer/components/organisms/StatusLoading' import StatusLoading from '~/src/renderer/components/organisms/StatusLoading'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -56,7 +55,6 @@ export default {
}, },
name: 'notifications', name: 'notifications',
components: { Notification, StatusLoading }, components: { Notification, StatusLoading },
mixins: [reloadable],
computed: { computed: {
...mapState({ ...mapState({
openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar, openSideBar: state => state.TimelineSpace.Contents.SideBar.openSideBar,
@ -224,7 +222,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
await this.reloadable()
this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge') this.$store.dispatch('TimelineSpace/Contents/Notifications/resetBadge')
} finally { } finally {
this.$store.commit('TimelineSpace/changeLoading', false) this.$store.commit('TimelineSpace/changeLoading', false)

View File

@ -33,7 +33,6 @@
import { mapState, mapGetters } from 'vuex' import { mapState, mapGetters } from 'vuex'
import moment from 'moment' import moment from 'moment'
import Toot from '~/src/renderer/components/organisms/Toot' import Toot from '~/src/renderer/components/organisms/Toot'
import reloadable from '~/src/renderer/components/mixins/reloadable'
import { EventEmitter } from '~/src/renderer/components/event' import { EventEmitter } from '~/src/renderer/components/event'
import { ScrollPosition } from '~/src/renderer/components/utils/scroll' import { ScrollPosition } from '~/src/renderer/components/utils/scroll'
@ -49,7 +48,6 @@ export default {
}, },
name: 'public', name: 'public',
components: { Toot }, components: { Toot },
mixins: [reloadable],
computed: { computed: {
...mapState('TimelineSpace/Contents/Public', { ...mapState('TimelineSpace/Contents/Public', {
timeline: state => state.timeline, timeline: state => state.timeline,
@ -221,7 +219,6 @@ export default {
async reload() { async reload() {
this.$store.commit('TimelineSpace/changeLoading', true) this.$store.commit('TimelineSpace/changeLoading', true)
try { try {
await this.reloadable()
} finally { } finally {
this.$store.commit('TimelineSpace/changeLoading', false) this.$store.commit('TimelineSpace/changeLoading', false)
} }

View File

@ -1,24 +0,0 @@
<script>
export default {
name: 'reloadable',
methods: {
async reloadable() {
const account = await this.$store
.dispatch('TimelineSpace/localAccount', this.$route.params.id)
.catch((err) => {
this.$message({
message: this.$t('message.account_load_error'),
type: 'error',
})
throw err
})
await this.$store.dispatch('GlobalHeader/stopUserStreamings')
await this.$store.dispatch('TimelineSpace/stopStreamings')
await this.$store.dispatch('TimelineSpace/fetchContentsTimelines')
await this.$store.dispatch('TimelineSpace/startStreamings')
this.$store.dispatch('GlobalHeader/startUserStreamings')
return account
},
},
}
</script>

View File

@ -0,0 +1,29 @@
import { Store } from 'vuex'
import { RootState } from '@/store'
import { RouteLocationNormalizedLoaded } from 'vue-router'
import { i18n } from 'i18next'
import { ElMessage } from 'element-plus'
import { ACTION_TYPES } from '@/store/TimelineSpace'
import { ACTION_TYPES as GLOBAL_ACTION } from '@/store/GlobalHeader'
export default function useReloadable(store: Store<RootState>, route: RouteLocationNormalizedLoaded, i18next: i18n) {
async function reloadable() {
const account = await store.dispatch(`TimelineSpace/${ACTION_TYPES.LOCAL_ACCOUNT}`, route.params.id).catch(err => {
ElMessage({
message: i18next.t('message.account_load_error'),
type: 'error'
})
throw err
})
await store.dispatch(`GlobalHeader/${GLOBAL_ACTION.STOP_USER_STREAMINGS}`)
await store.dispatch(`TimelineSpace/${ACTION_TYPES.STOP_STREAMINGS}`)
await store.dispatch(`TimelineSpace/${ACTION_TYPES.FETCH_CONTENTS_TIMELINES}`)
await store.dispatch(`TimelineSpace/${ACTION_TYPES.START_STREAMINGS}`)
store.dispatch(`GlobalHeader/${GLOBAL_ACTION.START_USER_STREAMINGS}`)
return account
}
return {
reloadable
}
}

View File

@ -92,8 +92,46 @@ const mutations: MutationTree<TimelineSpaceState> = {
} }
} }
export const ACTION_TYPES = {
INIT_LOAD: 'initLoad',
PREPARE_SPACE: 'prepareSpace',
LOCAL_ACCOUNT: 'localAccount',
FETCH_ACCOUNT: 'fetchAccount',
CLEAR_ACCOUNT: 'clearAccount',
DETECT_SNS: 'detectSNS',
WATCH_SHORTCUT_EVENTS: 'watchShortcutEvents',
REMOVE_SHORTCUT_EVENTS: 'removeShortcutEvents',
CLEAR_UNREAD: 'clearUnread',
FETCH_EMOJIS: 'fetchEmojis',
FETCH_FILTERS: 'fetchFilters',
FETCH_INSTANCE: 'fetchInstance',
LOAD_TIMELINE_SETTING: 'loadTimelineSetting',
FETCH_CONTENTS_TIMELINES: 'fetchContentsTimelines',
CLEAR_CONTENTS_TIMELINES: 'clearContentsTimelines',
BIND_STREAMINGS: 'bindStreamings',
START_STREAMINGS: 'startStreamings',
STOP_STREAMINGS: 'stopStreamings',
UNBIND_STREAMINGS: 'unbindStreamings',
BIND_USER_STREAMING: 'bindUserStreaming',
BIND_LOCAL_STREAMING: 'bindLocalStreaming',
START_LOCAL_STREAMING: 'startLocalStreaming',
BIND_PUBLIC_STREAMING: 'bindPublicStreaming',
START_PUBLIC_STREAMING: 'startPublicStreaming',
BIND_DIRECT_MESSAGES_STREAMING: 'bindDirectMessagesStreaming',
START_DIRECT_MESSAGES_STREAMING: 'startDirectMessagesStreaming',
UNBIND_USER_STREAMING: 'unbindUserStreaming',
UNBIND_LOCAL_STREAMING: 'unbindLocalStreaming',
STOP_LOCAL_STREAMING: 'stopLocalStreaming',
UNBIND_PUBLIC_STREAMING: 'unbindPublicStreaming',
STOP_PUBLIC_STREAMING: 'stopPublicStreaming',
UNBIND_DIRECT_MESSAGES_STREAMING: 'unbindDirectMessagesStreaming',
STOP_DIRECT_MESSAGES_STREAMING: 'stopDirectMessagesStreaming',
UPDATE_TOOT_FOR_ALL_TIMELINES: 'updateTootForAllTimelines',
WAIT_TO_UNBIND_USER_STREAMING: 'waitToUnbindUserStreaming'
}
const actions: ActionTree<TimelineSpaceState, RootState> = { const actions: ActionTree<TimelineSpaceState, RootState> = {
initLoad: async ({ dispatch, commit }, accountId: string): Promise<LocalAccount> => { [ACTION_TYPES.INIT_LOAD]: async ({ dispatch, commit }, accountId: string): Promise<LocalAccount> => {
commit(MUTATION_TYPES.CHANGE_LOADING, true) commit(MUTATION_TYPES.CHANGE_LOADING, true)
dispatch('watchShortcutEvents') dispatch('watchShortcutEvents')
const account: LocalAccount = await dispatch('localAccount', accountId).catch(_ => { const account: LocalAccount = await dispatch('localAccount', accountId).catch(_ => {
@ -113,7 +151,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
}) })
return account return account
}, },
prepareSpace: async ({ state, dispatch }) => { [ACTION_TYPES.PREPARE_SPACE]: async ({ state, dispatch }) => {
await dispatch('bindStreamings') await dispatch('bindStreamings')
dispatch('startStreamings') dispatch('startStreamings')
await dispatch('fetchEmojis', state.account) await dispatch('fetchEmojis', state.account)
@ -124,7 +162,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
// ------------------------------------------------- // -------------------------------------------------
// Accounts // Accounts
// ------------------------------------------------- // -------------------------------------------------
localAccount: async ({ dispatch, commit }, id: string): Promise<LocalAccount> => { [ACTION_TYPES.LOCAL_ACCOUNT]: async ({ dispatch, commit }, id: string): Promise<LocalAccount> => {
const account: LocalAccount = await win.ipcRenderer.invoke('get-local-account', id) const account: LocalAccount = await win.ipcRenderer.invoke('get-local-account', id)
if (account.username === undefined || account.username === null || account.username === '') { if (account.username === undefined || account.username === null || account.username === '') {
const acct: LocalAccount = await dispatch('fetchAccount', account) const acct: LocalAccount = await dispatch('fetchAccount', account)
@ -135,22 +173,22 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
return account return account
} }
}, },
fetchAccount: async (_, account: LocalAccount): Promise<LocalAccount> => { [ACTION_TYPES.FETCH_ACCOUNT]: async (_, account: LocalAccount): Promise<LocalAccount> => {
const acct: LocalAccount = await win.ipcRenderer.invoke('update-account', account) const acct: LocalAccount = await win.ipcRenderer.invoke('update-account', account)
return acct return acct
}, },
clearAccount: async ({ commit }) => { [ACTION_TYPES.CLEAR_ACCOUNT]: async ({ commit }) => {
commit(MUTATION_TYPES.UPDATE_ACCOUNT, blankAccount) commit(MUTATION_TYPES.UPDATE_ACCOUNT, blankAccount)
return true return true
}, },
detectSNS: async ({ commit, state }) => { [ACTION_TYPES.DETECT_SNS]: async ({ commit, state }) => {
const sns = await detector(state.account.baseURL) const sns = await detector(state.account.baseURL)
commit(MUTATION_TYPES.CHANGE_SNS, sns) commit(MUTATION_TYPES.CHANGE_SNS, sns)
}, },
// ----------------------------------------------- // -----------------------------------------------
// Shortcuts // Shortcuts
// ----------------------------------------------- // -----------------------------------------------
watchShortcutEvents: ({ commit, dispatch }) => { [ACTION_TYPES.WATCH_SHORTCUT_EVENTS]: ({ commit, dispatch }) => {
win.ipcRenderer.on('CmdOrCtrl+N', () => { win.ipcRenderer.on('CmdOrCtrl+N', () => {
dispatch('TimelineSpace/Modals/NewToot/openModal', {}, { root: true }) dispatch('TimelineSpace/Modals/NewToot/openModal', {}, { root: true })
}) })
@ -161,7 +199,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Modals/Shortcut/changeModal', true, { root: true }) commit('TimelineSpace/Modals/Shortcut/changeModal', true, { root: true })
}) })
}, },
removeShortcutEvents: async () => { [ACTION_TYPES.REMOVE_SHORTCUT_EVENTS]: async () => {
win.ipcRenderer.removeAllListeners('CmdOrCtrl+N') win.ipcRenderer.removeAllListeners('CmdOrCtrl+N')
win.ipcRenderer.removeAllListeners('CmdOrCtrl+K') win.ipcRenderer.removeAllListeners('CmdOrCtrl+K')
return true return true
@ -169,13 +207,13 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
/** /**
* clearUnread * clearUnread
*/ */
clearUnread: async ({ dispatch }) => { [ACTION_TYPES.CLEAR_UNREAD]: async ({ dispatch }) => {
dispatch('TimelineSpace/SideMenu/clearUnread', {}, { root: true }) dispatch('TimelineSpace/SideMenu/clearUnread', {}, { root: true })
}, },
/** /**
* fetchEmojis * fetchEmojis
*/ */
fetchEmojis: async ({ commit, state }, account: LocalAccount): Promise<Array<Entity.Emoji>> => { [ACTION_TYPES.FETCH_EMOJIS]: async ({ commit, state }, account: LocalAccount): Promise<Array<Entity.Emoji>> => {
const client = generator(state.sns, account.baseURL, null, 'Whalebird') const client = generator(state.sns, account.baseURL, null, 'Whalebird')
const res = await client.getInstanceCustomEmojis() const res = await client.getInstanceCustomEmojis()
commit(MUTATION_TYPES.UPDATE_EMOJIS, res.data) commit(MUTATION_TYPES.UPDATE_EMOJIS, res.data)
@ -184,7 +222,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
/** /**
* fetchFilters * fetchFilters
*/ */
fetchFilters: async ({ commit, state, rootState }): Promise<Array<Entity.Filter>> => { [ACTION_TYPES.FETCH_FILTERS]: async ({ commit, state, rootState }): Promise<Array<Entity.Filter>> => {
try { try {
const client = generator(state.sns, state.account.baseURL, state.account.accessToken, rootState.App.userAgent) const client = generator(state.sns, state.account.baseURL, state.account.accessToken, rootState.App.userAgent)
const res = await client.getFilters() const res = await client.getFilters()
@ -197,17 +235,17 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
/** /**
* fetchInstance * fetchInstance
*/ */
fetchInstance: async ({ commit, state }, account: LocalAccount) => { [ACTION_TYPES.FETCH_INSTANCE]: async ({ commit, state }, account: LocalAccount) => {
const client = generator(state.sns, account.baseURL, null, 'Whalebird') const client = generator(state.sns, account.baseURL, null, 'Whalebird')
const res = await client.getInstance() const res = await client.getInstance()
commit(MUTATION_TYPES.UPDATE_TOOT_MAX, res.data.max_toot_chars) commit(MUTATION_TYPES.UPDATE_TOOT_MAX, res.data.max_toot_chars)
return true return true
}, },
loadTimelineSetting: async ({ commit }, accountID: string) => { [ACTION_TYPES.LOAD_TIMELINE_SETTING]: async ({ commit }, accountID: string) => {
const setting: Setting = await win.ipcRenderer.invoke('get-account-setting', accountID) const setting: Setting = await win.ipcRenderer.invoke('get-account-setting', accountID)
commit(MUTATION_TYPES.UPDATE_TIMELINE_SETTING, setting.timeline) commit(MUTATION_TYPES.UPDATE_TIMELINE_SETTING, setting.timeline)
}, },
fetchContentsTimelines: async ({ dispatch, state }) => { [ACTION_TYPES.FETCH_CONTENTS_TIMELINES]: async ({ dispatch, state }) => {
dispatch('TimelineSpace/Contents/changeLoading', true, { root: true }) dispatch('TimelineSpace/Contents/changeLoading', true, { root: true })
await dispatch('TimelineSpace/Contents/Home/fetchTimeline', {}, { root: true }).finally(() => { await dispatch('TimelineSpace/Contents/Home/fetchTimeline', {}, { root: true }).finally(() => {
dispatch('TimelineSpace/Contents/changeLoading', false, { root: true }) dispatch('TimelineSpace/Contents/changeLoading', false, { root: true })
@ -225,7 +263,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
await dispatch('TimelineSpace/Contents/Public/fetchPublicTimeline', {}, { root: true }) await dispatch('TimelineSpace/Contents/Public/fetchPublicTimeline', {}, { root: true })
} }
}, },
clearContentsTimelines: ({ commit }) => { [ACTION_TYPES.CLEAR_CONTENTS_TIMELINES]: ({ commit }) => {
commit('TimelineSpace/Contents/Home/clearTimeline', {}, { root: true }) commit('TimelineSpace/Contents/Home/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/Local/clearTimeline', {}, { root: true }) commit('TimelineSpace/Contents/Local/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/DirectMessages/clearTimeline', {}, { root: true }) commit('TimelineSpace/Contents/DirectMessages/clearTimeline', {}, { root: true })
@ -233,7 +271,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Public/clearTimeline', {}, { root: true }) commit('TimelineSpace/Contents/Public/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/Mentions/clearMentions', {}, { root: true }) commit('TimelineSpace/Contents/Mentions/clearMentions', {}, { root: true })
}, },
bindStreamings: ({ dispatch, state }) => { [ACTION_TYPES.BIND_STREAMINGS]: ({ dispatch, state }) => {
dispatch('bindUserStreaming') dispatch('bindUserStreaming')
if (state.timelineSetting.unreadNotification.direct) { if (state.timelineSetting.unreadNotification.direct) {
dispatch('bindDirectMessagesStreaming') dispatch('bindDirectMessagesStreaming')
@ -245,7 +283,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
dispatch('bindPublicStreaming') dispatch('bindPublicStreaming')
} }
}, },
startStreamings: ({ dispatch, state }) => { [ACTION_TYPES.START_STREAMINGS]: ({ dispatch, state }) => {
if (state.timelineSetting.unreadNotification.direct) { if (state.timelineSetting.unreadNotification.direct) {
dispatch('startDirectMessagesStreaming') dispatch('startDirectMessagesStreaming')
} }
@ -256,12 +294,12 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
dispatch('startPublicStreaming') dispatch('startPublicStreaming')
} }
}, },
stopStreamings: ({ dispatch }) => { [ACTION_TYPES.STOP_STREAMINGS]: ({ dispatch }) => {
dispatch('stopDirectMessagesStreaming') dispatch('stopDirectMessagesStreaming')
dispatch('stopLocalStreaming') dispatch('stopLocalStreaming')
dispatch('stopPublicStreaming') dispatch('stopPublicStreaming')
}, },
unbindStreamings: ({ dispatch }) => { [ACTION_TYPES.UNBIND_STREAMINGS]: ({ dispatch }) => {
dispatch('unbindUserStreaming') dispatch('unbindUserStreaming')
dispatch('unbindDirectMessagesStreaming') dispatch('unbindDirectMessagesStreaming')
dispatch('unbindLocalStreaming') dispatch('unbindLocalStreaming')
@ -270,7 +308,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
// ------------------------------------------------ // ------------------------------------------------
// Each streaming methods // Each streaming methods
// ------------------------------------------------ // ------------------------------------------------
bindUserStreaming: async ({ commit, state, rootState, dispatch }) => { [ACTION_TYPES.BIND_USER_STREAMING]: async ({ commit, state, rootState, dispatch }) => {
if (!state.account._id) { if (!state.account._id) {
throw new Error('Account is not set') throw new Error('Account is not set')
} }
@ -306,7 +344,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Mentions/deleteToot', id, { root: true }) commit('TimelineSpace/Contents/Mentions/deleteToot', id, { root: true })
}) })
}, },
bindLocalStreaming: ({ commit, rootState }) => { [ACTION_TYPES.BIND_LOCAL_STREAMING]: ({ commit, rootState }) => {
win.ipcRenderer.on('update-start-local-streaming', (_, update: Entity.Status) => { win.ipcRenderer.on('update-start-local-streaming', (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Local/appendTimeline', update, { root: true }) commit('TimelineSpace/Contents/Local/appendTimeline', update, { root: true })
if (rootState.TimelineSpace.Contents.Local.heading && Math.random() > 0.8) { if (rootState.TimelineSpace.Contents.Local.heading && Math.random() > 0.8) {
@ -318,7 +356,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Local/deleteToot', id, { root: true }) commit('TimelineSpace/Contents/Local/deleteToot', id, { root: true })
}) })
}, },
startLocalStreaming: ({ state }) => { [ACTION_TYPES.START_LOCAL_STREAMING]: ({ state }) => {
// @ts-ignore // @ts-ignore
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars // eslint-disable-line no-unused-vars
@ -328,7 +366,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
}) })
}) })
}, },
bindPublicStreaming: ({ commit, rootState }) => { [ACTION_TYPES.BIND_PUBLIC_STREAMING]: ({ commit, rootState }) => {
win.ipcRenderer.on('update-start-public-streaming', (_, update: Entity.Status) => { win.ipcRenderer.on('update-start-public-streaming', (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Public/appendTimeline', update, { root: true }) commit('TimelineSpace/Contents/Public/appendTimeline', update, { root: true })
if (rootState.TimelineSpace.Contents.Public.heading && Math.random() > 0.8) { if (rootState.TimelineSpace.Contents.Public.heading && Math.random() > 0.8) {
@ -340,7 +378,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/Public/deleteToot', id, { root: true }) commit('TimelineSpace/Contents/Public/deleteToot', id, { root: true })
}) })
}, },
startPublicStreaming: ({ state }) => { [ACTION_TYPES.START_PUBLIC_STREAMING]: ({ state }) => {
// @ts-ignore // @ts-ignore
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars // eslint-disable-line no-unused-vars
@ -350,7 +388,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
}) })
}) })
}, },
bindDirectMessagesStreaming: ({ commit, rootState }) => { [ACTION_TYPES.BIND_DIRECT_MESSAGES_STREAMING]: ({ commit, rootState }) => {
win.ipcRenderer.on('update-start-directmessages-streaming', (_, update: Entity.Status) => { win.ipcRenderer.on('update-start-directmessages-streaming', (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/DirectMessages/appendTimeline', update, { root: true }) commit('TimelineSpace/Contents/DirectMessages/appendTimeline', update, { root: true })
if (rootState.TimelineSpace.Contents.DirectMessages.heading && Math.random() > 0.8) { if (rootState.TimelineSpace.Contents.DirectMessages.heading && Math.random() > 0.8) {
@ -362,7 +400,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
commit('TimelineSpace/Contents/DirectMessages/deleteToot', id, { root: true }) commit('TimelineSpace/Contents/DirectMessages/deleteToot', id, { root: true })
}) })
}, },
startDirectMessagesStreaming: ({ state }) => { [ACTION_TYPES.START_DIRECT_MESSAGES_STREAMING]: ({ state }) => {
// @ts-ignore // @ts-ignore
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// eslint-disable-line no-unused-vars // eslint-disable-line no-unused-vars
@ -372,7 +410,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
}) })
}) })
}, },
unbindUserStreaming: ({ state, commit }) => { [ACTION_TYPES.UNBIND_USER_STREAMING]: ({ state, commit }) => {
// When unbind is called, sometimes account is already cleared and account does not have _id. // When unbind is called, sometimes account is already cleared and account does not have _id.
// So we have to get previous account to unbind streamings. // So we have to get previous account to unbind streamings.
if (state.bindingAccount) { if (state.bindingAccount) {
@ -386,31 +424,31 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
console.info('binding account does not exist') console.info('binding account does not exist')
} }
}, },
unbindLocalStreaming: () => { [ACTION_TYPES.UNBIND_LOCAL_STREAMING]: () => {
win.ipcRenderer.removeAllListeners('error-start-local-streaming') win.ipcRenderer.removeAllListeners('error-start-local-streaming')
win.ipcRenderer.removeAllListeners('update-start-local-streaming') win.ipcRenderer.removeAllListeners('update-start-local-streaming')
win.ipcRenderer.removeAllListeners('delete-start-local-streaming') win.ipcRenderer.removeAllListeners('delete-start-local-streaming')
}, },
stopLocalStreaming: () => { [ACTION_TYPES.STOP_LOCAL_STREAMING]: () => {
win.ipcRenderer.send('stop-local-streaming') win.ipcRenderer.send('stop-local-streaming')
}, },
unbindPublicStreaming: () => { [ACTION_TYPES.UNBIND_PUBLIC_STREAMING]: () => {
win.ipcRenderer.removeAllListeners('error-start-public-streaming') win.ipcRenderer.removeAllListeners('error-start-public-streaming')
win.ipcRenderer.removeAllListeners('update-start-public-streaming') win.ipcRenderer.removeAllListeners('update-start-public-streaming')
win.ipcRenderer.removeAllListeners('delete-start-public-streaming') win.ipcRenderer.removeAllListeners('delete-start-public-streaming')
}, },
stopPublicStreaming: () => { [ACTION_TYPES.STOP_PUBLIC_STREAMING]: () => {
win.ipcRenderer.send('stop-public-streaming') win.ipcRenderer.send('stop-public-streaming')
}, },
unbindDirectMessagesStreaming: () => { [ACTION_TYPES.UNBIND_DIRECT_MESSAGES_STREAMING]: () => {
win.ipcRenderer.removeAllListeners('error-start-directmessages-streaming') win.ipcRenderer.removeAllListeners('error-start-directmessages-streaming')
win.ipcRenderer.removeAllListeners('update-start-directmessages-streaming') win.ipcRenderer.removeAllListeners('update-start-directmessages-streaming')
win.ipcRenderer.removeAllListeners('delete-start-directmessages-streaming') win.ipcRenderer.removeAllListeners('delete-start-directmessages-streaming')
}, },
stopDirectMessagesStreaming: () => { [ACTION_TYPES.STOP_DIRECT_MESSAGES_STREAMING]: () => {
win.ipcRenderer.send('stop-directmessages-streaming') win.ipcRenderer.send('stop-directmessages-streaming')
}, },
updateTootForAllTimelines: ({ commit, state }, status: Entity.Status): boolean => { [ACTION_TYPES.UPDATE_TOOT_FOR_ALL_TIMELINES]: ({ commit, state }, status: Entity.Status): boolean => {
commit('TimelineSpace/Contents/Home/updateToot', status, { root: true }) commit('TimelineSpace/Contents/Home/updateToot', status, { root: true })
commit('TimelineSpace/Contents/Notifications/updateToot', status, { root: true }) commit('TimelineSpace/Contents/Notifications/updateToot', status, { root: true })
commit('TimelineSpace/Contents/Mentions/updateToot', status, { root: true }) commit('TimelineSpace/Contents/Mentions/updateToot', status, { root: true })
@ -425,7 +463,7 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
} }
return true return true
}, },
waitToUnbindUserStreaming: async ({ state, dispatch }): Promise<boolean> => { [ACTION_TYPES.WAIT_TO_UNBIND_USER_STREAMING]: async ({ state, dispatch }): Promise<boolean> => {
if (!state.bindingAccount) { if (!state.bindingAccount) {
return true return true
} }

View File

@ -5,7 +5,7 @@ import { LocalMarker } from '~/src/types/localMarker'
import { MyWindow } from '~/src/types/global' import { MyWindow } from '~/src/types/global'
import { LoadingCard } from '@/types/loading-card' import { LoadingCard } from '@/types/loading-card'
const win = (window as any) as MyWindow const win = window as any as MyWindow
export type HomeState = { export type HomeState = {
lazyLoading: boolean lazyLoading: boolean
@ -122,8 +122,16 @@ const mutations: MutationTree<HomeState> = {
} }
} }
export const ACTION_TYPES = {
FETCH_TIMELINE: 'fetchTimeline',
LAZY_FETCH_TIMELINE: 'lazyFetchTimeline',
FETCH_TIMELINE_SINCE: 'fetchTimelineSince',
GET_MARKER: 'getMarker',
SAVE_MARKER: 'saveMarker'
}
const actions: ActionTree<HomeState, RootState> = { const actions: ActionTree<HomeState, RootState> = {
fetchTimeline: async ({ dispatch, commit, rootState }) => { [ACTION_TYPES.FETCH_TIMELINE]: async ({ dispatch, commit, rootState }) => {
const client = generator( const client = generator(
rootState.TimelineSpace.sns, rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL, rootState.TimelineSpace.account.baseURL,
@ -149,7 +157,8 @@ const actions: ActionTree<HomeState, RootState> = {
// If the number of unread statuses is less than 40, max_id is necessary, but it is enough to reject duplicated statuses. // If the number of unread statuses is less than 40, max_id is necessary, but it is enough to reject duplicated statuses.
// So we do it in mutation. // So we do it in mutation.
max_id: null, max_id: null,
id: 'loading-card' id: 'loading-card',
uri: 'loading-card'
} }
const res = await client.getHomeTimeline({ limit: 40, max_id: lastReadStatus.id }) const res = await client.getHomeTimeline({ limit: 40, max_id: lastReadStatus.id })
@ -168,7 +177,10 @@ const actions: ActionTree<HomeState, RootState> = {
return res.data return res.data
} }
}, },
lazyFetchTimeline: async ({ state, commit, rootState }, lastStatus: Entity.Status): Promise<Array<Entity.Status> | null> => { [ACTION_TYPES.LAZY_FETCH_TIMELINE]: async (
{ state, commit, rootState },
lastStatus: Entity.Status
): Promise<Array<Entity.Status> | null> => {
if (state.lazyLoading) { if (state.lazyLoading) {
return Promise.resolve(null) return Promise.resolve(null)
} }
@ -189,7 +201,7 @@ const actions: ActionTree<HomeState, RootState> = {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false) commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
}) })
}, },
fetchTimelineSince: async ({ state, rootState, commit }, since_id: string): Promise<Array<Entity.Status> | null> => { [ACTION_TYPES.FETCH_TIMELINE_SINCE]: async ({ state, rootState, commit }, since_id: string): Promise<Array<Entity.Status> | null> => {
const client = generator( const client = generator(
rootState.TimelineSpace.sns, rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL, rootState.TimelineSpace.account.baseURL,
@ -229,7 +241,8 @@ const actions: ActionTree<HomeState, RootState> = {
type: 'middle-load', type: 'middle-load',
since_id: res.data[0].id, since_id: res.data[0].id,
max_id: maxID, max_id: maxID,
id: 'loading-card' id: 'loading-card',
uri: 'loading-card'
} }
let timeline: Array<Entity.Status | LoadingCard> = [card] let timeline: Array<Entity.Status | LoadingCard> = [card]
timeline = timeline.concat(res.data) timeline = timeline.concat(res.data)
@ -239,7 +252,7 @@ const actions: ActionTree<HomeState, RootState> = {
} }
return res.data return res.data
}, },
getMarker: async ({ rootState }): Promise<LocalMarker | null> => { [ACTION_TYPES.GET_MARKER]: async ({ rootState }): Promise<LocalMarker | null> => {
if (!rootState.TimelineSpace.timelineSetting.useMarker.home) { if (!rootState.TimelineSpace.timelineSetting.useMarker.home) {
return null return null
} }
@ -265,7 +278,7 @@ const actions: ActionTree<HomeState, RootState> = {
const localMarker: LocalMarker | null = await win.ipcRenderer.invoke('get-home-marker', rootState.TimelineSpace.account._id) const localMarker: LocalMarker | null = await win.ipcRenderer.invoke('get-home-marker', rootState.TimelineSpace.account._id)
return localMarker return localMarker
}, },
saveMarker: async ({ state, rootState }) => { [ACTION_TYPES.SAVE_MARKER]: async ({ state, rootState }) => {
const timeline = state.timeline const timeline = state.timeline
if (timeline.length === 0 || timeline[0].id === 'loading-card') { if (timeline.length === 0 || timeline[0].id === 'loading-card') {
return return

View File

@ -138,7 +138,8 @@ const actions: ActionTree<MentionsState, RootState> = {
type: 'middle-load', type: 'middle-load',
since_id: localMarker.last_read_id, since_id: localMarker.last_read_id,
max_id: null, max_id: null,
id: 'loading-card' id: 'loading-card',
uri: 'loading-card'
} }
let mentions: Array<Entity.Notification | LoadingCard> = [card] let mentions: Array<Entity.Notification | LoadingCard> = [card]
const res = await client.getNotifications({ limit: 30, max_id: nextResponse.data[0].id, exclude_types: excludes }) const res = await client.getNotifications({ limit: 30, max_id: nextResponse.data[0].id, exclude_types: excludes })
@ -211,7 +212,8 @@ const actions: ActionTree<MentionsState, RootState> = {
type: 'middle-load', type: 'middle-load',
since_id: res.data[0].id, since_id: res.data[0].id,
max_id: maxID, max_id: maxID,
id: 'loading-card' id: 'loading-card',
uri: 'loading-card'
} }
let mentions: Array<Entity.Notification | LoadingCard> = [card] let mentions: Array<Entity.Notification | LoadingCard> = [card]
mentions = mentions.concat(res.data) mentions = mentions.concat(res.data)

View File

@ -5,7 +5,7 @@ import { LocalMarker } from '~/src/types/localMarker'
import { MyWindow } from '~/src/types/global' import { MyWindow } from '~/src/types/global'
import { LoadingCard } from '@/types/loading-card' import { LoadingCard } from '@/types/loading-card'
const win = (window as any) as MyWindow const win = window as any as MyWindow
export type NotificationsState = { export type NotificationsState = {
lazyLoading: boolean lazyLoading: boolean
@ -135,7 +135,8 @@ const actions: ActionTree<NotificationsState, RootState> = {
// If the number of unread statuses is less than 30, max_id is necessary, but it is enough to reject duplicated statuses. // If the number of unread statuses is less than 30, max_id is necessary, but it is enough to reject duplicated statuses.
// So we do it in mutation. // So we do it in mutation.
max_id: null, max_id: null,
id: 'loading-card' id: 'loading-card',
uri: 'loading-card'
} }
let notifications: Array<Entity.Notification | LoadingCard> = [card] let notifications: Array<Entity.Notification | LoadingCard> = [card]
const res = await client.getNotifications({ limit: 30, max_id: nextResponse.data[0].id }) const res = await client.getNotifications({ limit: 30, max_id: nextResponse.data[0].id })
@ -203,7 +204,8 @@ const actions: ActionTree<NotificationsState, RootState> = {
type: 'middle-load', type: 'middle-load',
since_id: res.data[0].id, since_id: res.data[0].id,
max_id: maxID, max_id: maxID,
id: 'loading-card' id: 'loading-card',
uri: 'loading-card'
} }
let notifications: Array<Entity.Notification | LoadingCard> = [card] let notifications: Array<Entity.Notification | LoadingCard> = [card]
notifications = notifications.concat(res.data) notifications = notifications.concat(res.data)

View File

@ -3,4 +3,5 @@ export type LoadingCard = {
max_id: string | null max_id: string | null
since_id: string | null since_id: string | null
id: 'loading-card' id: 'loading-card'
uri: 'loading-card'
} }