Skip to content

Commit

Permalink
fix(badge): badge not updating when switching to a not yet registered…
Browse files Browse the repository at this point in the history
… tab
  • Loading branch information
lutangar committed Dec 20, 2019
1 parent 7b52646 commit 131ee41
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/app/actions/tabsLifecycle.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 }
});
2 changes: 2 additions & 0 deletions src/app/background/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -37,6 +38,7 @@ export default function* rootSaga() {
fork(listenActionsFromMessages('background')),
fork(externalMessage),
fork(watchBrowserAction),
fork(watchActivatedTab),
fork(handleBrowserAction),
fork(openOptions),
fork(ratings),
Expand Down
27 changes: 27 additions & 0 deletions src/app/background/sagas/watchActivatedTab.saga.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
17 changes: 17 additions & 0 deletions src/webext/createActivatedTabListener.ts
Original file line number Diff line number Diff line change
@@ -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);
};
};

0 comments on commit 131ee41

Please sign in to comment.