-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timers: add experimental scheduler api
Adds experimental implementations of the yield and wait APIs being explored at https://github.com/WICG/scheduling-apis. When I asked the WHATWG folks about the possibility of standardizing the [awaitable versions of setTimeout/setImmediate](whatwg/html#7340) that we have implemented in `timers/promises`, they pointed at the work in progress scheduling APIs draft as they direction they'll be going. While there is definitely a few thing in that draft that have questionable utility to Node.js, the yield and wait APIs map cleanly to the setImmediate and setTimeout we already have. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #40909 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
- Loading branch information
1 parent
74557c3
commit c5f9e96
Showing
3 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const { scheduler } = require('timers/promises'); | ||
const { setTimeout } = require('timers'); | ||
const { | ||
strictEqual, | ||
rejects, | ||
} = require('assert'); | ||
|
||
async function testYield() { | ||
await scheduler.yield(); | ||
process.emit('foo'); | ||
} | ||
testYield().then(common.mustCall()); | ||
queueMicrotask(common.mustCall(() => { | ||
process.addListener('foo', common.mustCall()); | ||
})); | ||
|
||
async function testWait() { | ||
let value = 0; | ||
setTimeout(() => value++, 10); | ||
await scheduler.wait(15); | ||
strictEqual(value, 1); | ||
} | ||
|
||
testWait().then(common.mustCall()); | ||
|
||
async function testCancelableWait1() { | ||
const ac = new AbortController(); | ||
const wait = scheduler.wait(1e6, { signal: ac.signal }); | ||
ac.abort(); | ||
await rejects(wait, { | ||
code: 'ABORT_ERR', | ||
message: 'The operation was aborted', | ||
}); | ||
} | ||
|
||
testCancelableWait1().then(common.mustCall()); | ||
|
||
async function testCancelableWait2() { | ||
const wait = scheduler.wait(10000, { signal: AbortSignal.abort() }); | ||
await rejects(wait, { | ||
code: 'ABORT_ERR', | ||
message: 'The operation was aborted', | ||
}); | ||
} | ||
|
||
testCancelableWait2().then(common.mustCall()); |