mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-07 04:05:37 +08:00
Reset a replaced scheduler's dispatcher in one place in setContext
Assign the reconciled cache in the comparison in runLoop, and reset the dispatcher before the parking check in setContext. Saves 7 gzip bytes in the basic bundle; the replacement test now reads the dispatcher while the fiber is suspended, so the reset in runLoop stays covered.
This commit is contained in:
@@ -691,9 +691,7 @@ export class FiberImpl<A = any, E = any> implements Fiber.Fiber<A, E> {
|
||||
runLoop(effect: Primitive): Exit.Exit<A, E> | Yield {
|
||||
// Take the scheduler setContext gave the fiber while it was suspended, or
|
||||
// while async_hooks entered it
|
||||
const latest = (this.context as any).cacheRoot._fiberCache
|
||||
if (this.cache !== latest) {
|
||||
this.cache = latest
|
||||
if (this.cache !== (this.cache = (this.context as any).cacheRoot._fiberCache)) {
|
||||
this._dispatcher = undefined
|
||||
}
|
||||
const prevFiber = (globalThis as any)[currentFiberTypeId]
|
||||
@@ -825,13 +823,13 @@ export class FiberImpl<A = any, E = any> implements Fiber.Fiber<A, E> {
|
||||
const cache: Fiber.Fiber.Cache = root._fiberCache ??= makeFiberContextCache(context)
|
||||
const scheduler = this.cache?.scheduler
|
||||
if (scheduler !== cache.scheduler) {
|
||||
this._dispatcher = undefined
|
||||
// A ParkingScheduler may hold the effect a suspended fiber runs with
|
||||
// next, so the fiber keeps it until it runs
|
||||
if (scheduler && !this._running && !this._exit && Scheduler.isParkingScheduler(scheduler)) {
|
||||
this.cache = { ...cache, scheduler }
|
||||
return
|
||||
}
|
||||
this._dispatcher = undefined
|
||||
}
|
||||
this.cache = cache
|
||||
}
|
||||
|
||||
@@ -291,11 +291,13 @@ describe("Scheduler", () => {
|
||||
{ scheduler }
|
||||
)
|
||||
const context = Context.add(fiber.context, Scheduler.Scheduler, other)
|
||||
const dispatcher = fiber.currentDispatcher
|
||||
let dispatcher: Scheduler.SchedulerDispatcher | undefined
|
||||
const replace = () => {
|
||||
onEnter = undefined
|
||||
fiber.setContext(context)
|
||||
assert.strictEqual(fiber.context, context)
|
||||
// Read before the fiber runs, so the run has to replace it
|
||||
dispatcher = fiber.currentDispatcher
|
||||
}
|
||||
if (entering) onEnter = replace
|
||||
else replace()
|
||||
|
||||
Reference in New Issue
Block a user