mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 21:21:31 +03:00
7730a6ea5a
swc_ecma_transforms_compat: - `async_to_generator`: Handle await in async generators correctly. (#1752) swc_ecma_transforms_module: - Don't panic on double import from one module. (#1757)
185 lines
5.5 KiB
JavaScript
185 lines
5.5 KiB
JavaScript
function AsyncGenerator(gen) {
|
|
var front, back;
|
|
function send(key, arg) {
|
|
return new Promise(function(resolve, reject) {
|
|
var request = {
|
|
key: key,
|
|
arg: arg,
|
|
resolve: resolve,
|
|
reject: reject,
|
|
next: null
|
|
};
|
|
if (back) {
|
|
back = back.next = request;
|
|
} else {
|
|
front = back = request;
|
|
resume(key, arg);
|
|
}
|
|
});
|
|
}
|
|
function resume(key, arg) {
|
|
try {
|
|
var result = gen[key](arg);
|
|
var value = result.value;
|
|
var wrappedAwait = value instanceof _AwaitValue;
|
|
Promise.resolve(wrappedAwait ? value.wrapped : value).then(function(arg) {
|
|
if (wrappedAwait) {
|
|
resume("next", arg);
|
|
return;
|
|
}
|
|
settle(result.done ? "return" : "normal", arg);
|
|
}, function(err) {
|
|
resume("throw", err);
|
|
});
|
|
} catch (err) {
|
|
settle("throw", err);
|
|
}
|
|
}
|
|
function settle(type, value) {
|
|
switch(type){
|
|
case "return":
|
|
front.resolve({
|
|
value: value,
|
|
done: true
|
|
});
|
|
break;
|
|
case "throw":
|
|
front.reject(value);
|
|
break;
|
|
default:
|
|
front.resolve({
|
|
value: value,
|
|
done: false
|
|
});
|
|
break;
|
|
}
|
|
front = front.next;
|
|
if (front) {
|
|
resume(front.key, front.arg);
|
|
} else {
|
|
back = null;
|
|
}
|
|
}
|
|
this._invoke = send;
|
|
if (typeof gen.return !== "function") {
|
|
this.return = undefined;
|
|
}
|
|
}
|
|
if (typeof Symbol === "function" && Symbol.asyncIterator) {
|
|
AsyncGenerator.prototype[Symbol.asyncIterator] = function() {
|
|
return this;
|
|
};
|
|
}
|
|
AsyncGenerator.prototype.next = function(arg) {
|
|
return this._invoke("next", arg);
|
|
};
|
|
AsyncGenerator.prototype.throw = function(arg) {
|
|
return this._invoke("throw", arg);
|
|
};
|
|
AsyncGenerator.prototype.return = function(arg) {
|
|
return this._invoke("return", arg);
|
|
};
|
|
function _asyncIterator(iterable) {
|
|
var method;
|
|
if (typeof Symbol === "function") {
|
|
if (Symbol.asyncIterator) {
|
|
method = iterable[Symbol.asyncIterator];
|
|
if (method != null) return method.call(iterable);
|
|
}
|
|
if (Symbol.iterator) {
|
|
method = iterable[Symbol.iterator];
|
|
if (method != null) return method.call(iterable);
|
|
}
|
|
}
|
|
throw new TypeError("Object is not async iterable");
|
|
}
|
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
try {
|
|
var info = gen[key](arg);
|
|
var value = info.value;
|
|
} catch (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
if (info.done) {
|
|
resolve(value);
|
|
} else {
|
|
Promise.resolve(value).then(_next, _throw);
|
|
}
|
|
}
|
|
function _asyncToGenerator(fn) {
|
|
return function() {
|
|
var self = this, args = arguments;
|
|
return new Promise(function(resolve, reject) {
|
|
var gen = fn.apply(self, args);
|
|
function _next(value) {
|
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
}
|
|
function _throw(err) {
|
|
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
}
|
|
_next(undefined);
|
|
});
|
|
};
|
|
}
|
|
function _awaitAsyncGenerator(value) {
|
|
return new _AwaitValue(value);
|
|
}
|
|
function _AwaitValue(value) {
|
|
this.wrapped = value;
|
|
}
|
|
function _wrapAsyncGenerator(fn) {
|
|
return function() {
|
|
return new AsyncGenerator(fn.apply(this, arguments));
|
|
};
|
|
}
|
|
function _generate() {
|
|
_generate = _wrapAsyncGenerator(function*() {
|
|
const results = yield _awaitAsyncGenerator(Promise.all([
|
|
Promise.resolve(1),
|
|
Promise.resolve(2),
|
|
Promise.resolve(3),
|
|
]));
|
|
for (const result of results){
|
|
console.log(`yield ${result}`);
|
|
yield result;
|
|
}
|
|
});
|
|
return _generate.apply(this, arguments);
|
|
}
|
|
function generate() {
|
|
return _generate.apply(this, arguments);
|
|
}
|
|
function _printValues() {
|
|
_printValues = _asyncToGenerator(function*() {
|
|
const iterator = generate();
|
|
{
|
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError;
|
|
try {
|
|
for(var _iterator = _asyncIterator(iterator), _step, _value; _step = yield _iterator.next(), _iteratorNormalCompletion = _step.done, _value = yield _step.value, !_iteratorNormalCompletion; _iteratorNormalCompletion = true){
|
|
const value = _value;
|
|
console.log(`iterator value: ${value}`);
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally{
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
yield _iteratorError.return();
|
|
}
|
|
} finally{
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
return _printValues.apply(this, arguments);
|
|
}
|
|
function printValues() {
|
|
return _printValues.apply(this, arguments);
|
|
}
|
|
printValues();
|