async.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.forwardAsync = forwardAsync;
  6. exports.isAsync = void 0;
  7. exports.isThenable = isThenable;
  8. exports.maybeAsync = maybeAsync;
  9. exports.waitFor = exports.onFirstPause = void 0;
  10. function _gensync() {
  11. const data = require("gensync");
  12. _gensync = function () {
  13. return data;
  14. };
  15. return data;
  16. }
  17. function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
  18. function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
  19. const runGenerator = _gensync()(function* (item) {
  20. return yield* item;
  21. });
  22. const isAsync = exports.isAsync = _gensync()({
  23. sync: () => false,
  24. errback: cb => cb(null, true)
  25. });
  26. function maybeAsync(fn, message) {
  27. return _gensync()({
  28. sync(...args) {
  29. const result = fn.apply(this, args);
  30. if (isThenable(result)) throw new Error(message);
  31. return result;
  32. },
  33. async(...args) {
  34. return Promise.resolve(fn.apply(this, args));
  35. }
  36. });
  37. }
  38. const withKind = _gensync()({
  39. sync: cb => cb("sync"),
  40. async: function () {
  41. var _ref = _asyncToGenerator(function* (cb) {
  42. return cb("async");
  43. });
  44. return function async(_x) {
  45. return _ref.apply(this, arguments);
  46. };
  47. }()
  48. });
  49. function forwardAsync(action, cb) {
  50. const g = _gensync()(action);
  51. return withKind(kind => {
  52. const adapted = g[kind];
  53. return cb(adapted);
  54. });
  55. }
  56. const onFirstPause = exports.onFirstPause = _gensync()({
  57. name: "onFirstPause",
  58. arity: 2,
  59. sync: function (item) {
  60. return runGenerator.sync(item);
  61. },
  62. errback: function (item, firstPause, cb) {
  63. let completed = false;
  64. runGenerator.errback(item, (err, value) => {
  65. completed = true;
  66. cb(err, value);
  67. });
  68. if (!completed) {
  69. firstPause();
  70. }
  71. }
  72. });
  73. const waitFor = exports.waitFor = _gensync()({
  74. sync: x => x,
  75. async: function () {
  76. var _ref2 = _asyncToGenerator(function* (x) {
  77. return x;
  78. });
  79. return function async(_x2) {
  80. return _ref2.apply(this, arguments);
  81. };
  82. }()
  83. });
  84. function isThenable(val) {
  85. return !!val && (typeof val === "object" || typeof val === "function") && !!val.then && typeof val.then === "function";
  86. }
  87. 0 && 0;
  88. //# sourceMappingURL=async.js.map