cmd-run.js 950 B

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