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 { Status } from 'megalodon'
type DeleteListener = (e: Event, id: number) => void
type Instance = string
type Timeline = {
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 UpdateListener = (e: Event, status: Status) => void
@Component
export default class AddColumn extends Vue {
public instance: Instance = ''
@ -61,22 +62,41 @@ export default class AddColumn extends Vue {
}
public addTL() {
let timeline: Timeline = { name: this.instance, statuses: new Map() }
let timeline: Timeline = { name: this.instance }
this.showInput = false
this.instance = ''
this.pubTL.push(timeline)
// TL
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) => {
if (timeline.statuses === undefined) {
timeline.statuses = new Map()
}
timeline.statuses.set(status.id, status)
this.$forceUpdate()
}
ipcRenderer.on(`update-${timeline.name}-no-auth`, updateListener)
this.updateListeners.push([`update-${timeline.name}-no-auth`, updateListener])
// delete
let deleteListener = (_: Event, id: number) => {
if (timeline.statuses === undefined) {
timeline.statuses = new Map()
}
timeline.statuses.delete(id)
this.$forceUpdate()
}
@ -85,13 +105,6 @@ export default class AddColumn extends Vue {
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>=

View File

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

View File

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