1 | 'use strict';
|
2 | var call = require('../internals/function-call');
|
3 | var perform = require('../internals/perform');
|
4 | var anObject = require('../internals/an-object');
|
5 | var create = require('../internals/object-create');
|
6 | var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
|
7 | var defineBuiltIns = require('../internals/define-built-ins');
|
8 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
9 | var InternalStateModule = require('../internals/internal-state');
|
10 | var getBuiltIn = require('../internals/get-built-in');
|
11 | var getMethod = require('../internals/get-method');
|
12 | var AsyncIteratorPrototype = require('../internals/async-iterator-prototype');
|
13 | var createIterResultObject = require('../internals/create-iter-result-object');
|
14 | var iteratorClose = require('../internals/iterator-close');
|
15 |
|
16 | var Promise = getBuiltIn('Promise');
|
17 |
|
18 | var TO_STRING_TAG = wellKnownSymbol('toStringTag');
|
19 | var ASYNC_ITERATOR_HELPER = 'AsyncIteratorHelper';
|
20 | var WRAP_FOR_VALID_ASYNC_ITERATOR = 'WrapForValidAsyncIterator';
|
21 | var setInternalState = InternalStateModule.set;
|
22 |
|
23 | var createAsyncIteratorProxyPrototype = function (IS_ITERATOR) {
|
24 | var IS_GENERATOR = !IS_ITERATOR;
|
25 | var getInternalState = InternalStateModule.getterFor(IS_ITERATOR ? WRAP_FOR_VALID_ASYNC_ITERATOR : ASYNC_ITERATOR_HELPER);
|
26 |
|
27 | var getStateOrEarlyExit = function (that) {
|
28 | var stateCompletion = perform(function () {
|
29 | return getInternalState(that);
|
30 | });
|
31 |
|
32 | var stateError = stateCompletion.error;
|
33 | var state = stateCompletion.value;
|
34 |
|
35 | if (stateError || (IS_GENERATOR && state.done)) {
|
36 | return { exit: true, value: stateError ? Promise.reject(state) : Promise.resolve(createIterResultObject(undefined, true)) };
|
37 | } return { exit: false, value: state };
|
38 | };
|
39 |
|
40 | return defineBuiltIns(create(AsyncIteratorPrototype), {
|
41 | next: function next() {
|
42 | var stateCompletion = getStateOrEarlyExit(this);
|
43 | var state = stateCompletion.value;
|
44 | if (stateCompletion.exit) return state;
|
45 | var handlerCompletion = perform(function () {
|
46 | return anObject(state.nextHandler(Promise));
|
47 | });
|
48 | var handlerError = handlerCompletion.error;
|
49 | var value = handlerCompletion.value;
|
50 | if (handlerError) state.done = true;
|
51 | return handlerError ? Promise.reject(value) : Promise.resolve(value);
|
52 | },
|
53 | 'return': function () {
|
54 | var stateCompletion = getStateOrEarlyExit(this);
|
55 | var state = stateCompletion.value;
|
56 | if (stateCompletion.exit) return state;
|
57 | state.done = true;
|
58 | var iterator = state.iterator;
|
59 | var returnMethod, result;
|
60 | var completion = perform(function () {
|
61 | if (state.inner) try {
|
62 | iteratorClose(state.inner.iterator, 'normal');
|
63 | } catch (error) {
|
64 | return iteratorClose(iterator, 'throw', error);
|
65 | }
|
66 | return getMethod(iterator, 'return');
|
67 | });
|
68 | returnMethod = result = completion.value;
|
69 | if (completion.error) return Promise.reject(result);
|
70 | if (returnMethod === undefined) return Promise.resolve(createIterResultObject(undefined, true));
|
71 | completion = perform(function () {
|
72 | return call(returnMethod, iterator);
|
73 | });
|
74 | result = completion.value;
|
75 | if (completion.error) return Promise.reject(result);
|
76 | return IS_ITERATOR ? Promise.resolve(result) : Promise.resolve(result).then(function (resolved) {
|
77 | anObject(resolved);
|
78 | return createIterResultObject(undefined, true);
|
79 | });
|
80 | }
|
81 | });
|
82 | };
|
83 |
|
84 | var WrapForValidAsyncIteratorPrototype = createAsyncIteratorProxyPrototype(true);
|
85 | var AsyncIteratorHelperPrototype = createAsyncIteratorProxyPrototype(false);
|
86 |
|
87 | createNonEnumerableProperty(AsyncIteratorHelperPrototype, TO_STRING_TAG, 'Async Iterator Helper');
|
88 |
|
89 | module.exports = function (nextHandler, IS_ITERATOR) {
|
90 | var AsyncIteratorProxy = function AsyncIterator(record, state) {
|
91 | if (state) {
|
92 | state.iterator = record.iterator;
|
93 | state.next = record.next;
|
94 | } else state = record;
|
95 | state.type = IS_ITERATOR ? WRAP_FOR_VALID_ASYNC_ITERATOR : ASYNC_ITERATOR_HELPER;
|
96 | state.nextHandler = nextHandler;
|
97 | state.counter = 0;
|
98 | state.done = false;
|
99 | setInternalState(this, state);
|
100 | };
|
101 |
|
102 | AsyncIteratorProxy.prototype = IS_ITERATOR ? WrapForValidAsyncIteratorPrototype : AsyncIteratorHelperPrototype;
|
103 |
|
104 | return AsyncIteratorProxy;
|
105 | };
|