Skip to content

Commit

Permalink
avoid observable side effects of %Array.prototype.values% usage in …
Browse files Browse the repository at this point in the history
…array-like branch of `Array.fromAsync`

tc39/proposal-array-from-async#30
  • Loading branch information
zloirock committed Oct 6, 2022
1 parent 684ab08 commit 54c7f11
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Avoiding observable side effects of `%Array.prototype.values%` usage in array-like branch of `Array.fromAsync`, [proposal-array-from-async/30](https://github.com/tc39/proposal-array-from-async/pull/30)
- Added `inverse` option to `core-js-compat`, [#1119](https://github.com/zloirock/core-js/issues/1119)
- Added `format` option to `core-js-builder`, [#1120](https://github.com/zloirock/core-js/issues/1120)

Expand Down
18 changes: 16 additions & 2 deletions packages/core-js/internals/array-from-async.js
@@ -1,5 +1,6 @@
'use strict';
var bind = require('../internals/function-bind-context');
var uncurryThis = require('../internals/function-uncurry-this');
var toObject = require('../internals/to-object');
var isConstructor = require('../internals/is-constructor');
var getAsyncIterator = require('../internals/get-async-iterator');
Expand All @@ -13,7 +14,20 @@ var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator');
var toArray = require('../internals/async-iterator-iteration').toArray;

var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator');
var arrayIterator = getVirtual('Array').values;
var arrayIterator = uncurryThis(getVirtual('Array').values);
var arrayIteratorNext = uncurryThis(arrayIterator([]).next);

var safeArrayIterator = function () {
return new SafeArrayIterator(this);
};

var SafeArrayIterator = function (O) {
this.iterator = arrayIterator(O);
};

SafeArrayIterator.prototype.next = function () {
return arrayIteratorNext(this.iterator);
};

// `Array.fromAsync` method implementation
// https://github.com/tc39/proposal-array-from-async
Expand All @@ -26,7 +40,7 @@ module.exports = function fromAsync(asyncItems /* , mapfn = undefined, thisArg =
var O = toObject(asyncItems);
if (mapfn !== undefined) mapfn = bind(mapfn, thisArg);
var usingAsyncIterator = getMethod(O, ASYNC_ITERATOR);
var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || arrayIterator;
var usingSyncIterator = usingAsyncIterator ? undefined : getIteratorMethod(O) || safeArrayIterator;
var A = isConstructor(C) ? new C() : [];
var iterator = usingAsyncIterator
? getAsyncIterator(O, usingAsyncIterator)
Expand Down

0 comments on commit 54c7f11

Please sign in to comment.