cmd-run.js 1004 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import bridge from './bridge';
  2. import { sendCmd } from './util-content';
  3. import { INJECT_CONTENT } from '../util';
  4. const { runningIds } = bridge;
  5. const resolvedPromise = promiseResolve();
  6. let badgePromise;
  7. let numBadgesSent = 0;
  8. let bfCacheWired;
  9. export function Run(id, realm) {
  10. runningIds::push(id);
  11. bridge.ids::push(id);
  12. if (realm === INJECT_CONTENT) {
  13. bridge.invokableIds::push(id);
  14. }
  15. if (!badgePromise) {
  16. badgePromise = resolvedPromise::then(throttledSetBadge);
  17. }
  18. if (!bfCacheWired) {
  19. bfCacheWired = true;
  20. window::on('pageshow', evt => {
  21. // isTrusted is `unforgeable` per DOM spec so we don't need to safeguard its getter
  22. if (evt.isTrusted && evt.persisted) {
  23. sendCmd('SetBadge', runningIds);
  24. }
  25. });
  26. }
  27. }
  28. function throttledSetBadge() {
  29. const num = runningIds.length;
  30. if (numBadgesSent < num) {
  31. numBadgesSent = num;
  32. return sendCmd('SetBadge', runningIds)::then(() => {
  33. badgePromise = throttledSetBadge();
  34. });
  35. }
  36. }