Effect CommunityEC
Effect Community4w ago
3 replies
harrysolovay

Persisting an RPC Client Instance Across Effect Calls

I'm building a browser SDK that opens/communicates with an RPC server within a web worker (RpcClient.layerProtocolWorker).

The worker holds onto some state and I don't want it to be destroyed/recreated between RPC calls.

This means I need to instantiate and keep the RPC client alive / avoid its finalizers from closing the port.

Is there a good way to do something like the following, wherein MyRpcClient exists outside of the context of the Effect / subsequent calls of someLibraryMethod access that same MyRpcClient instance?

const someLibraryMethod = () => Effect.gen(function*() {
  const sameClientInstanceBetweenCalls = yield* MyRpcClient
  yield* sameClientInstanceBetweenCalls.someMethod()
}).pipe(Effect.runPromise)


Perhaps something similar to what we see from Atom.keepAlive in @effect-atom/atom-react?

const MyRpcClientAlive = MyRpcClient.pipe(Effect.keepAlive)

// use as normal, but now it's persisted between Effect executions
yield* MyRpcClientAlive
Was this page helpful?