-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: Add Readonly-Streams (at least in Typings) #212
Comments
I think I understand what you want. You want the types to be something like: flyd.stream(v: T): WritableStream<T>
type Stream<T> = WritableStream<T> | ReadonlyStream<T>
map<T,V>(fn: (v: T)=> V) => (s: Stream<T>) => ReadonlyStream<V> Did you run into a problem putting values into dependent streams where these types would've helped? What was the situation? |
No, it is not a technical issue. Currently I created my own ReadonlyStream interface by copying the Stream interface and removing the unwanted methods (the setters and end stream)
and I am doing an explicit cast like:
But some methods have problems with that interface and then I need to do a cast back to flyd.Stream. On the other hand, I am not sure, if ReadonlyStreams are a wanted feature in general, or if it is only a habit of mine. Cheers and thanks for the quick response. PS: Maybe I can rework the typings in such a way and contribute it back to the project? I realy love flyd and use it as my state layer (as redux replacement) together with preact. That looks like this:
|
Hey again!
Absolutely! Feel free to submit a PR with reworked/better typings
It's less of a use case and more of "that's how it works internally." I have however used it in one instance where data could come from multiple sources. I.e. a user entity had a feature flags object, but I also had a live subscription to feature flag updates const user$ = stream()
const featureFlags$ = user$.map(prop('featureFlags'))
fetch('/user').then(x => x.json()).then(user$)
subscribeToFeatureFlagUpdates(featureFlags$)
I would definitely keep the end stream on there. There are cases where you'd want to end a dependent stream separately from its parent. const x$ = stream(3)
const xx$ = x$.map(x => x * x)
const xxx$ = xx$.map(xx => xx * xx)
// does not end the parent stream, but does end the xxx$ stream
xx$.end(true)
Nice! That's exactly how I started using it. albeit with mithril as the view layer. check out @foxdonut 's meiosis for inspiration on stream based state management https://meiosis.js.org/ |
Currently there is no possibility to export a stream as readonly, to prevent the updating of a combined, piped or mapped stream.
At least some typescript typings could enhance the proposed usage api doc for such an exported stream.
For example:
The text was updated successfully, but these errors were encountered: