Skip to content

Commit

Permalink
Fallback to @@iterator and @@asyncIterator to be compatible with rege…
Browse files Browse the repository at this point in the history
…nerator
  • Loading branch information
rpetrich committed May 26, 2019
1 parent 14681af commit 900d216
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion async-to-promises.test.js
Expand Up @@ -37,7 +37,7 @@ const environments = {
},
};

const helperNames = ["_Pact", "_settle", "_isSettledPact", "_async", "_await", "_awaitIgnored", "_continue", "_continueIgnored", "_forTo", "_forValues", "_forIn", "_forOwn", "_forOf", "_forAwaitOf", "_for", "_do", "_switch", "_call", "_callIgnored", "_invoke", "_invokeIgnored", "_catch", "_finallyRethrows", "_finally", "_rethrow", "_empty", "_earlyReturn", "_catchInGenerator", "_wrapReturnedValue", "_wrapYieldedValue", "_AsyncGenerator", "_asyncIteratorSymbol"];
const helperNames = ["_Pact", "_settle", "_isSettledPact", "_async", "_await", "_awaitIgnored", "_continue", "_continueIgnored", "_forTo", "_forValues", "_forIn", "_forOwn", "_forOf", "_forAwaitOf", "_for", "_do", "_switch", "_call", "_callIgnored", "_invoke", "_invokeIgnored", "_catch", "_finallyRethrows", "_finally", "_rethrow", "_empty", "_earlyReturn", "_catchInGenerator", "_wrapReturnedValue", "_wrapYieldedValue", "_AsyncGenerator", "_iteratorSymbol", "_asyncIteratorSymbol"];

const stripHelpersVisitor = {
FunctionDeclaration(path) {
Expand Down
77 changes: 38 additions & 39 deletions helpers.js
Expand Up @@ -162,55 +162,54 @@ export function _forOwn(target, body, check) {
return _forTo(keys, function(i) { return body(keys[i]); }, check);
}

export const _iteratorSymbol = typeof Symbol !== "undefined" ? (Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator"))) : "@@iterator";

// Asynchronously iterate through an object's values
// Uses for...of if the runtime supports it, otherwise iterates until length on a copy
export function _forOf(target, body, check) {
if (typeof Symbol !== "undefined") {
var iteratorSymbol = Symbol.iterator;
if (iteratorSymbol && (iteratorSymbol in target)) {
var iterator = target[iteratorSymbol](), step, pact, reject;
function _cycle(result) {
try {
while (!(step = iterator.next()).done && (!check || !check())) {
result = body(step.value);
if (result && result.then) {
if (_isSettledPact(result)) {
result = result.v;
} else {
result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
return;
}
if (typeof target[_iteratorSymbol] === "function") {
var iterator = target[_iteratorSymbol](), step, pact, reject;
function _cycle(result) {
try {
while (!(step = iterator.next()).done && (!check || !check())) {
result = body(step.value);
if (result && result.then) {
if (_isSettledPact(result)) {
result = result.v;
} else {
result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
return;
}
}
if (pact) {
_settle(pact, 1, result);
} else {
pact = result;
}
} catch (e) {
_settle(pact || (pact = new Pact()), 2, e);
}
if (pact) {
_settle(pact, 1, result);
} else {
pact = result;
}
} catch (e) {
_settle(pact || (pact = new Pact()), 2, e);
}
_cycle();
if (iterator.return) {
var _fixup = function(value) {
try {
if (!step.done) {
iterator.return();
}
} catch(e) {
}
_cycle();
if (iterator.return) {
var _fixup = function(value) {
try {
if (!step.done) {
iterator.return();
}
return value;
} catch(e) {
}
if (pact && pact.then) {
return pact.then(_fixup, function(e) {
throw _fixup(e);
});
}
_fixup();
return value;
}
if (pact && pact.then) {
return pact.then(_fixup, function(e) {
throw _fixup(e);
});
}
return pact;
_fixup();
}
return pact;
}
// No support for Symbol.iterator
if (!("length" in target)) {
Expand All @@ -224,7 +223,7 @@ export function _forOf(target, body, check) {
return _forTo(values, function(i) { return body(values[i]); }, check);
}

export const _asyncIteratorSymbol = typeof Symbol !== "undefined" ? (Symbol.asyncIterator || (Symbol.asyncIterator = Symbol("Symbol.asyncIterator"))) : "Symbol.asyncIterator";
export const _asyncIteratorSymbol = typeof Symbol !== "undefined" ? (Symbol.asyncIterator || (Symbol.asyncIterator = Symbol("Symbol.asyncIterator"))) : "@@asyncIterator";

// Asynchronously iterate on a value using it's async iterator if present, or its synchronous iterator if missing
export function _forAwaitOf(target, body, check) {
Expand Down

0 comments on commit 900d216

Please sign in to comment.