functional.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.once = once;
  6. var _async = require("./async.js");
  7. function once(fn) {
  8. let result;
  9. let resultP;
  10. let promiseReferenced = false;
  11. return function* () {
  12. if (!result) {
  13. if (resultP) {
  14. promiseReferenced = true;
  15. return yield* (0, _async.waitFor)(resultP);
  16. }
  17. if (!(yield* (0, _async.isAsync)())) {
  18. try {
  19. result = {
  20. ok: true,
  21. value: yield* fn()
  22. };
  23. } catch (error) {
  24. result = {
  25. ok: false,
  26. value: error
  27. };
  28. }
  29. } else {
  30. let resolve, reject;
  31. resultP = new Promise((res, rej) => {
  32. resolve = res;
  33. reject = rej;
  34. });
  35. try {
  36. result = {
  37. ok: true,
  38. value: yield* fn()
  39. };
  40. resultP = null;
  41. if (promiseReferenced) resolve(result.value);
  42. } catch (error) {
  43. result = {
  44. ok: false,
  45. value: error
  46. };
  47. resultP = null;
  48. if (promiseReferenced) reject(error);
  49. }
  50. }
  51. }
  52. if (result.ok) return result.value;else throw result.value;
  53. };
  54. }
  55. 0 && 0;
  56. //# sourceMappingURL=functional.js.map