esnext.async-iterator.flat-map.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. // https://github.com/tc39/proposal-iterator-helpers
  3. var $ = require('../internals/export');
  4. var call = require('../internals/function-call');
  5. var aCallable = require('../internals/a-callable');
  6. var anObject = require('../internals/an-object');
  7. var isObject = require('../internals/is-object');
  8. var getIteratorDirect = require('../internals/get-iterator-direct');
  9. var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
  10. var createIterResultObject = require('../internals/create-iter-result-object');
  11. var getAsyncIteratorFlattenable = require('../internals/get-async-iterator-flattenable');
  12. var closeAsyncIteration = require('../internals/async-iterator-close');
  13. var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
  14. var state = this;
  15. var iterator = state.iterator;
  16. var mapper = state.mapper;
  17. return new Promise(function (resolve, reject) {
  18. var doneAndReject = function (error) {
  19. state.done = true;
  20. reject(error);
  21. };
  22. var ifAbruptCloseAsyncIterator = function (error) {
  23. closeAsyncIteration(iterator, doneAndReject, error, doneAndReject);
  24. };
  25. var outerLoop = function () {
  26. try {
  27. Promise.resolve(anObject(call(state.next, iterator))).then(function (step) {
  28. try {
  29. if (anObject(step).done) {
  30. state.done = true;
  31. resolve(createIterResultObject(undefined, true));
  32. } else {
  33. var value = step.value;
  34. try {
  35. var result = mapper(value, state.counter++);
  36. var handler = function (mapped) {
  37. try {
  38. state.inner = getAsyncIteratorFlattenable(mapped);
  39. innerLoop();
  40. } catch (error4) { ifAbruptCloseAsyncIterator(error4); }
  41. };
  42. if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
  43. else handler(result);
  44. } catch (error3) { ifAbruptCloseAsyncIterator(error3); }
  45. }
  46. } catch (error2) { doneAndReject(error2); }
  47. }, doneAndReject);
  48. } catch (error) { doneAndReject(error); }
  49. };
  50. var innerLoop = function () {
  51. var inner = state.inner;
  52. if (inner) {
  53. try {
  54. Promise.resolve(anObject(call(inner.next, inner.iterator))).then(function (result) {
  55. try {
  56. if (anObject(result).done) {
  57. state.inner = null;
  58. outerLoop();
  59. } else resolve(createIterResultObject(result.value, false));
  60. } catch (error1) { ifAbruptCloseAsyncIterator(error1); }
  61. }, ifAbruptCloseAsyncIterator);
  62. } catch (error) { ifAbruptCloseAsyncIterator(error); }
  63. } else outerLoop();
  64. };
  65. innerLoop();
  66. });
  67. });
  68. $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
  69. flatMap: function flatMap(mapper) {
  70. return new AsyncIteratorProxy(getIteratorDirect(this), {
  71. mapper: aCallable(mapper),
  72. inner: null
  73. });
  74. }
  75. });