import browser from "webextension-polyfill"
import { debounce, flatMap } from "lodash"
import { updateTabIcons } from "../update"

export const broadcast = debounce(async () => {
  const tabs = flatMap(
    await browser.windows.getAll({ populate: true }),
    window => window.tabs!,
  )
  tabs.forEach(tab => updateTabIcons({ ...tab, tabId: tab.id! }))
  tabs.forEach(tab => browser.tabs.sendMessage(tab.id!, { method: "fileUpdated" }))
}, 100)
