Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
31d86e60
Commit
31d86e60
authored
Nov 11, 2025
by
Janka Uryga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean up fast-immediate
parent
e2bbf4b3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
107 deletions
+57
-107
fast-set-immediate.external.ts
...next/src/server/app-render/fast-set-immediate.external.ts
+57
-107
No files found.
packages/next/src/server/app-render/fast-set-immediate.external.ts
View file @
31d86e60
...
...
@@ -23,91 +23,57 @@ export function install() {
export
function
runPendingImmediatesAfterCurrentTask
()
{
startCapturingImmediates
()
scheduleWorkAfterTicksAndMicrotasks
()
}
const
scheduleWorkAfterTicksAndMicrotasks
=
()
=>
{
originalNextTick
(()
=>
{
queueMicrotask
(()
=>
{
originalNextTick
(()
=>
{
if
(
pendingNextTicks
>
0
)
{
// We have raw nextTicks. Let those run first.
debug
?.(
`scheduler :: yielding to
${
pendingNextTicks
}
nextTicks`
)
return
scheduleWorkAfterTicksAndMicrotasks
()
}
return
performWork
()
})
function
scheduleWorkAfterTicksAndMicrotasks
()
{
originalNextTick
(()
=>
{
queueMicrotask
(()
=>
{
originalNextTick
(()
=>
{
if
(
pendingNextTicks
>
0
)
{
// We have raw nextTicks. Let those run first.
debug
?.(
`scheduler :: yielding to
${
pendingNextTicks
}
nextTicks`
)
return
scheduleWorkAfterTicksAndMicrotasks
()
}
return
performWork
()
})
})
}
})
}
const
performWork
=
()
=>
{
debug
?.(
`scheduler :: performing work`
)
function
performWork
()
{
debug
?.(
`scheduler :: performing work`
)
// Find the first (if any) queued immediate that wasn't cleared
let
queueItem
:
ActiveQueueItem
|
null
=
null
while
(
queuedImmediates
.
length
)
{
const
maybeQueItem
=
queuedImmediates
.
shift
()
!
if
(
!
maybeQueItem
.
isCleared
)
{
queueItem
=
maybeQueItem
break
}
}
if
(
!
queueItem
)
{
debug
?.(
`scheduler :: no immediates queued, exiting`
)
stopCapturingImmediates
()
return
// Find the first (if any) queued immediate that wasn't cleared
let
queueItem
:
ActiveQueueItem
|
null
=
null
while
(
queuedImmediates
.
length
)
{
const
maybeQueItem
=
queuedImmediates
.
shift
()
!
if
(
!
maybeQueItem
.
isCleared
)
{
queueItem
=
maybeQueItem
break
}
const
{
immediateObject
,
callback
,
args
}
=
queueItem
// note that this is not a real Immediate object, because we're running them in a nextTick
// TODO: we're already in our own tick, do we need another one?
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
)
}
if
(
!
queueItem
)
{
debug
?.(
`scheduler :: no immediates queued, exiting`
)
stopCapturingImmediates
()
return
}
const
scheduleImmediateInNextTickAndContinueLoop
=
(
callback
:
(...
args
:
any
[])
=>
any
,
...
args
:
any
[]
)
=>
{
let
isCleared
=
false
const
{
immediateObject
,
callback
,
args
}
=
queueItem
originalNextTick
(()
=>
{
// schedule the loop again in case there's more immediates after this.
scheduleWorkAfterTicksAndMicrotasks
()
immediateObject
[
INTERNALS
].
queueItem
=
null
clearQueueItem
(
queueItem
)
if
(
isCleared
)
return
callback
.
apply
(
null
,
args
)
})
// schedule the loop again in case there's more immediates after this.
scheduleWorkAfterTicksAndMicrotasks
()
const
handle
=
{
ref
()
{
return
this
},
unref
()
{
return
this
},
hasRef
()
{
return
true
},
_onImmediate
()
{},
[
Symbol
.
dispose
]()
{
isCleared
=
true
},
}
satisfies
NodeJS
.
Immediate
return
handle
// execute the callback.
if
(
args
)
{
callback
.
apply
(
null
,
args
)
}
else
{
callback
()
}
scheduleWorkAfterTicksAndMicrotasks
()
}
function
startCapturingImmediates
()
{
...
...
@@ -132,14 +98,12 @@ type ActiveQueueItem = {
isCleared
:
false
callback
:
(...
args
:
any
[])
=>
any
args
:
any
[]
|
null
hasRef
:
boolean
immediateObject
:
BufferedImmediate
immediateObject
:
NextImmediate
}
type
ClearedQueueItem
=
{
isCleared
:
true
callback
:
null
args
:
null
hasRef
:
null
immediateObject
:
null
}
...
...
@@ -148,7 +112,6 @@ function clearQueueItem(originalQueueItem: QueueItem) {
queueItem
.
isCleared
=
true
queueItem
.
callback
=
null
queueItem
.
args
=
null
queueItem
.
hasRef
=
null
queueItem
.
immediateObject
=
null
}
...
...
@@ -220,13 +183,12 @@ function patchedSetImmediate(): NodeJS.Immediate {
let
args
:
any
[]
|
null
=
arguments
.
length
>
1
?
Array
.
prototype
.
slice
.
call
(
arguments
,
1
)
:
null
const
immediateObject
=
new
Buffered
Immediate
()
const
immediateObject
=
new
Next
Immediate
()
const
queueItem
:
ActiveQueueItem
=
{
isCleared
:
false
,
callback
,
args
,
hasRef
:
true
,
immediateObject
,
}
queuedImmediates
.
push
(
queueItem
)
...
...
@@ -256,9 +218,9 @@ function patchedSetImmediatePromisify<T = void>(
}
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
)
{
signal
.
addEventListener
(
...
...
@@ -279,7 +241,7 @@ const patchedClearImmediate = (
immediateObject
:
NodeJS
.
Immediate
|
undefined
)
=>
{
if
(
immediateObject
&&
INTERNALS
in
immediateObject
)
{
;(
immediateObject
as
Buffered
Immediate
)[
Symbol
.
dispose
]()
;(
immediateObject
as
Next
Immediate
)[
Symbol
.
dispose
]()
}
else
{
originalClearImmediate
(
immediateObject
)
}
...
...
@@ -289,50 +251,41 @@ const patchedClearImmediate = (
const
INTERNALS
:
unique
symbol
=
Symbol
.
for
(
'next.Immediate.internals'
)
type
QueuedImmediateInternals
=
|
{
queueItem
:
ActiveQueueItem
|
null
nativeImmediate
:
null
}
|
{
queueItem
:
null
nativeImmediate
:
NodeJS
.
Immediate
}
type
NextImmediateInternals
=
{
/** Stored to reflect `ref()`/`unref()` calls, but has no effect otherwise */
hasRef
:
boolean
queueItem
:
ActiveQueueItem
|
null
}
/** Makes sure that we're implementing all the public `Immediate` methods */
interface
NativeImmediate
extends
NodeJS
.
Immediate
{}
/** Implements a shim for the native `Immediate` class returned by `setImmediate` */
class
Buffered
Immediate
implements
NativeImmediate
{
[
INTERNALS
]:
Queued
ImmediateInternals
=
{
class
Next
Immediate
implements
NativeImmediate
{
[
INTERNALS
]:
Next
ImmediateInternals
=
{
queueItem
:
null
,
nativeImmediate
:
null
,
hasRef
:
true
,
}
hasRef
()
{
const
internals
=
this
[
INTERNALS
]
if
(
internals
.
queueItem
)
{
return
internals
.
queueItem
.
hasRef
}
else
if
(
internals
.
nativeImmediate
)
{
return
internals
.
nativeImmediate
.
hasRef
()
return
internals
.
hasRef
}
else
{
// if we're no longer queued (cleared or executed), hasRef is always false
return
false
}
}
ref
()
{
const
internals
=
this
[
INTERNALS
]
if
(
internals
.
queueItem
)
{
internals
.
queueItem
.
hasRef
=
true
}
else
if
(
internals
.
nativeImmediate
)
{
internals
.
nativeImmediate
.
ref
()
internals
.
hasRef
=
true
}
return
this
}
unref
()
{
const
internals
=
this
[
INTERNALS
]
if
(
internals
.
queueItem
)
{
internals
.
queueItem
.
hasRef
=
false
}
else
if
(
internals
.
nativeImmediate
)
{
internals
.
nativeImmediate
.
unref
()
internals
.
hasRef
=
false
}
return
this
}
...
...
@@ -348,9 +301,6 @@ class BufferedImmediate implements NativeImmediate {
const
queueItem
=
internals
.
queueItem
internals
.
queueItem
=
null
clearQueueItem
(
queueItem
)
}
else
if
(
internals
.
nativeImmediate
)
{
// If we executed the queue, and we have a native immediate.
originalClearImmediate
(
internals
.
nativeImmediate
)
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment