Refactoring load timeline

This commit is contained in:
kPherox 2019-04-24 19:31:06 +09:00
parent 5e4fa1726d
commit 430a64f592
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D
3 changed files with 35 additions and 22 deletions

View File

@ -31,14 +31,15 @@ import { ipcRenderer } from 'electron'
import { Component, Vue } from 'vue-property-decorator' import { Component, Vue } from 'vue-property-decorator'
import { Status } from 'megalodon' import { Status } from 'megalodon'
type DeleteListener = (e: Event, id: number) => void
type Instance = string type Instance = string
type Timeline = { type Timeline = {
name: string name: string
statuses: Map<number, Status> statuses?: Map<number, Status>
} }
type UpdateListener = (e: Event, status: Status) => void
type DeleteListener = (e: Event, id: number) => void
type Timelines = Timeline[] type Timelines = Timeline[]
type UpdateListener = (e: Event, status: Status) => void
@Component @Component
export default class AddColumn extends Vue { export default class AddColumn extends Vue {
public instance: Instance = '' public instance: Instance = ''
@ -61,22 +62,41 @@ export default class AddColumn extends Vue {
} }
public addTL() { public addTL() {
let timeline: Timeline = { name: this.instance, statuses: new Map() } let timeline: Timeline = { name: this.instance }
this.showInput = false this.showInput = false
this.instance = '' this.instance = ''
this.pubTL.push(timeline) this.pubTL.push(timeline)
// TL
this.loadTL(timeline) this.loadTL(timeline)
// streaming
this.subscribeStreaming(timeline)
}
public loadTL(timeline: Timeline) {
let statuses: Status[] = ipcRenderer.sendSync('no-auth-timeline', timeline.name)
timeline.statuses = new Map(statuses.map((status): [number, Status] => [status.id, status]))
}
public async subscribeStreaming(timeline: Timeline) {
// update
let updateListener = (_: Event, status: Status) => { let updateListener = (_: Event, status: Status) => {
if (timeline.statuses === undefined) {
timeline.statuses = new Map()
}
timeline.statuses.set(status.id, status) timeline.statuses.set(status.id, status)
this.$forceUpdate() this.$forceUpdate()
} }
ipcRenderer.on(`update-${timeline.name}-no-auth`, updateListener) ipcRenderer.on(`update-${timeline.name}-no-auth`, updateListener)
this.updateListeners.push([`update-${timeline.name}-no-auth`, updateListener]) this.updateListeners.push([`update-${timeline.name}-no-auth`, updateListener])
// delete
let deleteListener = (_: Event, id: number) => { let deleteListener = (_: Event, id: number) => {
if (timeline.statuses === undefined) {
timeline.statuses = new Map()
}
timeline.statuses.delete(id) timeline.statuses.delete(id)
this.$forceUpdate() this.$forceUpdate()
} }
@ -85,13 +105,6 @@ export default class AddColumn extends Vue {
ipcRenderer.send('open-streaming', timeline.name, 'no-auth') ipcRenderer.send('open-streaming', timeline.name, 'no-auth')
} }
public loadTL(timeline: Timeline) {
ipcRenderer.once(`timeline-${timeline.name}-no-auth`, (_: Event, statuses: Status[]) => {
timeline.statuses = new Map(statuses.map((status): [number, Status] => [status.id, status]))
})
ipcRenderer.send('no-auth-timeline', timeline.name)
}
} }
</script>= </script>=

View File

@ -1,5 +1,6 @@
import { import {
ipcMain ipcMain,
Event
} from 'electron' } from 'electron'
import { Status, Response } from 'megalodon' import { Status, Response } from 'megalodon'
@ -11,10 +12,7 @@ export default class Timeline {
ipcMain.on('no-auth-timeline', async (event: Event, name: string) => { ipcMain.on('no-auth-timeline', async (event: Event, name: string) => {
const client = Client.getNoAuthClient(name) const client = Client.getNoAuthClient(name)
let res: Response<[Status]> = await client.get<[Status]>('/timelines/public?local=true') let res: Response<[Status]> = await client.get<[Status]>('/timelines/public?local=true')
event.returnValue = res.data
if (Window.windowMap.has('main')) {
Window.windowMap.get('main')!.webContents.send(`timeline-${name}-no-auth`, res.data)
}
}) })
} }
} }

View File

@ -1,8 +1,10 @@
import { BrowserWindow, Accelerator } from 'electron' import { BrowserWindow, Accelerator } from 'electron'
export function disableAll(win: BrowserWindow): void declare module 'electron-localshortcut' {
export function enableAll(win: BrowserWindow): void function disableAll(win: BrowserWindow): void
export function isRegistered(win: BrowserWindow, accelerator: Accelerator): void function enableAll(win: BrowserWindow): void
export function register(win: BrowserWindow, accelerator: Accelerator, callback: () => void): void function isRegistered(win: BrowserWindow, accelerator: Accelerator): void
export function unregister(win: BrowserWindow, accelerator: Accelerator): void function register(win: BrowserWindow, accelerator: Accelerator, callback: () => void): void
export function unregisterAll(win: BrowserWindow): void function unregister(win: BrowserWindow, accelerator: Accelerator): void
function unregisterAll(win: BrowserWindow): void
}