Remove listener when component destroy

This commit is contained in:
kPherox 2019-04-24 17:15:42 +09:00
parent 52c6ff22ee
commit cee7b7f079
No known key found for this signature in database
GPG Key ID: C04751C2BFA2F62D

View File

@ -32,18 +32,25 @@ import { Component, Vue } from 'vue-property-decorator'
import { Status } from 'megalodon' import { Status } from 'megalodon'
type Instance = string type Instance = string
type showInput = boolean
type Timeline = { type Timeline = {
name: string name: string
statuses: Status[] statuses: Status[]
} }
type UpdateListener = (e: Event, status: Status) => void
type Timelines = Timeline[] type Timelines = Timeline[]
@Component @Component
export default class AddColumn extends Vue { export default class AddColumn extends Vue {
public instance: Instance = '' public instance: Instance = ''
public showInput: showInput = true public showInput: boolean = true
public updateListeners: [string, UpdateListener][] = []
public pubTL: Timelines = [] public pubTL: Timelines = []
beforeDestroy() {
this.updateListeners.forEach(([name, listener]) => {
ipcRenderer.removeListener(name, listener)
})
}
public get hasDomain() { public get hasDomain() {
return this.instance != '' return this.instance != ''
} }
@ -55,12 +62,16 @@ export default class AddColumn extends Vue {
this.pubTL.push({ name: instance, statuses: [] }) this.pubTL.push({ name: instance, statuses: [] })
this.timeline() this.timeline()
ipcRenderer.send('open-streaming', instance, 'no-auth') let updateListener = (_: Event, status: Status) => {
ipcRenderer.on(`update-${instance}-no-auth`, (_: Event, status: Status) => {
this.pubTL.filter(tl => tl.name === instance).forEach(function (tl) { this.pubTL.filter(tl => tl.name === instance).forEach(function (tl) {
tl.statuses.unshift(status) tl.statuses.unshift(status)
}) })
}) this.$forceUpdate()
}
ipcRenderer.on(`update-${instance}-no-auth`, updateListener)
this.updateListeners.push([`update-${instance}-no-auth`, updateListener])
ipcRenderer.send('open-streaming', instance, 'no-auth')
} }
public timeline() { public timeline() {