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