utils.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getStorage = getStorage;
  6. exports.usePromise = usePromise;
  7. /**
  8. * Need to invoke a function at runtime instead of import-time to make tests
  9. * pass with mocked browser and chrome objects
  10. */
  11. function getStorage() {
  12. return typeof browser !== 'undefined' && browser.storage || typeof chrome !== 'undefined' && chrome.storage;
  13. }
  14. /**
  15. * Need to invoke a function at runtime instead of import-time to make tests
  16. * pass with mocked browser and chrome objects
  17. */
  18. function usesPromises() {
  19. var storage = getStorage();
  20. try {
  21. return storage && storage.local.get && storage.local.get() && typeof storage.local.get().then === 'function';
  22. } catch (e) {
  23. return false;
  24. }
  25. }
  26. /**
  27. * Converts a callback-based API to a promise based API.
  28. * For now we assume that there is only one arg in addition to the callback
  29. */
  30. function usePromise(fn, arg) {
  31. if (usesPromises()) {
  32. return fn(arg);
  33. }
  34. return new Promise(function (resolve) {
  35. fn(arg, function () {
  36. resolve.apply(void 0, arguments);
  37. });
  38. });
  39. }