From 131ee41b23330b8f39f260b1e26bcbba3f1e47d5 Mon Sep 17 00:00:00 2001 From: lutangar Date: Mon, 16 Dec 2019 14:33:36 +0100 Subject: [PATCH] fix(badge): badge not updating when switching to a not yet registered tab --- src/app/actions/tabsLifecycle.ts | 12 ++++++++- src/app/background/sagas/index.ts | 2 ++ .../sagas/watchActivatedTab.saga.ts | 27 +++++++++++++++++++ src/webext/createActivatedTabListener.ts | 17 ++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/app/background/sagas/watchActivatedTab.saga.ts create mode 100644 src/webext/createActivatedTabListener.ts diff --git a/src/app/actions/tabsLifecycle.ts b/src/app/actions/tabsLifecycle.ts index dbcc0b665..c26fa6609 100644 --- a/src/app/actions/tabsLifecycle.ts +++ b/src/app/actions/tabsLifecycle.ts @@ -1,6 +1,6 @@ import { ReceivedAction } from 'webext/createMessageHandler'; import Tab from 'app/lmem/tab'; -import { BaseAction, TabAction } from '.'; +import { BaseAction, StandardAction, TabAction } from '.'; export const TAB_REMOVED = 'BROWSER/TAB_REMOVED'; @@ -23,3 +23,13 @@ export const tabDied = (tab: Tab): TabDiedAction => ({ type: TAB_DIED, meta: { tab } }); + +export const TAB_ACTIVATED = 'BROWSER/TAB_ACTIVATED'; +export interface TabActivatedAction extends StandardAction { + type: typeof TAB_ACTIVATED; + meta: { tabId: number }; +} +export const tabActivated = (tabId: number): TabActivatedAction => ({ + type: TAB_ACTIVATED, + meta: { tabId } +}); diff --git a/src/app/background/sagas/index.ts b/src/app/background/sagas/index.ts index 0201ac77b..a9d1fd4ea 100644 --- a/src/app/background/sagas/index.ts +++ b/src/app/background/sagas/index.ts @@ -8,6 +8,7 @@ import error from '../../sagas/error'; import listenActionsFromMessages from '../../sagas/listenActionsFromMessages'; import refreshMatchingContexts from './refreshMatchingContexts'; import refreshContributors from './refreshContributors'; +import watchActivatedTab from './watchActivatedTab.saga'; import watchBrowserAction from './watchBrowserAction.saga'; import handleBrowserAction from './handleBrowserAction.saga'; import openOptions from './openOptions.saga'; @@ -37,6 +38,7 @@ export default function* rootSaga() { fork(listenActionsFromMessages('background')), fork(externalMessage), fork(watchBrowserAction), + fork(watchActivatedTab), fork(handleBrowserAction), fork(openOptions), fork(ratings), diff --git a/src/app/background/sagas/watchActivatedTab.saga.ts b/src/app/background/sagas/watchActivatedTab.saga.ts new file mode 100644 index 000000000..c482b3d3d --- /dev/null +++ b/src/app/background/sagas/watchActivatedTab.saga.ts @@ -0,0 +1,27 @@ +import { call, put, take, select } from '@redux-saga/core/effects'; +import { eventChannel } from '@redux-saga/core'; +import createActivatedTabListener from 'webext/createActivatedTabListener'; +import { createErrorAction } from 'app/actions'; +import { getTabById } from '../selectors/tabs'; +import { disable } from 'webext/browserAction'; +import { resetBadge } from 'app/lmem/badge'; + +export default function*() { + const channel = yield call(() => eventChannel(createActivatedTabListener)); + + while (true) { + try { + const { + meta: { tabId } + } = yield take(channel); + const tab = yield select(getTabById(tabId)); + + if (!tab || !tab.url) { + yield call(resetBadge, tabId); + yield call(disable, tabId); + } + } catch (e) { + yield put(createErrorAction()(e)); + } + } +} diff --git a/src/webext/createActivatedTabListener.ts b/src/webext/createActivatedTabListener.ts new file mode 100644 index 000000000..bf9d4960c --- /dev/null +++ b/src/webext/createActivatedTabListener.ts @@ -0,0 +1,17 @@ +import { Action } from 'redux'; +import { tabActivated } from 'app/actions'; + +type Emit = (action: Action) => void; + +export default (emit: Emit) => { + const handleTabActivated = ({ tabId }: chrome.tabs.TabActiveInfo) => { + emit(tabActivated(tabId)); + }; + + chrome.tabs.onActivated.addListener(handleTabActivated); + + // unsubscribe + return () => { + chrome.tabs.onActivated.removeListener(handleTabActivated); + }; +};