Dev.to WebDev ๐Ÿ›  Dev ๐Ÿ‘ 0 ๐Ÿ“– 9 min read

About eager-init services in Effect.ts

The laziest article I've ever written. It still makes complete sense. Used AI to translate from the native language where the conversation happened into English. The translation is fully validated. The format is inspired

The laziest article I've ever written. It still makes complete sense. Used AI to translate from the native language where the conversation happened into English. The translation is fully validated. The format is inspired by Ink and Switch's article called "Artificial".

Evangeline Rome:
and even with Deferred you can do it way fucking cooler
lazy services
so they don't block application start
https://github.com/nikelborm/effect-garden/blob/da25498ba088addfaedd0227ae2fc8eac4895ec4/apps/next-midi-demo/app/services/DeferredMIDIAccessService.ts#L32

https://github.com/nikelborm/effect-garden/blob/da25498ba088addfaedd0227ae2fc8eac4895ec4/apps/next-midi-demo/app/helpers/toStatusStream.ts#L7
honestly I could've easily generalized this wrapper with the lazy service, and I even have a half-started version of it somewhere in my playground
but I was too lazy to finish it
this approach has a potential downside
namely that errors from creating the layer are no longer tracked in the layer itself, and every individual consumer of this implementation will see in the type system how it failed during initialization
so you'd have to handle the same error in lots of places and potentially decide individually, every time, what to do with it, instead of doing it once at the layer level
look at the second file
this approach also has an additional upside that's just as underrated
https://github.com/nikelborm/effect-garden/blob/da25498ba088addfaedd0227ae2fc8eac4895ec4/apps/next-midi-demo/app/services/DeferredAudioContextService.ts
specifically at this:

currentTime: Effect.flatMap(
  deferredAudioContext,
  EAudioContext.currentTime,
),

decodeAudioData: (encodedAudioBuffer: ArrayBuffer) =>
  Effect.flatMap(
    deferredAudioContext,
    EAudioContext.decodeAudioData(encodedAudioBuffer),
  ),

createPlayback,

the methods get forwarded transparently
so if the implementation hasn't been built yet they just hang waiting
and if it has, the methods get called on the implementation immediately
AND
in regular layers
different implementations can fail with different errors
and those get tracked in the layer's types
this approach only works fine when there's basically one implementation overall
because you'll have to force all the errors into one implementation (one service shape/interface), or wrap them in some GeneralAudioContextAcquireFailure { cause: unknown } that every method of the service will carry
like, you've got just one tag
and the service type is hardcoded in it, including the whole list of errors every method of the service implementation can return
same shit they were going on about with "don't leak dependencies in implementation"
so here we're leaking them, on purpose, and not just them
[forwarded from another chat, thought you folks would find this pattern interesting too]

replying to Evangeline Rome:
https://github.com/nikelborm/effect-garden/blob/da25498ba088addf...

btw "lazy" isn't quite the right word for them here. they don't start building at the moment someone requests them. they're more non-blocking, or eager to return. they start building immediately at app start, but since using them is optional and triggered by later user actions, there's no point blocking the whole app, and waiting for the build to finish can be pushed off until the exact moment they're actually needed

replying to Evangeline Rome:
https://github.com/nikelborm/effect-garden/blob/da25498ba088addf...

on the frontend, naively shoving things into layers like this gives a pretty mediocre result
because the moment you get even ONE layer that takes some time to run, that's it. we're fucked. the whole app freezes on the frontend and the user can't click anything because the app's whole layer hasn't finished building

Friend A:
but then doesn't a popup show up so the user has to click somewhere?

Evangeline Rome:
in my case, if I don't use this approach, the permission request to MIDI devices, which hangs the whole time until the user grants it, will freeze the entire app and all interactions
creating an audio context takes 200 ms
creating a MIDI context takes anywhere from 200 to infinity
creating the root OPFS directory that everybody wants to use via DI
same thing

Friend A:
maybe launch them in parallel? or is it synchronous?

Evangeline Rome:
I'm launching everything in parallel
it's all async
but what's rendered on my buttons is streams
and all user inputs are also read as reactive streams
the whole app, I've got a lot of web APIs that depend on and interact with each other
and to make a button interactive I shouldn't have to wait for the webmidi system to initialize
to make a button interactive I need to wait for the status from OPFS to find out whether we've downloaded the audio file or not
and until all the layers are built, the runtime won't start executing the main effect they're provided to
so if even one layer isn't eager, it'll freeze the whole app and all interactions and kill responsiveness
and unfortunately the fairy tale that goes "just make small separate AtomRuntimes" doesn't help here
because all the onClicks, all the streaming of audio assets, everything lives in an effect and is rendered from the cached filesystem state and the playback-queue state machine
and they depend on each other

Friend A:
I feel like the service availability problem fundamentally can't be solved
but maybe you could somehow reconfigure the Scheduler for the browser?

Evangeline Rome:
I haven't dug down to those depths myself
something else is needed here, I think
conceptually I get the feeling that the whole two-stage Layer system doesn't fit
that context is one simple object built once
that yield* Tag isn't a reactive subscription to wait for the moment a service gets built
so:

Friend A:

replying to Evangeline Rome:
that yield* Tag isn't a reactive subscription to...

yeah, here you could actually use Effectable โ€” so it waits on deferred.await

replying to Friend A:
but maybe you could somehow reconfigure the Scheduler for...

and it's also region-scoped, like pretty much everything, btw
so apparently you can customize fiber subtrees
but I haven't worked with it much
only remember you can configure yieldCount, I think, and schedule fiberinos

Evangeline Rome:

replying to Evangeline Rome:
so:

Effect.gen(function*() {
  const impl1 = yield* Tag1SecToInit
  const return1 = yield* impl1.1secLongCall()

  const impl2 = yield* Tag2SecToInit
  yield* impl2.instantCall(return1)
}).pipe(
  Effect.Provide(AppLayer)
)

so ideally it'd be awesome to have something like this in my case. and that's why I'm suffering with all these Deferreds. I want out-of-the-box tooling for this.
as soon as one second passes: Tag1SecToInit is built and impl1 is resolved
as soon as the next second passes: impl1.1secLongCall finishes executing and Tag2SecToInit is built and impl2 is resolved and the instantCall method finishes instantly
done in 2 seconds

while with the current setup, with layers by default
it's first two seconds of waiting to build both of them in parallel, and then one more to wait for the blocking 1secLongCall in the middle of the main effect

three in total

replying to Friend A:
but maybe you could somehow reconfigure the Scheduler for...

I talked to an LLM, didn't see any use for it in my use case. didn't figure out myself either how it could be useful. if someone can explain to me how different scheduling could help within that last code snippet, I'd be glad to listen

replying to Friend A:
yeah, here you could actually use Effectable so it waits on...

oh, that might actually be a good idea btw

a code example wouldn't hurt if you've got the energy and time right now

Friend A:
no, the scheduler is specifically about operations โ€” you've got a bunch of operations and you can schedule them, pause, prioritize, switch between fibers (yield execution)

Evangeline Rome:

replying to Friend A:
I feel like the service availability problem fundamentally can't be...

explain what you mean by service availability?

Friend A:

replying to Evangeline Rome:
explain what you mean by service availability?

you launch their construction at startup so they build in parallel with the app starting up
the app is built and the hot code asks for services โ€” and they haven't finished yet โ€” is that your case?

Evangeline Rome:
my brain is just dragging backend and SRE terms in here, like availability, and I'm kinda stuck

is that your case?

well yeah, but that's not the whole case, just part of it

replying to Friend A:
yeah, here you could actually use Effectable so it waits on...

so I thought
and it won't help though, will it

Friend A:
I'm sitting here looking at it too)

Evangeline Rome:
like
it won't even get to that point
until the layer is built
even Layer.map is useless

Friend A:
no, well, you have to build the layer the way you did it

Evangeline Rome:
yeah, that's needed
right into the very implementation
right where the layer is getting built
hooking in
because otherwise everything everywhere will wait for that layer's build
or you know what
I think I've got it
make a HOF that maps layers

<A, E, R>(layer) => Layer.effect(Effect.gen(function*(){
  const deferred = yield* Deferred.make<Context<AddContextInitErrs<A, E>>>()
  yield* Layer.build(layer).pipe(
    Deferred.into(deferred),
    Effect.forkScoped
  )
  return new Proxy(whatever....)
}))

this is the thing, btw
and maybe even way fucking cooler than the shit I was fucking around with in the playground I mentioned earlier
or maybe it's complementary
gotta keep playing with it

Good person B:
damn, look at you guys go! ๐Ÿค“
never get tired of admiring this

Evangeline Rome:

replying to Evangeline Rome:
make a HOF that maps layers <A, E,...

but here too it looks like you can catch a shitload of trouble. I don't yet know how to properly weld everything else in there, like an OTel span for building the layer itself, how to attach scopes correctly, how to route nested dependencies

replying to Evangeline Rome:
make a HOF that maps layers <A, E,...

that's just pseudocode I banged out in the chat just now

Friend A:
why the proxy?
maybe just put a Deferred into the context
and when reading, for example, do yield* MyTag.background โ€” resolve from the Deferred there
and if it's plain yield* MyTag โ€” you get a Deferred<MyService, Err>

Evangeline Rome:

replying to Friend A:
why the proxy?

so I don't have to write this crap out by hand
https://t.me/fp_ts/13688
you can generate it. either on individual values of the context container (the service implementations), or on all of them at once
to create on the fly those very effects that people will await
to push it even further away, until the ACTUAL use of the methods
because again
why freeze
if you're only using one method in some handler that's called on some event, like a click on a particular button

replying to Evangeline Rome:
so I don't have to write this crap by hand https://t.me/fp_ts/13688...

so like class static accessors, just in a different situation

for class static accessors you can use a proxy too

Friend A:
gotcha, maybe it can be split into two tasks

Evangeline Rome:
well, maybe
it's just that in my use case I'll only be using one anyway

replying to Evangeline Rome:
make a HOF that maps layers...

in general yeah
I originally meant putting the proxy in a bit deeper
imagine like

return Context.mapSome(
  ([tag, impl]) => tag == specific
    ? Option.some(new Proxy(impl, ...))
    : Option.none()
)

aaah
fuck
yeah
well, with Deferred that gets a bit more complicated
but that's the principle, yeah
it'll be a complex proxy
but totally doable

replying to Evangeline Rome:
specifically at this: currentTime:...

the plan is to immediately create such a magical context object that, when its keys get materialized, hands out values that are again proxies, which on each individual method add this kind of mapping with the Deferred
complications might start with how it plays with composition with other layers, like through Layer.map or Layer.merge
gotta play with this seriously

๐Ÿ“ฐ Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.