Effect CommunityEC
Effect Community3y ago
7 replies
Ken

Probably weird question about effect and stream

This may sound strange, but I am trying to make a repository that will work both on b-end and f-end. On the f-end I want to use zustand as an implementation.

Thanks to this project: https://github.com/zio/zio-insight-ui
I salvaged a way how to befriend stream with react through Effect. forkDaemon and custom hook.

But the concept that I am working with is a little bit complex.

For example, what if I have something like this:
interface ProductRepository {
  save: (product: Product): Effect<never, DuplicateProductError, void>,
  lookup: (id: ProductId):  Stream<never, never, Product>
} 

But later on I want to have following desired behavior:

I have a custom hook, that runs effects in runtime and returns me either result, or an error

const {isRunning,invoke, error, result} = useRuntimeEffect(/* my effect producer here */);


And I need something like this for streams/data that can change over time with an error but to be recoverable from an error.

An example (Pseudocode):

const {error, result} = useStream(() => store.lookup(id), [id]);
return <div>{ error ? error : result }</div>


Where we will receive an error when a product does not exist buy id, but later on, if the state changes it will re-render to the result.

I already thought about something like this:
Effect<never, ProductNotFound, Stream<never,never, Product>>


But this does not make a lot of sense for these reasons:
1) If Effect fails, I won't receive a stream.
2) I still have to consume the last value of a stream and deal somehow with Option.

I thought about encoding an error in the stream itself:
Stream<never, ProductNotFound, Product>> but I will still have those issues

I have a rough guess that what I want is achievable with fibers, but these are unknown field to me and maybe someone can point me to good material about them or propose me a better abstraction
GitHub
Contribute to zio/zio-insight-ui development by creating an account on GitHub.
GitHub - zio/zio-insight-ui
Was this page helpful?