Skip to content
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

Question: assert that a stream is active/has emitted/holds a value? #436

Open
sunny-g opened this issue May 8, 2017 · 2 comments
Open

Comments

@sunny-g
Copy link

sunny-g commented May 8, 2017

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 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:

const assertStreamHasValue = 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!

@davidchase
Copy link
Collaborator

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.

@sunny-g
Copy link
Author

sunny-g commented May 12, 2017

Thanks for your reply!

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.:

var stream = of({ a: 1 }).startWith({ a: 2 })
// emits { a: 2 } then { a: 1 }, though I'd only want { a: 1 } to be emitted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants