You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing Cycle.js and am using Most (which is amazing, by the way) to create some higher-order components to help abstract away some of the low-level stream programming for common Cycle.js component behaviors (mapping a props stream, adding to a props stream, etc).
The core pattern of the components me and my coworkers are writing map a props stream to a React element stream, and a common issue that occurs during development is that if some part of the props stream has not yet emitted a value, nothing will render.
My question is if there is a reliable way to know if a stream is active, if it has emitted a value, or if it holds a value so that I can warn my users of the HOCs that (in the event nothing has rendered), it's because one of their streams fails this test. I know a stream created by @most/hold will have a value at stream.source._hold.value if it is holding one, and I'm wondering if there's a property I can rely on when testing non-held streams or any other stream, or if there's a helper or part of the API I can rely on.
I'm thinking of writing something like this:
constassertStreamHasValue=stream=>{setTimeout(_=>{// ...test our stream here in a setTimeout so that it runs after all streams have been synchronously set up// either throw or console.warn if not in process.env.NODE_ENV !== 'production' if test has failed},0)}assertStreamHasValue(props$);
Thanks!
The text was updated successfully, but these errors were encountered:
hi @sunny-g thanks for the question, glad you like using Most
a common issue that occurs during development is that if some part of the props stream has not yet emitted a value, nothing will render.
have you tried using startWith which essentially starts a stream with the provide value and then follows with the events from the stream. Looking at the cycle.js.org docs it seems like a pretty common pattern they use when they need to show a piece of dom from a stream but the stream hasnt started.
i think startWith is probably an optimal solution, you could probably use it to not only render the dom but also provide some sort of message to the user if need based on the incoming value you started the stream with.
let us know if this works out for you, or perhaps you have a different scenario and we can come up with another solution.
First let me make clear that I do already understand the necessity of using startWith to make a stream have an initial value. The impetus for this question however is more about alerting me when I haven't done that, as failing silently can have a number of different causes and letting me know what direction to look in would shorten my time spent debugging.
Simple analogy - it's one thing to imply "this function should return an array, not an object", but another thing to throw an error in development when you haven't returned an array so that you don't fail silently.
Does that make sense?
Alternatively, I could just append a startWith to every props stream I create/is created with an HOC with some reasonable default value, but it seems that startWith emits the "started" value even when the stream before it has a value. E.g.:
varstream=of({a: 1}).startWith({a: 2})// emits { a: 2 } then { a: 1 }, though I'd only want { a: 1 } to be emitted
Summary
I'm writing Cycle.js and am using Most (which is amazing, by the way) to create some higher-order components to help abstract away some of the low-level stream programming for common Cycle.js component behaviors (mapping a props stream, adding to a props stream, etc).
The core pattern of the components me and my coworkers are writing map a props stream to a React element stream, and a common issue that occurs during development is that if some part of the props stream has not yet emitted a value, nothing will render.
My question is if there is a reliable way to know if a stream is active, if it has emitted a value, or if it holds a value so that I can warn my users of the HOCs that (in the event nothing has rendered), it's because one of their streams fails this test. I know a stream created by
@most/hold
will have a value atstream.source._hold.value
if it is holding one, and I'm wondering if there's a property I can rely on when testing non-held streams or any other stream, or if there's a helper or part of the API I can rely on.I'm thinking of writing something like this:
Thanks!
The text was updated successfully, but these errors were encountered: