From cee7b7f0791b9d1b2f95a39ec968c4977225606a Mon Sep 17 00:00:00 2001 From: kPherox Date: Wed, 24 Apr 2019 17:15:42 +0900 Subject: [PATCH] Remove listener when component destroy --- src/components/AddColumn/PublicTimeline.vue | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/AddColumn/PublicTimeline.vue b/src/components/AddColumn/PublicTimeline.vue index 67d8eba6..91c6dc9c 100644 --- a/src/components/AddColumn/PublicTimeline.vue +++ b/src/components/AddColumn/PublicTimeline.vue @@ -32,18 +32,25 @@ import { Component, Vue } from 'vue-property-decorator' import { Status } from 'megalodon' type Instance = string -type showInput = boolean type Timeline = { name: string statuses: Status[] } +type UpdateListener = (e: Event, status: Status) => void type Timelines = Timeline[] @Component export default class AddColumn extends Vue { public instance: Instance = '' - public showInput: showInput = true + public showInput: boolean = true + public updateListeners: [string, UpdateListener][] = [] public pubTL: Timelines = [] + beforeDestroy() { + this.updateListeners.forEach(([name, listener]) => { + ipcRenderer.removeListener(name, listener) + }) + } + public get hasDomain() { return this.instance != '' } @@ -55,12 +62,16 @@ export default class AddColumn extends Vue { this.pubTL.push({ name: instance, statuses: [] }) this.timeline() - ipcRenderer.send('open-streaming', instance, 'no-auth') - ipcRenderer.on(`update-${instance}-no-auth`, (_: Event, status: Status) => { + let updateListener = (_: Event, status: Status) => { this.pubTL.filter(tl => tl.name === instance).forEach(function (tl) { 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() {