Hello folks. As I convert more and more parts of my code into Effect, the confusion around interoperability is increasing. Particularly about long-lived Effect programs and it's interactions with third party code that may also be long lived, but not for the entire life of the application.
The things that I think need a long-lived effect program are: - Caching and batching of requests using requests resolvers - Repetitive schedules, for example, repeat a task every minute - Effects that subscribe to events coming from third party code like Svelte stores
The doubts that I have around it are: - How I am supposed to run them? I don't have any particular runtime requirements, so up until now I just have been using
Effect.runPromise(program)
Effect.runPromise(program)
. Does this promise need to be awaited in order to run? - How can I properly teardown those programs ? For example, when the Svelte component gets destroyed. Someone suggested that maybe just providing an abort signal to
runPromise
runPromise
may work, so I am doing this. Please see the piece of code attached and let me know if it makes sense
const me = writable(null as Me | null);function init() { const run = Effect.gen(function* (_) { const meData = yield* fetchMe; if (meData !== undefined) me.set(meData); }); const signal = new AbortController(); Effect.runPromise(run, { signal: signal.signal }); return () => ({ destroy: () => signal.abort(); });}export const appContext = { me, init }
const me = writable(null as Me | null);function init() { const run = Effect.gen(function* (_) { const meData = yield* fetchMe; if (meData !== undefined) me.set(meData); }); const signal = new AbortController(); Effect.runPromise(run, { signal: signal.signal }); return () => ({ destroy: () => signal.abort(); });}export const appContext = { me, init }