UNPKG

4.34 kBJavaScriptView Raw
1'use strict';
2var call = require('../internals/function-call');
3var perform = require('../internals/perform');
4var anObject = require('../internals/an-object');
5var create = require('../internals/object-create');
6var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
7var defineBuiltIns = require('../internals/define-built-ins');
8var wellKnownSymbol = require('../internals/well-known-symbol');
9var InternalStateModule = require('../internals/internal-state');
10var getBuiltIn = require('../internals/get-built-in');
11var getMethod = require('../internals/get-method');
12var AsyncIteratorPrototype = require('../internals/async-iterator-prototype');
13var createIterResultObject = require('../internals/create-iter-result-object');
14var iteratorClose = require('../internals/iterator-close');
15
16var Promise = getBuiltIn('Promise');
17
18var TO_STRING_TAG = wellKnownSymbol('toStringTag');
19var ASYNC_ITERATOR_HELPER = 'AsyncIteratorHelper';
20var WRAP_FOR_VALID_ASYNC_ITERATOR = 'WrapForValidAsyncIterator';
21var setInternalState = InternalStateModule.set;
22
23var 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
84var WrapForValidAsyncIteratorPrototype = createAsyncIteratorProxyPrototype(true);
85var AsyncIteratorHelperPrototype = createAsyncIteratorProxyPrototype(false);
86
87createNonEnumerableProperty(AsyncIteratorHelperPrototype, TO_STRING_TAG, 'Async Iterator Helper');
88
89module.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};