From 72ed7528f4f92b8768f3aa999e85ede6712feec2 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Mon, 1 Jul 2019 22:47:01 +0900
Subject: [PATCH] refs #921 TimelineSpace hold only one streaming account, and
wait to unbind before bind streamings
---
.../integration/store/TimelineSpace.spec.ts | 2 +-
.../renderer/unit/store/TimelineSpace.spec.ts | 2 +-
src/renderer/store/TimelineSpace.ts | 49 +++++++++++++------
3 files changed, 35 insertions(+), 18 deletions(-)
diff --git a/spec/renderer/integration/store/TimelineSpace.spec.ts b/spec/renderer/integration/store/TimelineSpace.spec.ts
index cfe3aab5..d47bf94a 100644
--- a/spec/renderer/integration/store/TimelineSpace.spec.ts
+++ b/spec/renderer/integration/store/TimelineSpace.spec.ts
@@ -30,7 +30,7 @@ const mockedInstance: Instance = {
const state = (): TimelineSpaceState => {
return {
account: blankAccount,
- previousAccount: null,
+ bindingAccount: null,
loading: false,
emojis: [],
tootMax: 500,
diff --git a/spec/renderer/unit/store/TimelineSpace.spec.ts b/spec/renderer/unit/store/TimelineSpace.spec.ts
index 516b27c7..32ae0d58 100644
--- a/spec/renderer/unit/store/TimelineSpace.spec.ts
+++ b/spec/renderer/unit/store/TimelineSpace.spec.ts
@@ -8,7 +8,7 @@ describe('TimelineSpace', () => {
beforeEach(() => {
state = {
account: blankAccount,
- previousAccount: null,
+ bindingAccount: null,
loading: false,
emojis: [],
tootMax: 500,
diff --git a/src/renderer/store/TimelineSpace.ts b/src/renderer/store/TimelineSpace.ts
index 80175a5d..6000f920 100644
--- a/src/renderer/store/TimelineSpace.ts
+++ b/src/renderer/store/TimelineSpace.ts
@@ -19,7 +19,7 @@ type MyEmoji = {
export type TimelineSpaceState = {
account: LocalAccount
- previousAccount: LocalAccount | null
+ bindingAccount: LocalAccount | null
loading: boolean
emojis: Array
tootMax: number
@@ -44,7 +44,7 @@ export const blankAccount: LocalAccount = {
const state = (): TimelineSpaceState => ({
account: blankAccount,
- previousAccount: null,
+ bindingAccount: null,
loading: false,
emojis: [],
tootMax: 500,
@@ -59,7 +59,7 @@ const state = (): TimelineSpaceState => ({
export const MUTATION_TYPES = {
UPDATE_ACCOUNT: 'updateAccount',
- UPDATE_PREVIOUS_ACCOUNT: 'updatePreviousAccount',
+ UPDATE_BINDING_ACCOUNT: 'updateBindingAccount',
CHANGE_LOADING: 'changeLoading',
UPDATE_EMOJIS: 'updateEmojis',
UPDATE_TOOT_MAX: 'updateTootMax',
@@ -72,8 +72,8 @@ const mutations: MutationTree = {
[MUTATION_TYPES.UPDATE_ACCOUNT]: (state, account: LocalAccount) => {
state.account = account
},
- [MUTATION_TYPES.UPDATE_PREVIOUS_ACCOUNT]: (state, account: LocalAccount) => {
- state.previousAccount = account
+ [MUTATION_TYPES.UPDATE_BINDING_ACCOUNT]: (state, account: LocalAccount) => {
+ state.bindingAccount = account
},
[MUTATION_TYPES.CHANGE_LOADING]: (state, value: boolean) => {
state.loading = value
@@ -123,13 +123,13 @@ const actions: ActionTree = {
})
return account
},
- prepareSpace: async ({ state, dispatch, commit }) => {
+ prepareSpace: async ({ state, dispatch }) => {
await dispatch('bindStreamings')
dispatch('startStreamings')
await dispatch('fetchEmojis', state.account)
await dispatch('fetchInstance', state.account)
- // Backup current account information.
- commit(MUTATION_TYPES.UPDATE_PREVIOUS_ACCOUNT, state.account)
+ // // Backup current account information.
+ // commit(MUTATION_TYPES.UPDATE_PREVIOUS_ACCOUNT, state.account)
},
// -------------------------------------------------
// Accounts
@@ -307,10 +307,14 @@ const actions: ActionTree = {
// ------------------------------------------------
// Each streaming methods
// ------------------------------------------------
- bindUserStreaming: ({ commit, state, rootState }) => {
+ bindUserStreaming: async ({ commit, state, rootState, dispatch }) => {
if (!state.account._id) {
throw new Error('Account is not set')
}
+ // We have to wait to unbind previous streaming.
+ await dispatch('waitToUnbindUserStreaming')
+
+ commit(MUTATION_TYPES.UPDATE_BINDING_ACCOUNT, state.account)
ipcRenderer.on(`update-start-all-user-streamings-${state.account._id!}`, (_, update: Status) => {
commit('TimelineSpace/Contents/Home/appendTimeline', update, { root: true })
// Sometimes archive old statuses
@@ -414,16 +418,18 @@ const actions: ActionTree = {
})
})
},
- unbindUserStreaming: ({ state }) => {
+ unbindUserStreaming: ({ state, commit }) => {
// When unbind is called, sometimes account is already cleared and account does not have _id.
// So we have to get previous account to unbind streamings.
- if (state.previousAccount) {
- ipcRenderer.removeAllListeners(`update-start-all-user-streamings-${state.previousAccount._id!}`)
- ipcRenderer.removeAllListeners(`mention-start-all-user-streamings-${state.previousAccount._id!}`)
- ipcRenderer.removeAllListeners(`notification-start-all-user-streamings-${state.previousAccount._id!}`)
- ipcRenderer.removeAllListeners(`delete-start-all-user-streamings-${state.previousAccount._id!}`)
+ if (state.bindingAccount) {
+ ipcRenderer.removeAllListeners(`update-start-all-user-streamings-${state.bindingAccount._id!}`)
+ ipcRenderer.removeAllListeners(`mention-start-all-user-streamings-${state.bindingAccount._id!}`)
+ ipcRenderer.removeAllListeners(`notification-start-all-user-streamings-${state.bindingAccount._id!}`)
+ ipcRenderer.removeAllListeners(`delete-start-all-user-streamings-${state.bindingAccount._id!}`)
+ // And we have to clear binding account after unbind.
+ commit(MUTATION_TYPES.UPDATE_BINDING_ACCOUNT, null)
} else {
- console.info('previous account does not exist')
+ console.info('binding account does not exist')
}
},
unbindLocalStreaming: () => {
@@ -464,6 +470,15 @@ const actions: ActionTree = {
commit('TimelineSpace/Contents/Public/updateToot', status, { root: true })
}
return true
+ },
+ waitToUnbindUserStreaming: async ({ state, dispatch }): Promise => {
+ if (!state.bindingAccount) {
+ return true
+ }
+ dispatch('unbindUserStreaming')
+ await sleep(500)
+ const res: boolean = await dispatch('waitToUnbindUserStreaming')
+ return res
}
}
@@ -490,3 +505,5 @@ const TimelineSpace: Module = {
}
export default TimelineSpace
+
+const sleep = (msec: number) => new Promise(resolve => setTimeout(resolve, msec))