Skip to content

Commit

Permalink
replace asap by queueMicrotask
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 24, 2018
1 parent 3e608c5 commit 13d5fe7
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,11 @@ module.exports = {
es6: true,
},
globals: {
asap: true,
compositeKey: true,
compositeSymbol: true,
globalThis: true,
Observable: true,
queueMicrotask: true,
},
},
],
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
- Fix descriptors of global properties.
- In the version without global pollution, if `Object#toString` does not support `@@toStringTag`, add to wrapped prototypes own `toString` method with `@@toStringTag` logic, see [#199](https://github.com/zloirock/core-js/issues/199).
- Update standard features and proposals:
- Remove mongolian vowel separator (U+180E) from the list of whitespaces for methods like `String#trim` (ES6 -> ES7)
- `asap` (old stage 0 proposal) replaced by `queueMicrotask` ([a part of HTML spec](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask))
- Update [`Observable`](https://github.com/tc39/proposal-observable) (#257, #276, etc.)
- Update `Array#flatten` -> `Array#flat` and `Array#flatMap` and move to the stage 3
- Update `global` [stage 3 proposal](https://github.com/tc39/proposal-global) - rename `global` to `globalThis`
- Update `String#matchAll` ([proposal-string-matchall#17](https://github.com/tc39/proposal-string-matchall/pull/17), [proposal-string-matchall#38](https://github.com/tc39/proposal-string-matchall/pull/38), etc.) and move to the stage 3
- Update `.name` properties of `String#{trimStart, trimEnd , trimLeft, trimRight}`, move to the stage 3
- Remove mongolian vowel separator (U+180E) from the list of whitespaces for methods like `String#trim` (ES6 -> ES7)
- Mark ES2016, ES2017 and ES2018 features as stable:
- `Array#includes` and `%TypedArray%#includes`
- `Object.values` and `Object.entries`
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Promise.resolve(32).then(x => console.log(x)); // => 32
- [Web standards](#web-standards)
- [setTimeout / setInterval](#settimeout--setinterval)
- [setImmediate](#setimmediate)
- [queueMicrotask](#queuemicrotask)
- [iterable DOM collections](#iterable-dom-collections)
- [Iteration helpers](#iteration-helpers)
- [Missing polyfills](#missing-polyfills)
Expand Down Expand Up @@ -1971,19 +1972,6 @@ core-js(-pure)/features/math/isubh
core-js(-pure)/features/math/imulh
core-js(-pure)/features/math/umulh
```
* `global.asap`, [TC39 discussion](https://github.com/rwaldron/tc39-notes/blob/master/es/2014-09/sept-25.md#510-globalasap-for-enqueuing-a-microtask), module [`esnext.asap`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.asap.js)
```js
function asap(fn: Function): void;
```
[*CommonJS entry points:*](#commonjs)
```js
core-js/proposals/asap
core-js(-pure)/features/asap
```
[*Examples*](http://goo.gl/tx3SRK):
```js
asap(() => console.log('called as microtask'));
```

#### Pre-stage 0 proposals
[*CommonJS entry points:*](#commonjs)
Expand Down Expand Up @@ -2071,6 +2059,21 @@ clearImmediate(setImmediate(() => {
console.log('Message will not be displayed');
}));
```
#### queueMicrotask
[Spec](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask), module [`web.queue-microtask`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.queue-microtask.js)
```js
function queueMicrotask(fn: Function): void;
```
[*CommonJS entry points:*](#commonjs)
```js
core-js/web/queue-microtask
core-js(-pure)/features/queue-microtask
```
[*Examples*](https://goo.gl/nsW8P9):
```js
queueMicrotask(() => console.log('called as microtask'));
```

#### Iterable DOM collections
Some DOM collections should have [iterable interface](https://heycam.github.io/webidl/#idl-iterable) or should be [inherited from `Array`](https://heycam.github.io/webidl/#LegacyArrayClass). That means they should have `forEach`, `keys`, `values`, `entries` and `@@iterator` methods for iteration. So add them. Modules [`web.dom-collections.iterator`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.dom-collections.iterator.js) and [`web.dom-collections.for-each`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.dom-collections.for-each.js).
```js
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js-builder/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ module.exports = {
'esnext.array.flat-map',
'esnext.array.last-index',
'esnext.array.last-item',
'esnext.asap',
'esnext.composite-key',
'esnext.composite-symbol',
'esnext.global-this',
Expand Down Expand Up @@ -278,6 +277,7 @@ module.exports = {
'web.dom-collections.for-each',
'web.dom-collections.iterator',
'web.immediate',
'web.queue-microtask',
'web.timers',
],
/* eslint-disable prefer-template */
Expand Down
3 changes: 0 additions & 3 deletions packages/core-js/features/asap.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/core-js/features/queue-microtask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('../modules/web.queue-microtask');

module.exports = require('../internals/path').queueMicrotask;
2 changes: 1 addition & 1 deletion packages/core-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ require('./modules/esnext.array.flat');
require('./modules/esnext.array.flat-map');
require('./modules/esnext.array.last-index');
require('./modules/esnext.array.last-item');
require('./modules/esnext.asap');
require('./modules/esnext.composite-key');
require('./modules/esnext.composite-symbol');
require('./modules/esnext.global-this');
Expand Down Expand Up @@ -275,5 +274,6 @@ require('./modules/esnext.weak-set.of');
require('./modules/web.dom-collections.for-each');
require('./modules/web.dom-collections.iterator');
require('./modules/web.immediate');
require('./modules/web.queue-microtask');
require('./modules/web.timers');
module.exports = require('./internals/path');
24 changes: 13 additions & 11 deletions packages/core-js/internals/microtask.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var macrotask = require('../internals/task').set;
var Observer = global.MutationObserver || global.WebKitMutationObserver;
var process = global.process;
var Promise = global.Promise;
var queueMicrotask = global.queueMicrotask;
var isNode = classof(process) == 'process';

module.exports = function () {
var head, last, notify;
var flush, head, last, notify;

var flush = function () {
// modern engines have queueMicrotask method
if (!queueMicrotask) {
flush = function () {
var parent, fn;
if (isNode && (parent = process.domain)) parent.exit();
while (head) {
Expand Down Expand Up @@ -58,13 +60,13 @@ module.exports = function () {
macrotask.call(global, flush);
};
}
}

return function (fn) {
var task = { fn: fn, next: undefined };
if (last) last.next = task;
if (!head) {
head = task;
notify();
} last = task;
};
module.exports = queueMicrotask || function (fn) {
var task = { fn: fn, next: undefined };
if (last) last.next = task;
if (!head) {
head = task;
notify();
} last = task;
};
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var iterate = require('../internals/iterate');
var checkCorrectnessOfIteration = require('../internals/check-correctness-of-iteration');
var speciesConstructor = require('../internals/species-constructor');
var task = require('../internals/task').set;
var microtask = require('../internals/microtask')();
var microtask = require('../internals/microtask');
var promiseResolve = require('../internals/promise-resolve');
var hostReportErrors = require('../internals/host-report-errors');
var newPromiseCapabilityModule = require('../internals/new-promise-capability');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// `asap` method
// https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-09/sept-25.md#510-globalasap-for-enqueuing-a-microtask
var microtask = require('../internals/microtask')();
// `queueMicrotask` method
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask
var microtask = require('../internals/microtask');
var process = require('../internals/global').process;
var isNode = require('../internals/classof-raw')(process) == 'process';

require('../internals/export')({ global: true }, {
asap: function asap(fn) {
queueMicrotask: function queueMicrotask(fn) {
var domain = isNode && process.domain;
microtask(domain ? domain.bind(fn) : fn);
}
Expand Down
1 change: 0 additions & 1 deletion packages/core-js/proposals/asap.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/core-js/stage/0.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('../proposals/asap');
require('../proposals/efficient-64-bit-arithmetic');
require('../proposals/string-at');

Expand Down
1 change: 1 addition & 0 deletions packages/core-js/web/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('../modules/web.dom-collections.for-each');
require('../modules/web.dom-collections.iterator');
require('../modules/web.immediate');
require('../modules/web.queue-microtask');
require('../modules/web.timers');

module.exports = require('../internals/path');
3 changes: 3 additions & 0 deletions packages/core-js/web/queue-microtask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('../modules/web.queue-microtask');

module.exports = require('../internals/path').queueMicrotask;
4 changes: 2 additions & 2 deletions tests/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok(typeof load('features/set-interval') === 'function');
ok(typeof load('features/set-immediate') === 'function');
ok(typeof load('features/clear-immediate') === 'function');
ok(typeof load('features/asap') === 'function');
ok(typeof load('features/queue-microtask') === 'function');
ok(typeof load('features/composite-key')({}, 1, {}) === 'object');
ok(typeof load('features/composite-symbol')({}, 1, {}) === 'symbol');
ok(load('features/is-iterable')([]));
Expand Down Expand Up @@ -588,11 +588,11 @@ for (const _PATH of ['../packages/core-js-pure', '../packages/core-js']) {
ok('Map' in load('es'));
ok('setTimeout' in load('web/timers'));
ok('setImmediate' in load('web/immediate'));
ok(typeof load('web/queue-microtask') === 'function');
load('web/dom-collections');
ok('setImmediate' in load('web'));
load('proposals/array-flat-and-flat-map');
load('proposals/array-last');
load('proposals/asap');
load('proposals/collection-methods');
load('proposals/collection-of-from');
load('proposals/efficient-64-bit-arithmetic');
Expand Down
2 changes: 1 addition & 1 deletion tests/pure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ import './es.weak-set';
QUnit.module('ESNext');
import './esnext.array.flat-map';
import './esnext.array.flat';
import './esnext.asap';
import './esnext.composite-key';
import './esnext.composite-symbol';
import './esnext.global-this';
Expand Down Expand Up @@ -213,6 +212,7 @@ import './esnext.weak-set.of';
QUnit.module('Web');
import './web.dom-collections.iterator';
import './web.immediate';
import './web.queue-microtask';
import './web.timers';

QUnit.module('Helpers');
Expand Down
10 changes: 5 additions & 5 deletions tests/pure/esnext.asap.js → tests/pure/web.queue-microtask.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asap from 'core-js-pure/features/asap';
import queueMicrotask from 'core-js-pure/features/queue-microtask';

QUnit.test('asap', assert => {
QUnit.test('queueMicrotask', assert => {
assert.expect(3);
assert.isFunction(asap);
assert.arity(asap, 1);
assert.isFunction(queueMicrotask);
assert.arity(queueMicrotask, 1);
const async = assert.async();
let done = false;
let after = false;
asap(() => {
queueMicrotask(() => {
if (!done) {
done = true;
assert.ok(after, 'works');
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ import './esnext.array.last-item';
import './esnext.array.last-index';
import './esnext.array.flat-map';
import './esnext.array.flat';
import './esnext.asap';
import './esnext.composite-key';
import './esnext.composite-symbol';
import './esnext.global-this';
Expand Down Expand Up @@ -266,4 +265,5 @@ QUnit.module('Web');
import './web.dom-collections.for-each';
import './web.dom-collections.iterator';
import './web.immediate';
import './web.queue-microtask';
import './web.timers';
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
QUnit.test('asap', assert => {
QUnit.test('queueMicrotask', assert => {
assert.expect(5);
assert.isFunction(asap);
assert.arity(asap, 1);
assert.name(asap, 'asap');
assert.looksNative(asap);
assert.isFunction(queueMicrotask);
assert.arity(queueMicrotask, 1);
assert.name(queueMicrotask, 'queueMicrotask');
assert.looksNative(queueMicrotask);
const async = assert.async();
let done = false;
let after = false;
asap(() => {
queueMicrotask(() => {
if (!done) {
done = true;
assert.ok(after, 'works');
Expand Down

0 comments on commit 13d5fe7

Please sign in to comment.