rrousselGit / flutter_hooks

React hooks for Flutter. Hooks are a new kind of object that manages a Widget life-cycles. They are used to increase code sharing between widgets and as a complete replacement for StatefulWidget.
MIT License
3.07k stars 175 forks source link

Add useStreamSubscription hook #373

Closed jezsung closed 11 months ago

jezsung commented 11 months ago

Adds a new hook useStreamSubscription that allows you to subscribe to a Stream and register callback handlers such as onData, onError, and onDone.

Returns the StreamSubscription returned by the Stream.listen.

CAUTION Awaiting on the returned StreamSubscription.cancel never ends on a test. Not sure if this is somehow expected.

https://github.com/jezsung/flutter_hooks/blob/53ed52bf3605952f2eb790d8c57b2831684b2479/packages/flutter_hooks/test/use_stream_subscription_test.dart#L207-L209

codecov[bot] commented 11 months ago

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (4f70845) 99.87% compared to head (4e58095) 99.87%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #373 +/- ## ======================================= Coverage 99.87% 99.87% ======================================= Files 18 18 Lines 775 804 +29 ======================================= + Hits 774 803 +29 Misses 1 1 ``` | [Impacted Files](https://app.codecov.io/gh/rrousselGit/flutter_hooks/pull/373?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet) | Coverage Δ | | |---|---|---| | [packages/flutter\_hooks/lib/src/async.dart](https://app.codecov.io/gh/rrousselGit/flutter_hooks/pull/373?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Remi+Rousselet#diff-cGFja2FnZXMvZmx1dHRlcl9ob29rcy9saWIvc3JjL2FzeW5jLmRhcnQ=) | `100.00% <100.00%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

rrousselGit commented 11 months ago

Hello! What's the difference with the useStreamListener PR?

I don't like the naming on this hook. Generally such hooks would be named useOnStreamChange

rrousselGit commented 11 months ago

If the goal is to return a StreamSubscription to cancel the subscription early, that's fine. Although other hooks don't really do this.

Could you add this to the README?

rrousselGit commented 11 months ago

Could you add 100% coverage too?

jezsung commented 11 months ago

@rrousselGit I made a few changes based on your feedback.

I tried to keep non trailing comma consistent but some tests with a long description get ugly if the trailing comma gets removed. I think keeping them look clean and readable should be prioritized so I didn't remove them all. What do you think?

jezsung commented 11 months ago

Just another thought on this, how about using a callable class to allow syntax like this:

final StreamSubscription subscription = useStream.listen(
  onData: (data) => {},
);

I saw the useTextEditingController is using a callable class to make this syntax possible and it seems really cool.

rrousselGit commented 11 months ago

LGTM, thanks!