Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69e419a

Browse filesBrowse files
authored
Add useOnStreamChange hook (#373)
1 parent 4f70845 commit 69e419a
Copy full SHA for 69e419a

File tree

Expand file treeCollapse file tree

3 files changed

+389
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+389
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ They will take care of creating/updating/disposing an object.
314314
| ---------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
315315
| [useStream](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useStream.html) | Subscribes to a `Stream` and returns its current state as an `AsyncSnapshot`. |
316316
| [useStreamController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useStreamController.html) | Creates a `StreamController` which will automatically be disposed. |
317+
| [useOnStreamChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnStreamChange.html) | Subscribes to a `Stream`, registers handlers, and returns the `StreamSubscription`.
317318
| [useFuture](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useFuture.html) | Subscribes to a `Future` and returns its current state as an `AsyncSnapshot`. |
318319

319320
#### Animation related hooks:

‎packages/flutter_hooks/lib/src/async.dart

Copy file name to clipboardExpand all lines: packages/flutter_hooks/lib/src/async.dart
+95Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,98 @@ class _StreamControllerHookState<T>
320320
@override
321321
String get debugLabel => 'useStreamController';
322322
}
323+
324+
/// Subscribes to a [Stream] and calls the [Stream.listen] to register the [onData],
325+
/// [onError], and [onDone].
326+
///
327+
/// Returns the [StreamSubscription] returned by the [Stream.listen].
328+
///
329+
/// See also:
330+
/// * [Stream], the object listened.
331+
/// * [Stream.listen], calls the provided handlers.
332+
StreamSubscription<T>? useOnStreamChange<T>(
333+
Stream<T>? stream, {
334+
void Function(T event)? onData,
335+
void Function(Object error, StackTrace stackTrace)? onError,
336+
void Function()? onDone,
337+
bool? cancelOnError,
338+
}) {
339+
return use<StreamSubscription<T>?>(
340+
_OnStreamChangeHook<T>(
341+
stream,
342+
onData: onData,
343+
onError: onError,
344+
onDone: onDone,
345+
cancelOnError: cancelOnError,
346+
),
347+
);
348+
}
349+
350+
class _OnStreamChangeHook<T> extends Hook<StreamSubscription<T>?> {
351+
const _OnStreamChangeHook(
352+
this.stream, {
353+
this.onData,
354+
this.onError,
355+
this.onDone,
356+
this.cancelOnError,
357+
});
358+
359+
final Stream<T>? stream;
360+
final void Function(T event)? onData;
361+
final void Function(Object error, StackTrace stackTrace)? onError;
362+
final void Function()? onDone;
363+
final bool? cancelOnError;
364+
365+
@override
366+
_StreamListenerHookState<T> createState() => _StreamListenerHookState<T>();
367+
}
368+
369+
class _StreamListenerHookState<T>
370+
extends HookState<StreamSubscription<T>?, _OnStreamChangeHook<T>> {
371+
StreamSubscription<T>? _subscription;
372+
373+
@override
374+
void initHook() {
375+
super.initHook();
376+
_subscribe();
377+
}
378+
379+
@override
380+
void didUpdateHook(_OnStreamChangeHook<T> oldWidget) {
381+
super.didUpdateHook(oldWidget);
382+
if (oldWidget.stream != hook.stream ||
383+
oldWidget.cancelOnError != hook.cancelOnError) {
384+
_unsubscribe();
385+
_subscribe();
386+
}
387+
}
388+
389+
@override
390+
void dispose() {
391+
_unsubscribe();
392+
}
393+
394+
void _subscribe() {
395+
final stream = hook.stream;
396+
if (stream != null) {
397+
_subscription = stream.listen(
398+
hook.onData,
399+
onError: hook.onError,
400+
onDone: hook.onDone,
401+
cancelOnError: hook.cancelOnError,
402+
);
403+
}
404+
}
405+
406+
void _unsubscribe() {
407+
_subscription?.cancel();
408+
}
409+
410+
@override
411+
StreamSubscription<T>? build(BuildContext context) {
412+
return _subscription;
413+
}
414+
415+
@override
416+
String get debugLabel => 'useOnStreamChange';
417+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.