Commit 31d86e60 authored by Janka Uryga's avatar Janka Uryga

clean up fast-immediate

parent e2bbf4b3
...@@ -23,91 +23,57 @@ export function install() { ...@@ -23,91 +23,57 @@ export function install() {
export function runPendingImmediatesAfterCurrentTask() { export function runPendingImmediatesAfterCurrentTask() {
startCapturingImmediates() startCapturingImmediates()
scheduleWorkAfterTicksAndMicrotasks()
}
const scheduleWorkAfterTicksAndMicrotasks = () => { function scheduleWorkAfterTicksAndMicrotasks() {
originalNextTick(() => { originalNextTick(() => {
queueMicrotask(() => { queueMicrotask(() => {
originalNextTick(() => { originalNextTick(() => {
if (pendingNextTicks > 0) { if (pendingNextTicks > 0) {
// We have raw nextTicks. Let those run first. // We have raw nextTicks. Let those run first.
debug?.(`scheduler :: yielding to ${pendingNextTicks} nextTicks`) debug?.(`scheduler :: yielding to ${pendingNextTicks} nextTicks`)
return scheduleWorkAfterTicksAndMicrotasks() return scheduleWorkAfterTicksAndMicrotasks()
} }
return performWork() return performWork()
})
}) })
}) })
} })
}
const performWork = () => { function performWork() {
debug?.(`scheduler :: performing work`) debug?.(`scheduler :: performing work`)
// Find the first (if any) queued immediate that wasn't cleared // Find the first (if any) queued immediate that wasn't cleared
let queueItem: ActiveQueueItem | null = null let queueItem: ActiveQueueItem | null = null
while (queuedImmediates.length) { while (queuedImmediates.length) {
const maybeQueItem = queuedImmediates.shift()! const maybeQueItem = queuedImmediates.shift()!
if (!maybeQueItem.isCleared) { if (!maybeQueItem.isCleared) {
queueItem = maybeQueItem queueItem = maybeQueItem
break break
}
}
if (!queueItem) {
debug?.(`scheduler :: no immediates queued, exiting`)
stopCapturingImmediates()
return
} }
}
const { immediateObject, callback, args } = queueItem if (!queueItem) {
debug?.(`scheduler :: no immediates queued, exiting`)
// note that this is not a real Immediate object, because we're running them in a nextTick stopCapturingImmediates()
// TODO: we're already in our own tick, do we need another one? return
const handle = args
? scheduleImmediateInNextTickAndContinueLoop(callback, ...args)
: scheduleImmediateInNextTickAndContinueLoop(callback)
// Now that we're no longer buffering the immediate,
// make the BufferedImmediate proxy calls to the native object instead
immediateObject[INTERNALS].queueItem = null
immediateObject[INTERNALS].nativeImmediate = handle
clearQueueItem(queueItem)
} }
const scheduleImmediateInNextTickAndContinueLoop = ( const { immediateObject, callback, args } = queueItem
callback: (...args: any[]) => any,
...args: any[]
) => {
let isCleared = false
originalNextTick(() => { immediateObject[INTERNALS].queueItem = null
// schedule the loop again in case there's more immediates after this. clearQueueItem(queueItem)
scheduleWorkAfterTicksAndMicrotasks()
if (isCleared) return // schedule the loop again in case there's more immediates after this.
callback.apply(null, args) scheduleWorkAfterTicksAndMicrotasks()
})
const handle = { // execute the callback.
ref() { if (args) {
return this callback.apply(null, args)
}, } else {
unref() { callback()
return this
},
hasRef() {
return true
},
_onImmediate() {},
[Symbol.dispose]() {
isCleared = true
},
} satisfies NodeJS.Immediate
return handle
} }
scheduleWorkAfterTicksAndMicrotasks()
} }
function startCapturingImmediates() { function startCapturingImmediates() {
...@@ -132,14 +98,12 @@ type ActiveQueueItem = { ...@@ -132,14 +98,12 @@ type ActiveQueueItem = {
isCleared: false isCleared: false
callback: (...args: any[]) => any callback: (...args: any[]) => any
args: any[] | null args: any[] | null
hasRef: boolean immediateObject: NextImmediate
immediateObject: BufferedImmediate
} }
type ClearedQueueItem = { type ClearedQueueItem = {
isCleared: true isCleared: true
callback: null callback: null
args: null args: null
hasRef: null
immediateObject: null immediateObject: null
} }
...@@ -148,7 +112,6 @@ function clearQueueItem(originalQueueItem: QueueItem) { ...@@ -148,7 +112,6 @@ function clearQueueItem(originalQueueItem: QueueItem) {
queueItem.isCleared = true queueItem.isCleared = true
queueItem.callback = null queueItem.callback = null
queueItem.args = null queueItem.args = null
queueItem.hasRef = null
queueItem.immediateObject = null queueItem.immediateObject = null
} }
...@@ -220,13 +183,12 @@ function patchedSetImmediate(): NodeJS.Immediate { ...@@ -220,13 +183,12 @@ function patchedSetImmediate(): NodeJS.Immediate {
let args: any[] | null = let args: any[] | null =
arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : null arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : null
const immediateObject = new BufferedImmediate() const immediateObject = new NextImmediate()
const queueItem: ActiveQueueItem = { const queueItem: ActiveQueueItem = {
isCleared: false, isCleared: false,
callback, callback,
args, args,
hasRef: true,
immediateObject, immediateObject,
} }
queuedImmediates.push(queueItem) queuedImmediates.push(queueItem)
...@@ -256,9 +218,9 @@ function patchedSetImmediatePromisify<T = void>( ...@@ -256,9 +218,9 @@ function patchedSetImmediatePromisify<T = void>(
} }
const immediate = patchedSetImmediate(resolve, value) const immediate = patchedSetImmediate(resolve, value)
if (options?.ref === false) {
immediate.unref() // Note that we're ignoring `options.ref`, because `unref()` has no effect
} // on our patched immediates
if (signal) { if (signal) {
signal.addEventListener( signal.addEventListener(
...@@ -279,7 +241,7 @@ const patchedClearImmediate = ( ...@@ -279,7 +241,7 @@ const patchedClearImmediate = (
immediateObject: NodeJS.Immediate | undefined immediateObject: NodeJS.Immediate | undefined
) => { ) => {
if (immediateObject && INTERNALS in immediateObject) { if (immediateObject && INTERNALS in immediateObject) {
;(immediateObject as BufferedImmediate)[Symbol.dispose]() ;(immediateObject as NextImmediate)[Symbol.dispose]()
} else { } else {
originalClearImmediate(immediateObject) originalClearImmediate(immediateObject)
} }
...@@ -289,50 +251,41 @@ const patchedClearImmediate = ( ...@@ -289,50 +251,41 @@ const patchedClearImmediate = (
const INTERNALS: unique symbol = Symbol.for('next.Immediate.internals') const INTERNALS: unique symbol = Symbol.for('next.Immediate.internals')
type QueuedImmediateInternals = type NextImmediateInternals = {
| { /** Stored to reflect `ref()`/`unref()` calls, but has no effect otherwise */
queueItem: ActiveQueueItem | null hasRef: boolean
nativeImmediate: null queueItem: ActiveQueueItem | null
} }
| {
queueItem: null
nativeImmediate: NodeJS.Immediate
}
/** Makes sure that we're implementing all the public `Immediate` methods */ /** Makes sure that we're implementing all the public `Immediate` methods */
interface NativeImmediate extends NodeJS.Immediate {} interface NativeImmediate extends NodeJS.Immediate {}
/** Implements a shim for the native `Immediate` class returned by `setImmediate` */ /** Implements a shim for the native `Immediate` class returned by `setImmediate` */
class BufferedImmediate implements NativeImmediate { class NextImmediate implements NativeImmediate {
[INTERNALS]: QueuedImmediateInternals = { [INTERNALS]: NextImmediateInternals = {
queueItem: null, queueItem: null,
nativeImmediate: null, hasRef: true,
} }
hasRef() { hasRef() {
const internals = this[INTERNALS] const internals = this[INTERNALS]
if (internals.queueItem) { if (internals.queueItem) {
return internals.queueItem.hasRef return internals.hasRef
} else if (internals.nativeImmediate) {
return internals.nativeImmediate.hasRef()
} else { } else {
// if we're no longer queued (cleared or executed), hasRef is always false
return false return false
} }
} }
ref() { ref() {
const internals = this[INTERNALS] const internals = this[INTERNALS]
if (internals.queueItem) { if (internals.queueItem) {
internals.queueItem.hasRef = true internals.hasRef = true
} else if (internals.nativeImmediate) {
internals.nativeImmediate.ref()
} }
return this return this
} }
unref() { unref() {
const internals = this[INTERNALS] const internals = this[INTERNALS]
if (internals.queueItem) { if (internals.queueItem) {
internals.queueItem.hasRef = false internals.hasRef = false
} else if (internals.nativeImmediate) {
internals.nativeImmediate.unref()
} }
return this return this
} }
...@@ -348,9 +301,6 @@ class BufferedImmediate implements NativeImmediate { ...@@ -348,9 +301,6 @@ class BufferedImmediate implements NativeImmediate {
const queueItem = internals.queueItem const queueItem = internals.queueItem
internals.queueItem = null internals.queueItem = null
clearQueueItem(queueItem) clearQueueItem(queueItem)
} else if (internals.nativeImmediate) {
// If we executed the queue, and we have a native immediate.
originalClearImmediate(internals.nativeImmediate)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment