Skip to content

Commit 24e5d63

Browse files
committed
build ManagedRuntime synchronously if possible
1 parent 4f21075 commit 24e5d63

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

.changeset/four-yaks-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
simplify fiber.await internals

.changeset/gentle-stars-stop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
build ManagedRuntime synchronously if possible

packages/effect/src/internal/fiberRuntime.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -451,22 +451,12 @@ export class FiberRuntime<in out A, in out E = never> extends Effectable.Class<A
451451
get await(): Effect.Effect<Exit.Exit<A, E>> {
452452
return core.async((resume) => {
453453
const cb = (exit: Exit.Exit<A, E>) => resume(core.succeed(exit))
454-
this.tell(
455-
FiberMessage.stateful((fiber, _) => {
456-
if (fiber._exitValue !== null) {
457-
cb(this._exitValue!)
458-
} else {
459-
fiber.addObserver(cb)
460-
}
461-
})
462-
)
463-
return core.sync(() =>
464-
this.tell(
465-
FiberMessage.stateful((fiber, _) => {
466-
fiber.removeObserver(cb)
467-
})
468-
)
469-
)
454+
if (this._exitValue !== null) {
455+
cb(this._exitValue!)
456+
return
457+
}
458+
this.addObserver(cb)
459+
return core.sync(() => this.removeObserver(cb))
470460
}, this.id())
471461
}
472462

packages/effect/src/internal/managedRuntime.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type * as M from "../ManagedRuntime.js"
77
import { pipeArguments } from "../Pipeable.js"
88
import { hasProperty } from "../Predicate.js"
99
import type * as Runtime from "../Runtime.js"
10+
import * as Scheduler from "../Scheduler.js"
1011
import * as Scope from "../Scope.js"
1112
import type { Mutable } from "../Types.js"
1213
import * as core from "./core.js"
@@ -57,8 +58,9 @@ export const make = <R, ER>(
5758
memoMap = memoMap ?? internalLayer.unsafeMakeMemoMap()
5859
const scope = internalRuntime.unsafeRunSyncEffect(fiberRuntime.scopeMake())
5960
let buildFiber: Fiber.RuntimeFiber<Runtime.Runtime<R>, ER> | undefined
60-
const runtimeEffect = core.withFiberRuntime<Runtime.Runtime<R>, ER>((fiber) => {
61+
const runtimeEffect = core.suspend(() => {
6162
if (!buildFiber) {
63+
const scheduler = new Scheduler.SyncScheduler()
6264
buildFiber = internalRuntime.unsafeForkEffect(
6365
core.tap(
6466
Scope.extend(
@@ -69,8 +71,9 @@ export const make = <R, ER>(
6971
self.cachedRuntime = rt
7072
}
7173
),
72-
{ scope, scheduler: fiber.currentScheduler }
74+
{ scope, scheduler }
7375
)
76+
scheduler.flush()
7477
}
7578
return core.flatten(buildFiber.await)
7679
})

packages/effect/test/ManagedRuntime.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,10 @@ describe("ManagedRuntime", () => {
7676
const result = Context.get(runtime.context, tag)
7777
strictEqual(result, "test")
7878
})
79+
80+
it("is built synchronously with runFork", () => {
81+
const runtime = ManagedRuntime.make(Layer.empty)
82+
runtime.runFork(Effect.void)
83+
runtime.runSync(Effect.void)
84+
})
7985
})

0 commit comments

Comments
 (0)