message: fix(badges): prevent stale values
diff: import { message } from '@/_helpers/browser-api'
  +import { Subject } from 'rxjs'
  +import { debounceTime, switchMap } from 'rxjs/operators'
  -export function initBadge() {
  - /** Sent when content script loaded */
  - message.addListener('SEND_TAB_BADGE_INFO', ({ payload }, sender) => {
  - if (sender.tab && sender.tab.id) {
  - updateBadge(sender.tab.id, payload)
  +interface UpdateBadgeOptions {
  + active: boolean
  + tempDisable: boolean
  + unsupported: boolean
  }
  - })
  - browser.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
  - if (changeInfo.status === 'complete') {
  - try {
  - const response = await message.send<'GET_TAB_BADGE_INFO'>(tabId, {
  +const onUpdated$ = new Subject<[number, UpdateBadgeOptions | null]>()
  +
  +onUpdated$
  + .pipe(
  + debounceTime(1000),
  + switchMap(async ([tabId, options]) =>
  + options
  + ? ([tabId, options] as const)
  + : ([
  + tabId,
  + (await message
  + .send<'GET_TAB_BADGE_INFO'>(tabId, {
  type: 'GET_TAB_BADGE_INFO'
  })
  - updateBadge(tabId, response)
  - } catch (e) {
  - updateBadge(tabId, {
  + .catch(() => {})) || {
  active: window.appConfig.active,
  tempDisable: false,
  unsupported: true
  - })
  - }
  - }
  - })
  -}
  -
  -export function updateBadge(
  - tabId: number,
  - options: {
  - active: boolean
  - tempDisable: boolean
  - unsupported: boolean
  }
  -) {
  + ] as const)
  + )
  + )
  + .subscribe(([tabId, options]) => {
  if (!options.active) {
  return setOff(tabId)
  }
  @@ -47,6 +44,21 @@ export function updateBadge(
  }
  return setEmpty(tabId)
  + })
  +
  +export function initBadge() {
  + /** Sent when content script loaded */
  + message.addListener('SEND_TAB_BADGE_INFO', ({ payload }, sender) => {
  + if (sender.tab && sender.tab.id) {
  + onUpdated$.next([sender.tab.id, payload])
  + }
  + })
  +
  + browser.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
  + if (changeInfo.status === 'complete') {
  + onUpdated$.next([tabId, null])
  + }
  + })
  }
  function setOff(tabId: number) {
  