refs #850 Replace List and Tag in Hashtag with typescript

This commit is contained in:
AkiraFukushima 2019-04-14 21:13:07 +09:00
parent f71edcc889
commit b2241ab2ad
8 changed files with 236 additions and 187 deletions

View File

@ -31,8 +31,8 @@ export default {
this.$store.dispatch('TimelineSpace/Contents/Hashtag/List/listTags')
},
methods: {
openTimeline (tag) {
this.$router.push({ path: `/${this.$route.params.id}/hashtag/${tag}` })
openTimeline (tagName) {
this.$router.push({ path: `/${this.$route.params.id}/hashtag/${tagName}` })
},
deleteTag (tag) {
this.$store.dispatch('TimelineSpace/Contents/Hashtag/List/removeTag', tag)

View File

@ -157,7 +157,7 @@ export default {
if (((event.target.clientHeight + event.target.scrollTop) >= document.getElementsByName('tag')[0].clientHeight - 10) && !this.lazyloading) {
this.$store.dispatch('TimelineSpace/Contents/Hashtag/Tag/lazyFetchTimeline', {
tag: this.tag,
last: this.timeline[this.timeline.length - 1]
status: this.timeline[this.timeline.length - 1]
})
}
// for unread control

View File

@ -1,12 +1,14 @@
import { ipcRenderer } from 'electron'
import List from './Hashtag/List'
import Tag from './Hashtag/Tag'
import List, { ListState } from './Hashtag/List'
import Tag, { TagState } from './Hashtag/Tag'
import { Module, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface HashtagState {}
export interface HashtagModuleState extends HashtagState {
List: ListState,
Tag: TagState
}
const state = (): HashtagState => ({})

View File

@ -1,46 +0,0 @@
import { ipcRenderer } from 'electron'
const List = {
namespaced: true,
state: {
tags: []
},
mutations: {
updateTags (state, tags) {
state.tags = tags
}
},
actions: {
listTags ({ commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.once('response-list-hashtags', (event, tags) => {
ipcRenderer.removeAllListeners('error-list-hashtags')
commit('updateTags', tags)
resolve(tags)
})
ipcRenderer.once('error-list-hashtags', (event, err) => {
ipcRenderer.removeAlListeners('response-list-hashtags')
reject(err)
})
ipcRenderer.send('list-hashtags')
})
},
removeTag ({ dispatch }, tag) {
return new Promise((resolve, reject) => {
ipcRenderer.once('response-remove-hashtag', () => {
ipcRenderer.removeAllListeners('error-remove-hashtag')
dispatch('listTags')
dispatch('TimelineSpace/SideMenu/listTags', {}, { root: true })
resolve('deleted')
})
ipcRenderer.once('error-remove-hashtag', (event, err) => {
ipcRenderer.removeAllListeners('response-remove-hashtag')
reject(err)
})
ipcRenderer.send('remove-hashtag', tag)
})
}
}
}
export default List

View File

@ -0,0 +1,63 @@
import { ipcRenderer } from 'electron'
import Hashtag from '~/src/types/hashtag'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface ListState {
tags: Array<Hashtag>
}
const state = (): ListState => ({
tags: []
})
export const MUTATION_TYPES = {
UPDATE_TAGS: 'updateTags'
}
const mutations: MutationTree<ListState> = {
[MUTATION_TYPES.UPDATE_TAGS]: (state, tags: Array<Hashtag>) => {
state.tags = tags
}
}
const actions: ActionTree<ListState, RootState> = {
listTags: ({ commit }) => {
return new Promise((resolve, reject) => {
ipcRenderer.once('response-list-hashtags', (_, tags: Array<Hashtag>) => {
ipcRenderer.removeAllListeners('error-list-hashtags')
commit(MUTATION_TYPES.UPDATE_TAGS, tags)
resolve(tags)
})
ipcRenderer.once('error-list-hashtags', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-list-hashtags')
reject(err)
})
ipcRenderer.send('list-hashtags')
})
},
removeTag: ({ dispatch }, tag: Hashtag) => {
return new Promise((resolve, reject) => {
ipcRenderer.once('response-remove-hashtag', () => {
ipcRenderer.removeAllListeners('error-remove-hashtag')
dispatch('listTags')
dispatch('TimelineSpace/SideMenu/listTags', {}, { root: true })
resolve('deleted')
})
ipcRenderer.once('error-remove-hashtag', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-remove-hashtag')
reject(err)
})
ipcRenderer.send('remove-hashtag', tag)
})
}
}
const List: Module<ListState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default List

View File

@ -1,136 +0,0 @@
import { ipcRenderer } from 'electron'
import Mastodon from 'megalodon'
const Tag = {
namespaced: true,
state: {
timeline: [],
unreadTimeline: [],
lazyLoading: false,
heading: true,
filter: ''
},
mutations: {
changeHeading (state, value) {
state.heading = value
},
appendTimeline (state, update) {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
updateTimeline (state, timeline) {
state.timeline = timeline
},
mergeTimeline (state) {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
insertTimeline (state, messages) {
state.timeline = state.timeline.concat(messages)
},
archiveTimeline (state) {
state.timeline = state.timeline.slice(0, 40)
},
clearTimeline (state) {
state.timeline = []
state.unreadTimeline = []
},
updateToot (state, message) {
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.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
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
deleteToot (state, message) {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
changeLazyLoading (state, value) {
state.lazyLoading = value
},
changeFilter (state, filter) {
state.filter = filter
}
},
actions: {
fetch ({ commit, rootState }, tag) {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get(`/timelines/tag/${encodeURIComponent(tag)}`, { limit: 40 })
.then(res => {
commit('updateTimeline', res.data)
})
},
startStreaming ({ state, commit, rootState }, tag) {
ipcRenderer.on('update-start-tag-streaming', (event, update) => {
commit('appendTimeline', update)
if (state.heading && Math.random() > 0.8) {
commit('archiveTimeline')
}
})
return new Promise((resolve, reject) => {
ipcRenderer.send('start-tag-streaming', {
tag: encodeURIComponent(tag),
account: rootState.TimelineSpace.account,
useWebsocket: rootState.TimelineSpace.useWebsocket
})
ipcRenderer.once('error-start-tag-streaming', (event, err) => {
reject(err)
})
})
},
stopStreaming () {
return new Promise(resolve => {
ipcRenderer.removeAllListeners('error-start-tag-streaming')
ipcRenderer.removeAllListeners('update-start-tag-streaming')
ipcRenderer.send('stop-tag-streaming')
resolve()
})
},
lazyFetchTimeline ({ state, commit, rootState }, obj) {
if (state.lazyLoading) {
return Promise.resolve(null)
}
if (obj.last === undefined || obj.last === null) {
return Promise.resolve(null)
}
commit('changeLazyLoading', true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get(`/timelines/tag/${obj.tag}`, { max_id: obj.last.id, limit: 40 })
.then(res => {
commit('insertTimeline', res.data)
commit('changeLazyLoading', false)
return res.data
})
.catch(err => {
commit('changeLazyLoading', false)
throw err
})
}
}
}
export default Tag

View File

@ -0,0 +1,162 @@
import { ipcRenderer } from 'electron'
import Mastodon, { Status, Response } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
import { LoadPositionWithTag } from '~src/types/load_position'
export interface TagState {
timeline: Array<Status>,
unreadTimeline: Array<Status>,
lazyLoading: boolean,
heading: boolean,
filter: string
}
const state = (): TagState => ({
timeline: [],
unreadTimeline: [],
lazyLoading: false,
heading: true,
filter: ''
})
export const MUTATION_TYPES = {
CHANGE_HEADING: 'changeHeading',
APPEND_TIMELINE: 'appendTimeline',
UPDATE_TIMELINE: 'updateTimeline',
MERGE_TIMELINE: 'mergeTimeline',
INSERT_TIMELINE: 'insertTimeline',
ARCHIVE_TIMELINE: 'archiveTimeline',
CLEAR_TIMELINE: 'clearTimeline',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',
CHANGE_LAZY_LOADING: 'changeLazyLoading',
CHANGE_FILTER: 'changeFilter'
}
const mutations: MutationTree<TagState> = {
[MUTATION_TYPES.CHANGE_HEADING]: (state, value: boolean) => {
state.heading = value
},
[MUTATION_TYPES.APPEND_TIMELINE]: (state, update: Status) => {
if (state.heading) {
state.timeline = [update].concat(state.timeline)
} else {
state.unreadTimeline = [update].concat(state.unreadTimeline)
}
},
[MUTATION_TYPES.UPDATE_TIMELINE]: (state, timeline: Array<Status>) => {
state.timeline = timeline
},
[MUTATION_TYPES.MERGE_TIMELINE]: (state) => {
state.timeline = state.unreadTimeline.slice(0, 80).concat(state.timeline)
state.unreadTimeline = []
},
[MUTATION_TYPES.INSERT_TIMELINE]: (state, messages: Array<Status>) => {
state.timeline = state.timeline.concat(messages)
},
[MUTATION_TYPES.ARCHIVE_TIMELINE]: (state) => {
state.timeline = state.timeline.slice(0, 40)
},
[MUTATION_TYPES.CLEAR_TIMELINE]: (state) => {
state.timeline = []
state.unreadTimeline = []
},
[MUTATION_TYPES.UPDATE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.map((toot) => {
if (toot.id === message.id) {
return message
} else if (toot.reblog !== null && toot.reblog.id === message.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
}
return Object.assign(toot, reblog)
} else {
return toot
}
})
},
[MUTATION_TYPES.DELETE_TOOT]: (state, message: Status) => {
state.timeline = state.timeline.filter((toot) => {
if (toot.reblog !== null && toot.reblog.id === message.id) {
return false
} else {
return toot.id !== message.id
}
})
},
[MUTATION_TYPES.CHANGE_LAZY_LOADING]: (state, value: boolean) => {
state.lazyLoading = value
},
[MUTATION_TYPES.CHANGE_FILTER]: (state, filter: string) => {
state.filter = filter
}
}
const actions: ActionTree<TagState, RootState> = {
fetch: async ({ commit, rootState }, tag: string): Promise<Array<Status>> => {
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
const res: Response<Array<Status>> = await client.get<Array<Status>>(`/timelines/tag/${encodeURIComponent(tag)}`, { limit: 40 })
commit(MUTATION_TYPES.UPDATE_TIMELINE, res.data)
return res.data
},
startStreaming: ({ state, commit, rootState }, tag: string) => {
ipcRenderer.on('update-start-tag-streaming', (_, update: Status) => {
commit(MUTATION_TYPES.APPEND_TIMELINE, update)
if (state.heading && Math.random() > 0.8) {
commit(MUTATION_TYPES.ARCHIVE_TIMELINE)
}
})
// @ts-ignore
return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
ipcRenderer.send('start-tag-streaming', {
tag: encodeURIComponent(tag),
account: rootState.TimelineSpace.account,
useWebsocket: rootState.TimelineSpace.useWebsocket
})
ipcRenderer.once('error-start-tag-streaming', (_, err: Error) => {
reject(err)
})
})
},
stopStreaming: () => {
return new Promise(resolve => {
ipcRenderer.removeAllListeners('error-start-tag-streaming')
ipcRenderer.removeAllListeners('update-start-tag-streaming')
ipcRenderer.send('stop-tag-streaming')
resolve()
})
},
lazyFetchTimeline: async ({ state, commit, rootState }, loadPosition: LoadPositionWithTag) => {
if (state.lazyLoading) {
return Promise.resolve(null)
}
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, true)
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v1'
)
return client.get<Array<Status>>(`/timelines/tag/${loadPosition.tag}`, { max_id: loadPosition.status.id, limit: 40 })
.then(res => {
commit(MUTATION_TYPES.INSERT_TIMELINE, res.data)
return res.data
})
.finally(() => {
commit(MUTATION_TYPES.CHANGE_LAZY_LOADING, false)
})
}
}
const Tag: Module<TagState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Tag

View File

@ -11,3 +11,7 @@ export interface LoadPositionWithAccount extends LoadPosition {
export interface LoadPositionWithList extends LoadPosition {
list_id: number
}
export interface LoadPositionWithTag extends LoadPosition {
tag: string
}