Skip to content

Commit

Permalink
add counter argument to async iterator helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 20, 2022
1 parent c8d22f6 commit f17389a
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 59 deletions.
1 change: 1 addition & 0 deletions packages/core-js/internals/async-iterator-create-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ module.exports = function (nextHandler, IS_ITERATOR) {
} else state = record;
state.type = ASYNC_ITERATOR_PROXY;
state.nextHandler = nextHandler;
state.counter = 0;
state.done = false;
state.awaiting = null;
setInternalState(this, state);
Expand Down
14 changes: 7 additions & 7 deletions packages/core-js/internals/async-iterator-iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var createMethod = function (TYPE) {
var Promise = getBuiltIn('Promise');
var iterator = record.iterator;
var next = record.next;
var index = 0;
var counter = 0;
var MAPPING = fn !== undefined;
if (MAPPING || !IS_TO_ARRAY) aCallable(fn);

Expand All @@ -30,36 +30,36 @@ var createMethod = function (TYPE) {

var loop = function () {
try {
if (IS_TO_ARRAY && MAPPING) try {
doesNotExceedSafeInteger(index);
if (MAPPING) try {
doesNotExceedSafeInteger(counter);
} catch (error5) { ifAbruptCloseAsyncIterator(error5); }
Promise.resolve(anObject(call(next, iterator))).then(function (step) {
try {
if (anObject(step).done) {
if (IS_TO_ARRAY) {
target.length = index;
target.length = counter;
resolve(target);
} else resolve(IS_SOME ? false : IS_EVERY || undefined);
} else {
var value = step.value;
try {
if (MAPPING) {
Promise.resolve(IS_TO_ARRAY ? fn(value, index) : fn(value)).then(function (result) {
Promise.resolve(fn(value, counter)).then(function (result) {
if (IS_FOR_EACH) {
loop();
} else if (IS_EVERY) {
result ? loop() : closeAsyncIteration(iterator, resolve, false, reject);
} else if (IS_TO_ARRAY) {
try {
target[index++] = result;
target[counter++] = result;
loop();
} catch (error4) { ifAbruptCloseAsyncIterator(error4); }
} else {
result ? closeAsyncIteration(iterator, resolve, IS_SOME || value, reject) : loop();
}
}, ifAbruptCloseAsyncIterator);
} else {
target[index++] = value;
target[counter++] = value;
loop();
}
} catch (error3) { ifAbruptCloseAsyncIterator(error3); }
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.async-iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
} else {
var value = step.value;
try {
Promise.resolve(filterer(value)).then(function (selected) {
Promise.resolve(filterer(value, state.counter++)).then(function (selected) {
selected ? resolve(createIterResultObject(value, false)) : loop();
}, ifAbruptCloseAsyncIterator);
} catch (error3) { ifAbruptCloseAsyncIterator(error3); }
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.async-iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
} else {
var value = step.value;
try {
Promise.resolve(mapper(value)).then(function (mapped) {
Promise.resolve(mapper(value, state.counter++)).then(function (mapped) {
try {
state.innerIterator = innerIterator = getAsyncIterator(mapped);
state.innerNext = aCallable(innerIterator.next);
Expand Down
1 change: 1 addition & 0 deletions packages/core-js/modules/esnext.async-iterator.indexed.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Remove from `core-js@4`
// https://github.com/tc39/proposal-iterator-helpers
var $ = require('../internals/export');
var indexed = require('../internals/async-iterator-indexed');
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.async-iterator.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
} else {
var value = step.value;
try {
Promise.resolve(mapper(value)).then(function (mapped) {
Promise.resolve(mapper(value, state.counter++)).then(function (mapped) {
resolve(createIterResultObject(mapped, false));
}, ifAbruptCloseAsyncIterator);
} catch (error2) { ifAbruptCloseAsyncIterator(error2); }
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.async-iterator.reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
var next = record.next;
var noInitial = arguments.length < 2;
var accumulator = noInitial ? undefined : arguments[1];
var counter = 0;
aCallable(reducer);

return new Promise(function (resolve, reject) {
Expand All @@ -38,12 +39,13 @@ $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
accumulator = value;
loop();
} else try {
Promise.resolve(reducer(accumulator, value)).then(function (result) {
Promise.resolve(reducer(accumulator, value, counter)).then(function (result) {
accumulator = result;
loop();
}, ifAbruptCloseAsyncIterator);
} catch (error3) { ifAbruptCloseAsyncIterator(error3); }
}
counter++;
} catch (error2) { reject(error2); }
}, reject);
} catch (error) { reject(error); }
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.every.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#every', assert => {
assert.expect(16);
assert.expect(17);
const async = assert.async();
const { every } = AsyncIterator.prototype;

Expand All @@ -17,10 +17,11 @@ QUnit.test('AsyncIterator#every', assert => {
return every.call(createIterator([1, 2, 3]), it => it === 2);
}).then(result => {
assert.false(result, 'basic functionality, -');
return every.call(createIterator([1]), function (arg) {
return every.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
});
}).then(() => {
return every.call(createIterator([1]), () => { throw 42; });
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#filter', assert => {
assert.expect(15);
assert.expect(16);
const async = assert.async();
const { filter } = AsyncIterator.prototype;

Expand All @@ -14,10 +14,11 @@ QUnit.test('AsyncIterator#filter', assert => {

filter.call(createIterator([1, 2, 3]), it => it % 2).toArray().then(it => {
assert.arrayEqual(it, [1, 3], 'basic functionality');
return filter.call(createIterator([1]), function (arg) {
return filter.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
}).toArray();
}).then(() => {
return filter.call(createIterator([1]), () => { throw 42; }).toArray();
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#find', assert => {
assert.expect(16);
assert.expect(17);
const async = assert.async();
const { find } = AsyncIterator.prototype;

Expand All @@ -17,10 +17,11 @@ QUnit.test('AsyncIterator#find', assert => {
return find.call(createIterator([1, 2, 3]), it => it === 4);
}).then(result => {
assert.same(result, undefined, 'basic functionality, -');
return find.call(createIterator([1]), function (arg) {
return find.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
});
}).then(() => {
return find.call(createIterator([1]), () => { throw 42; });
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#flatMap', assert => {
assert.expect(15);
assert.expect(16);
const async = assert.async();
const { flatMap } = AsyncIterator.prototype;

Expand All @@ -14,10 +14,11 @@ QUnit.test('AsyncIterator#flatMap', assert => {

flatMap.call(createIterator([1, [], 2, createIterable([3, 4]), [5, 6], 'ab']), it => typeof it == 'number' ? [-it] : it).toArray().then(it => {
assert.arrayEqual(it, [-1, -2, 3, 4, 5, 6, 'a', 'b'], 'basic functionality');
return flatMap.call(createIterator([1]), function (arg) {
return flatMap.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
return [arg];
}).toArray();
}).then(() => {
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#forEach', assert => {
assert.expect(15);
assert.expect(16);
const async = assert.async();
const { forEach } = AsyncIterator.prototype;

Expand All @@ -16,10 +16,11 @@ QUnit.test('AsyncIterator#forEach', assert => {

forEach.call(createIterator([1, 2, 3]), it => array.push(it)).then(() => {
assert.arrayEqual(array, [1, 2, 3], 'basic functionality');
return forEach.call(createIterator([1]), function (arg) {
return forEach.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
});
}).then(() => {
return forEach.call(createIterator([1]), () => { throw 42; });
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#map', assert => {
assert.expect(15);
assert.expect(16);
const async = assert.async();
const { map } = AsyncIterator.prototype;

Expand All @@ -14,10 +14,11 @@ QUnit.test('AsyncIterator#map', assert => {

map.call(createIterator([1, 2, 3]), it => it ** 2).toArray().then(it => {
assert.arrayEqual(it, [1, 4, 9], 'basic functionality');
return map.call(createIterator([1]), function (arg) {
return map.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
}).toArray();
}).then(() => {
return map.call(createIterator([1]), () => { throw 42; }).toArray();
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#reduce', assert => {
assert.expect(18);
assert.expect(19);
const async = assert.async();
const { reduce } = AsyncIterator.prototype;

Expand All @@ -14,11 +14,12 @@ QUnit.test('AsyncIterator#reduce', assert => {

reduce.call(createIterator([1, 2, 3]), (a, b) => a + b, 1).then(it => {
assert.same(it, 7, 'basic functionality, initial');
return reduce.call(createIterator([2]), function (a, b) {
return reduce.call(createIterator([2]), function (a, b, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arguments.length, 3, 'arguments length');
assert.same(a, 1, 'argument 1');
assert.same(b, 2, 'argument 2');
assert.same(counter, 0, 'counter');
}, 1);
}).then(() => {
return reduce.call(createIterator([1, 2, 3]), (a, b) => a + b);
Expand Down
7 changes: 4 additions & 3 deletions tests/pure/esnext.async-iterator.some.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { STRICT_THIS } from '../helpers/constants';
import AsyncIterator from 'core-js-pure/full/async-iterator';

QUnit.test('AsyncIterator#some', assert => {
assert.expect(16);
assert.expect(17);
const async = assert.async();
const { some } = AsyncIterator.prototype;

Expand All @@ -17,10 +17,11 @@ QUnit.test('AsyncIterator#some', assert => {
return some.call(createIterator([1, 2, 3]), it => it === 4);
}).then(result => {
assert.false(result, 'basic functionality, -');
return some.call(createIterator([1]), function (arg) {
return some.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
});
}).then(() => {
return some.call(createIterator([1]), () => { throw 42; });
Expand Down
7 changes: 4 additions & 3 deletions tests/tests/esnext.async-iterator.every.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#every', assert => {
assert.expect(18);
assert.expect(19);
const async = assert.async();
const { every } = AsyncIterator.prototype;

Expand All @@ -17,10 +17,11 @@ QUnit.test('AsyncIterator#every', assert => {
return every.call(createIterator([1, 2, 3]), it => it === 2);
}).then(result => {
assert.false(result, 'basic functionality, -');
return every.call(createIterator([1]), function (arg) {
return every.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
});
}).then(() => {
return every.call(createIterator([1]), () => { throw 42; });
Expand Down
7 changes: 4 additions & 3 deletions tests/tests/esnext.async-iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#filter', assert => {
assert.expect(17);
assert.expect(18);
const async = assert.async();
const { filter } = AsyncIterator.prototype;

Expand All @@ -14,10 +14,11 @@ QUnit.test('AsyncIterator#filter', assert => {

filter.call(createIterator([1, 2, 3]), it => it % 2).toArray().then(it => {
assert.arrayEqual(it, [1, 3], 'basic functionality');
return filter.call(createIterator([1]), function (arg) {
return filter.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
}).toArray();
}).then(() => {
return filter.call(createIterator([1]), () => { throw 42; }).toArray();
Expand Down
7 changes: 4 additions & 3 deletions tests/tests/esnext.async-iterator.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createIterator } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#find', assert => {
assert.expect(18);
assert.expect(19);
const async = assert.async();
const { find } = AsyncIterator.prototype;

Expand All @@ -17,10 +17,11 @@ QUnit.test('AsyncIterator#find', assert => {
return find.call(createIterator([1, 2, 3]), it => it === 4);
}).then(result => {
assert.same(result, undefined, 'basic functionality, -');
return find.call(createIterator([1]), function (arg) {
return find.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
});
}).then(() => {
return find.call(createIterator([1]), () => { throw 42; });
Expand Down
7 changes: 4 additions & 3 deletions tests/tests/esnext.async-iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createIterator, createIterable } from '../helpers/helpers';
import { STRICT_THIS } from '../helpers/constants';

QUnit.test('AsyncIterator#flatMap', assert => {
assert.expect(17);
assert.expect(18);
const async = assert.async();
const { flatMap } = AsyncIterator.prototype;

Expand All @@ -14,10 +14,11 @@ QUnit.test('AsyncIterator#flatMap', assert => {

flatMap.call(createIterator([1, [], 2, createIterable([3, 4]), [5, 6], 'ab']), it => typeof it == 'number' ? [-it] : it).toArray().then(it => {
assert.arrayEqual(it, [-1, -2, 3, 4, 5, 6, 'a', 'b'], 'basic functionality');
return flatMap.call(createIterator([1]), function (arg) {
return flatMap.call(createIterator([1]), function (arg, counter) {
assert.same(this, STRICT_THIS, 'this');
assert.same(arguments.length, 1, 'arguments length');
assert.same(arguments.length, 2, 'arguments length');
assert.same(arg, 1, 'argument');
assert.same(counter, 0, 'counter');
return [arg];
}).toArray();
}).then(() => {
Expand Down
Loading

0 comments on commit f17389a

Please sign in to comment.