[refactor] Local store

This commit is contained in:
AkiraFukushima 2023-01-02 21:26:08 +09:00
parent e72a7e7efe
commit 3b667ccaee
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
6 changed files with 102 additions and 382 deletions

View File

@ -1,234 +0,0 @@
import { Entity } from 'megalodon'
import Local, { LocalState, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Local'
const account: Entity.Account = {
id: '1',
username: 'h3poteto',
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
statuses_count: 100,
note: 'engineer',
url: 'https://pleroma.io',
avatar: '',
avatar_static: '',
header: '',
header_static: '',
emojis: [],
moved: null,
fields: null,
bot: false
}
const status1: Entity.Status = {
id: '1',
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'hoge',
plain_content: 'hoge',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: {
name: 'Web'
} as Entity.Application,
language: null,
pinned: null,
emoji_reactions: [],
bookmarked: false,
quote: false
}
const status2: Entity.Status = {
id: '2',
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'fuga',
plain_content: 'fuga',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: {
name: 'Web'
} as Entity.Application,
language: null,
pinned: null,
emoji_reactions: [],
bookmarked: false,
quote: false
}
const rebloggedStatus: Entity.Status = {
id: '3',
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: status1,
content: 'hoge',
plain_content: 'hoge',
created_at: '2019-03-31T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: {
name: 'Web'
} as Entity.Application,
language: null,
pinned: null,
emoji_reactions: [],
bookmarked: false,
quote: false
}
describe('TimelineSpace/Contents/Local', () => {
describe('mutations', () => {
let state: LocalState
describe('deleteToot', () => {
describe('message is not reblogged', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
timeline: [status2, status1],
unreads: []
}
})
it('should be deleted', () => {
Local.mutations![MUTATION_TYPES.DELETE_TOOT](state, status1.id)
expect(state.timeline).toEqual([status2])
})
})
describe('message is reblogged', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
timeline: [status2, rebloggedStatus],
unreads: []
}
})
it('should be deleted', () => {
Local.mutations![MUTATION_TYPES.DELETE_TOOT](state, status1.id)
expect(state.timeline).toEqual([status2])
})
})
})
describe('appendTimeline', () => {
describe('heading', () => {
describe('normal', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
timeline: [status2, status1],
unreads: []
}
})
it('should be updated timeline', () => {
Local.mutations![MUTATION_TYPES.APPEND_TIMELINE](state, rebloggedStatus)
expect(state.timeline).toEqual([rebloggedStatus, status2, status1])
})
})
describe('duplicated status', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: true,
timeline: [rebloggedStatus, status2, status1],
unreads: []
}
})
it('should not be updated timeline', () => {
Local.mutations![MUTATION_TYPES.APPEND_TIMELINE](state, rebloggedStatus)
expect(state.timeline).toEqual([rebloggedStatus, status2, status1])
})
})
})
describe('not heading', () => {
describe('normal', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: false,
timeline: [status2, status1],
unreads: []
}
})
it('should be updated timeline', () => {
Local.mutations![MUTATION_TYPES.APPEND_TIMELINE](state, rebloggedStatus)
expect(state.timeline).toEqual([status2, status1])
expect(state.unreads).toEqual([rebloggedStatus])
})
})
describe('duplicated status', () => {
beforeEach(() => {
state = {
lazyLoading: false,
heading: false,
timeline: [rebloggedStatus, status2, status1],
unreads: []
}
})
it('should not be updated timeline', () => {
Local.mutations![MUTATION_TYPES.APPEND_TIMELINE](state, rebloggedStatus)
expect(state.timeline).toEqual([rebloggedStatus, status2, status1])
})
})
})
})
})
})

View File

@ -200,10 +200,14 @@ export default defineComponent({
}
}
const updateToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
if (account.account) {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, { status: message, accountId: account.account.id })
}
}
const deleteToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, message.id)
if (account.account) {
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: message.id, accountId: account.account.id })
}
}
const fetchTimelineSince = (since_id: string) => {
loadingMore.value = true

View File

@ -1,14 +1,16 @@
<template>
<div id="local">
<div class="unread">{{ unreads.length > 0 ? unreads.length : '' }}</div>
<DynamicScroller :items="timeline" :min-item-size="86" id="scroller" class="scroller" ref="scroller">
<template v-slot="{ item, index, active }">
<DynamicScrollerItem :item="item" :active="active" :size-dependencies="[item.uri]" :data-index="index" :watchData="true">
<toot
v-if="account.account && account.server"
:message="item"
:focused="item.uri + item.id === focusedId"
:overlaid="modalOpened"
:filters="[]"
:account="account.account"
:server="account.server"
v-on:update="updateToot"
v-on:delete="deleteToot"
@focusRight="focusSidebar"
@ -27,7 +29,7 @@
</template>
<script lang="ts">
import { computed, defineComponent, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, ref, watch } from 'vue'
import { computed, defineComponent, onBeforeUnmount, onBeforeUpdate, onMounted, onUnmounted, ref, watch, reactive } from 'vue'
import { logicAnd } from '@vueuse/math'
import { useMagicKeys, whenever } from '@vueuse/core'
import { ElMessage } from 'element-plus'
@ -40,9 +42,11 @@ import { EventEmitter } from '@/components/event'
import useReloadable from '@/components/utils/reloadable'
import { ACTION_TYPES, MUTATION_TYPES } from '@/store/TimelineSpace/Contents/Local'
import { MUTATION_TYPES as SIDE_MENU_MUTATION } from '@/store/TimelineSpace/SideMenu'
import { MUTATION_TYPES as TIMELINE_MUTATION, ACTION_TYPES as TIMELINE_ACTION } from '@/store/TimelineSpace'
import { MUTATION_TYPES as TIMELINE_MUTATION } from '@/store/TimelineSpace'
import { MUTATION_TYPES as HEADER_MUTATION } from '@/store/TimelineSpace/HeaderMenu'
import { MUTATION_TYPES as CONTENTS_MUTATION } from '@/store/TimelineSpace/Contents'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
import { MyWindow } from '~/src/types/global'
export default defineComponent({
name: 'local',
@ -55,29 +59,33 @@ export default defineComponent({
const { reloadable } = useReloadable(store, route, i18n)
const { j, k, Ctrl_r } = useMagicKeys()
const win = (window as any) as MyWindow
const id = computed(() => parseInt(route.params.id as string))
const focusedId = ref<string | null>(null)
const scroller = ref<any>(null)
const lazyLoading = ref(false)
const heading = ref(true)
const account = reactive<{ account: LocalAccount | null; server: LocalServer | null }>({
account: null,
server: null
})
const timeline = computed(() => store.state.TimelineSpace.Contents.Local.timeline[id.value])
const timeline = computed(() => store.state.TimelineSpace.Contents.Local.timeline)
const unreads = computed(() => store.state.TimelineSpace.Contents.Local.unreads)
const lazyLoading = computed(() => store.state.TimelineSpace.Contents.Local.lazyLoading)
const heading = computed(() => store.state.TimelineSpace.Contents.Local.heading)
const openSideBar = computed(() => store.state.TimelineSpace.Contents.SideBar.openSideBar)
const startReload = computed(() => store.state.TimelineSpace.HeaderMenu.reload)
const unreadNotification = computed(() => store.state.TimelineSpace.timelineSetting.unreadNotification)
const modalOpened = computed<boolean>(() => store.getters[`TimelineSpace/Modals/modalOpened`])
const currentFocusedIndex = computed(() => timeline.value.findIndex(toot => focusedId.value === toot.uri + toot.id))
const shortcutEnabled = computed(() => !modalOpened.value)
onMounted(async () => {
const [a, s]: [LocalAccount, LocalServer] = await win.ipcRenderer.invoke('get-local-account', id.value)
account.account = a
account.server = s
store.commit(`TimelineSpace/SideMenu/${SIDE_MENU_MUTATION.CHANGE_UNREAD_LOCAL_TIMELINE}`, false)
document.getElementById('scroller')?.addEventListener('scroll', onScroll)
if (!unreadNotification.value.local) {
store.commit(`TimelineSpace/Contents/${CONTENTS_MUTATION.CHANGE_LOADING}`, true)
await initialize().finally(() => {
store.commit(`TimelineSpace/Contents/${CONTENTS_MUTATION.CHANGE_LOADING}`, false)
})
}
})
onBeforeUpdate(() => {
if (store.state.TimelineSpace.SideMenu.unreadLocalTimeline && heading.value) {
@ -85,18 +93,9 @@ export default defineComponent({
}
})
onBeforeUnmount(() => {
if (!unreadNotification.value.local) {
store.dispatch(`TimelineSpace/${TIMELINE_ACTION.STOP_LOCAL_STREAMING}`)
store.dispatch(`TimelineSpace/${TIMELINE_ACTION.UNBIND_LOCAL_STREAMING}`)
}
EventEmitter.off('focus-timeline')
})
onUnmounted(() => {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
store.commit(`${space}/${MUTATION_TYPES.ARCHIVE_TIMELINE}`)
if (!unreadNotification.value.local) {
store.commit(`${space}/${MUTATION_TYPES.CLEAR_TIMELINE}`)
}
const el = document.getElementById('scroller')
if (el) {
el.removeEventListener('scroll', onScroll)
@ -113,9 +112,9 @@ export default defineComponent({
watch(focusedId, (newVal, _oldVal) => {
if (newVal && heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, false)
heading.value = false
} else if (newVal === null && !heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
heading.value = true
}
})
whenever(logicAnd(j, shortcutEnabled), () => {
@ -132,34 +131,29 @@ export default defineComponent({
reload()
})
const initialize = async () => {
await store.dispatch(`${space}/${ACTION_TYPES.FETCH_LOCAL_TIMELINE}`).catch(_ => {
ElMessage({
message: i18n.t('message.timeline_fetch_error'),
type: 'error'
})
})
await store.dispatch(`TimelineSpace/${TIMELINE_ACTION.BIND_LOCAL_STREAMING}`)
store.dispatch(`TimelineSpace/${TIMELINE_ACTION.START_LOCAL_STREAMING}`)
}
const onScroll = (event: Event) => {
if (
(event.target as HTMLElement)!.clientHeight + (event.target as HTMLElement)!.scrollTop >=
document.getElementById('scroller')!.scrollHeight - 10 &&
!lazyLoading.value
) {
store.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, timeline.value[timeline.value.length - 1]).catch(() => {
ElMessage({
message: i18n.t('message.timeline_fetch_error'),
type: 'error'
store
.dispatch(`${space}/${ACTION_TYPES.LAZY_FETCH_TIMELINE}`, {
lastStatus: timeline.value[timeline.value.length - 1],
account: account.account,
server: account.server
})
.catch(() => {
ElMessage({
message: i18n.t('message.timeline_fetch_error'),
type: 'error'
})
})
})
}
if ((event.target as HTMLElement)!.scrollTop > 10 && heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, false)
heading.value = false
} else if ((event.target as HTMLElement)!.scrollTop <= 10 && !heading.value) {
store.commit(`${space}/${MUTATION_TYPES.CHANGE_HEADING}`, true)
store.commit(`${space}/${MUTATION_TYPES.MERGE_UNREADS}`)
heading.value = true
}
}
const reload = async () => {
@ -171,10 +165,14 @@ export default defineComponent({
}
}
const updateToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, message)
if (account.account) {
store.commit(`${space}/${MUTATION_TYPES.UPDATE_TOOT}`, { status: message, accountId: account.account.id })
}
}
const deleteToot = (message: Entity.Status) => {
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, message.id)
if (account.account) {
store.commit(`${space}/${MUTATION_TYPES.DELETE_TOOT}`, { statusId: message.id, accountId: account.account.id })
}
}
const upper = () => {
scroller.value.scrollToItem(0)
@ -213,7 +211,7 @@ export default defineComponent({
openSideBar,
heading,
upper,
unreads
account
}
}
})

View File

@ -121,6 +121,7 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
req.forEach(async ([account, server]) => {
await dispatch('TimelineSpace/Contents/Home/fetchTimeline', { account, server }, { root: true })
await dispatch('TimelineSpace/Contents/Notifications/fetchNotifications', { account, server }, { root: true })
await dispatch('TimelineSpace/Contents/Local/fetchLocalTimeline', { account, server }, { root: true })
})
},
[ACTION_TYPES.BIND_STREAMINGS]: async ({ commit }, req: Array<[LocalAccount, LocalServer]>) => {
@ -138,6 +139,14 @@ const actions: ActionTree<GlobalHeaderState, RootState> = {
commit('TimelineSpace/Contents/Home/deleteToot', { statusId: id, accountId: account.id }, { root: true })
commit('TimelineSpace/Contents/Notifications/deleteToot', { statusId: id, accountId: account.id }, { root: true })
})
win.ipcRenderer.removeAllListeners(`update-local-streamings-${account.id}`)
win.ipcRenderer.on(`update-local-streamings-${account.id}`, (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Local/appendTimeline', { status: update, accountId: account.id }, { root: true })
})
win.ipcRenderer.removeAllListeners(`delete-local-streamings-${account.id}`)
win.ipcRenderer.on(`delete-local-streamings-${account.id}`, (_, id: string) => {
commit('TimelineSpace/Contents/Local/deleteToot', { statusId: id, accountId: account.id }, { root: true })
})
})
}
}

View File

@ -87,7 +87,6 @@ export const ACTION_TYPES = {
FETCH_CONTENTS_TIMELINES: 'fetchContentsTimelines',
CLEAR_CONTENTS_TIMELINES: 'clearContentsTimelines',
BIND_STREAMINGS: 'bindStreamings',
BIND_LOCAL_STREAMING: 'bindLocalStreaming',
BIND_PUBLIC_STREAMING: 'bindPublicStreaming',
BIND_DIRECT_MESSAGES_STREAMING: 'bindDirectMessagesStreaming',
UPDATE_TOOT_FOR_ALL_TIMELINES: 'updateTootForAllTimelines',
@ -199,34 +198,19 @@ const actions: ActionTree<TimelineSpaceState, RootState> = {
},
[ACTION_TYPES.FETCH_CONTENTS_TIMELINES]: async ({ dispatch }) => {
await dispatch('TimelineSpace/Contents/DirectMessages/fetchTimeline', {}, { root: true })
await dispatch('TimelineSpace/Contents/Local/fetchLocalTimeline', {}, { root: true })
await dispatch('TimelineSpace/Contents/Public/fetchPublicTimeline', {}, { root: true })
},
[ACTION_TYPES.CLEAR_CONTENTS_TIMELINES]: ({ commit }) => {
commit('TimelineSpace/Contents/Local/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/DirectMessages/clearTimeline', {}, { root: true })
commit('TimelineSpace/Contents/Public/clearTimeline', {}, { root: true })
},
[ACTION_TYPES.BIND_STREAMINGS]: ({ dispatch }) => {
dispatch('bindDirectMessagesStreaming')
dispatch('bindLocalStreaming')
dispatch('bindPublicStreaming')
},
// ------------------------------------------------
// Each streaming methods
// ------------------------------------------------
[ACTION_TYPES.BIND_LOCAL_STREAMING]: ({ commit, rootState, state }) => {
win.ipcRenderer.on(`update-local-streamings-${state.account!.id}`, (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Local/appendTimeline', update, { root: true })
if (rootState.TimelineSpace.Contents.Local.heading && Math.random() > 0.8) {
commit('TimelineSpace/Contents/Local/archiveTimeline', {}, { root: true })
}
commit('TimelineSpace/SideMenu/changeUnreadLocalTimeline', true, { root: true })
})
win.ipcRenderer.on(`delete-local-streamings-${state.account!.id}`, (_, id: string) => {
commit('TimelineSpace/Contents/Local/deleteToot', id, { root: true })
})
},
[ACTION_TYPES.BIND_PUBLIC_STREAMING]: ({ commit, rootState, state }) => {
win.ipcRenderer.on(`update-public-streamings-${state.account!.id}`, (_, update: Entity.Status) => {
commit('TimelineSpace/Contents/Public/appendTimeline', update, { root: true })

View File

@ -1,70 +1,52 @@
import generator, { Entity } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
import { LocalAccount } from '~src/types/localAccount'
import { LocalServer } from '~src/types/localServer'
export type LocalState = {
timeline: Array<Entity.Status>
lazyLoading: boolean
heading: boolean
unreads: Array<Entity.Status>
timeline: { [key: number]: Array<Entity.Status> }
}
const state = (): LocalState => ({
timeline: [],
lazyLoading: false,
heading: true,
unreads: []
timeline: {}
})
export const MUTATION_TYPES = {
CHANGE_HEADING: 'changeHeading',
APPEND_TIMELINE: 'appendTimeline',
UPDATE_TIMELINE: 'updateTimeline',
REPLACE_TIMELINE: 'replaceTimeline',
INSERT_TIMELINE: 'insertTimeline',
ARCHIVE_TIMELINE: 'archiveTimeline',
CLEAR_TIMELINE: 'clearTimeline',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',
CHANGE_LAZY_LOADING: 'changeLazyLoading',
MERGE_UNREADS: 'mergeUnreads'
DELETE_TOOT: 'deleteToot'
}
const mutations: MutationTree<LocalState> = {
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Entity.Status) => {
// Reject duplicated status in timeline
if (!state.timeline.find(item => item.id === update.id) && !state.unreads.find(item => item.id === update.id)) {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreads = [update].concat(state.unreads)
}
[MUTATION_TYPES.APPEND_TIMELINE]: (state, obj: { status: Entity.Status; accountId: number }) => {
if (state.timeline[obj.accountId]) {
state.timeline[obj.accountId] = [obj.status, ...state.timeline[obj.accountId]]
} else {
state.timeline[obj.accountId] = [obj.status]
}
},
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, messages: Array<Entity.Status>) => {
state.timeline = messages
[MUTATION_TYPES.REPLACE_TIMELINE]: (state, obj: { statuses: Array<Entity.Status>; accountId: number }) => {
state.timeline[obj.accountId] = obj.statuses
},
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Entity.Status>) => {
state.timeline = state.timeline.concat(messages)
[MUTATION_TYPES.INSERT_TIMELINE]: (state, obj: { statuses: Array<Entity.Status>; accountId: number }) => {
if (state.timeline[obj.accountId]) {
state.timeline[obj.accountId] = [...state.timeline[obj.accountId], ...obj.statuses]
} else {
state.timeline[obj.accountId] = obj.statuses
}
},
[MUTATION_TYPES.ARCHIVE_TIMELINE]: state => {
state.timeline = state.timeline.slice(0, 40)
},
[MUTATION_TYPES.CLEAR_TIMELINE]: state => {
state.timeline = []
state.unreads = []
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Entity.Status) => {
state.timeline = state.timeline.map(toot => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.id) {
[MUTATION_TYPES.UPDATE_TOOT]: (state, obj: { status: Entity.Status; accountId: number }) => {
state.timeline[obj.accountId] = state.timeline[obj.accountId].map(toot => {
if (toot.id === obj.status.id) {
return obj.status
} else if (toot.reblog !== null && toot.reblog.id === obj.status.id) {
// When user reblog/favourite a reblogged toot, target message is a original toot.
// So, a message which is received now is original toot.
const reblog = {
reblog: message
reblog: obj.status
}
return Object.assign(toot, reblog)
} else {
@ -72,21 +54,14 @@ const mutations: MutationTree<LocalState> = {
}
})
},
[MUTATION_TYPES.DELETE_TOOT]: (state, id: string) => {
state.timeline = state.timeline.filter(toot => {
if (toot.reblog !== null && toot.reblog.id === id) {
[MUTATION_TYPES.DELETE_TOOT]: (state, obj: { statusId: string; accountId: number }) => {
state.timeline[obj.accountId] = state.timeline[obj.accountId].filter(toot => {
if (toot.reblog !== null && toot.reblog.id === obj.statusId) {
return false
} else {
return toot.id !== id
return toot.id !== obj.statusId
}
})
},
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.MERGE_UNREADS]: state => {
state.timeline = state.unreads.slice(0, 80).concat(state.timeline)
state.unreads = []
}
}
@ -96,45 +71,29 @@ export const ACTION_TYPES = {
}
const actions: ActionTree<LocalState, RootState> = {
[ACTION_TYPES.FETCH_LOCAL_TIMELINE]: async ({ commit, rootState }): Promise<Array<Entity.Status>> => {
const client = generator(
rootState.TimelineSpace.server!.sns,
rootState.TimelineSpace.server!.baseURL,
rootState.TimelineSpace.account!.accessToken,
rootState.App.userAgent
)
[ACTION_TYPES.FETCH_LOCAL_TIMELINE]: async (
{ commit, rootState },
req: { account: LocalAccount; server: LocalServer }
): Promise<Array<Entity.Status>> => {
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
try {
const res = await client.getLocalTimeline({ limit: 20 })
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
commit(MUTATION_TYPES.REPLACE_TIMELINE, { statuses: res.data, accountId: req.account.id })
return res.data
} catch (err) {
return []
}
},
[ACTION_TYPES.LAZY_FETCH_TIMELINE]: async (
{ state, commit, rootState },
lastStatus: Entity.Status
{ commit, rootState },
req: { lastStatus: Entity.Status; account: LocalAccount; server: LocalServer }
): Promise<Array<Entity.Status> | null> => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = generator(
rootState.TimelineSpace.server!.sns,
rootState.TimelineSpace.server!.baseURL,
rootState.TimelineSpace.account!.accessToken,
rootState.App.userAgent
)
return client
.getLocalTimeline({ max_id: lastStatus.id, limit: 20 })
.then(res => {
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
return client.getLocalTimeline({ max_id: req.lastStatus.id, limit: 20 }).then(res => {
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
return res.data
})
}
}