MCPcopy Create free account
hub / github.com/Effect-TS/effect / withRun

Function withRun

packages/effect/src/unstable/rpc/Utils.ts:25–58  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23 * @since 4.0.0
24 */
25export const withRun = <
26 A extends {
27 readonly run: (f: (...args: Array<any>) => Effect.Effect<void>) => Effect.Effect<never>
28 }
29>() =>
30<EX, RX>(f: (write: Parameters<A["run"]>[0]) => Effect.Effect<Omit<A, "run">, EX, RX>): Effect.Effect<A, EX, RX> =>
31 Effect.suspend(() => {
32 const semaphore = Semaphore.makeUnsafe(1)
33 let buffer: Array<[Array<any>, Context.Context<never>]> = []
34 let write = (...args: Array<any>): Effect.Effect<void> =>
35 Effect.contextWith((context) => {
36 buffer.push([args, context])
37 return Effect.void
38 })
39 return Effect.map(f((...args) => write(...args)), (a) => ({
40 ...a,
41 run(f) {
42 return semaphore.withPermits(1)(Effect.gen(function*() {
43 const prev = write
44 write = f
45
46 for (const [args, context] of buffer) {
47 yield* Effect.provideContext(Effect.suspend(() => f(...args)), context)
48 }
49 buffer = []
50
51 return yield* Effect.onExit(Effect.never, () => {
52 write = prev
53 return Effect.void
54 })
55 }))
56 }
57 } as A))
58 })
59
60/**
61 * Builds an RPC client protocol service that tracks active client IDs and

Callers 1

ProtocolClass · 0.90

Calls 3

writeFunction · 0.70
fFunction · 0.50
mapMethod · 0.45

Tested by

no test coverage detected