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

Add the useOnListenableChange hook #390

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ They will take care of creating/updating/disposing an object.
| [useListenableSelector](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useListenableSelector.html) | Similar to `useListenable`, but allows filtering UI rebuilds |
| [useValueNotifier](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useValueNotifier.html) | Creates a `ValueNotifier` which will be automatically disposed. |
| [useValueListenable](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useValueListenable.html) | Subscribes to a `ValueListenable` and return its value. |
| [useOnListenableChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnListenableChange.html) | Subscribes to a `Listenable` and registers `listener` to the passed `listenable`. |

#### Misc hooks:

Expand Down
59 changes: 59 additions & 0 deletions packages/flutter_hooks/lib/src/listenable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,62 @@ class _UseValueNotifierHookState<T>
@override
String get debugLabel => 'useValueNotifier';
}

/// Subscribes to a [Listenable] and registers `listener` to the passed `listenable`.
///
/// See also:
/// * [useListenable]
/// * [useEffect]
void useOnListenableChange(VoidCallback listener, Listenable listenable) {
use(_OnListenableChangeHook(listener, listenable));
}

class _OnListenableChangeHook extends Hook<void> {
_OnListenableChangeHook(this.listener, this.listenable);

final VoidCallback listener;
final Listenable listenable;

@override
_OnListenableChangeHookState createState() => _OnListenableChangeHookState();
}

class _OnListenableChangeHookState
extends HookState<void, _OnListenableChangeHook> {
Dispose? disposer;

@override
void initHook() {
super.initHook();
scheduleEffect();
}

@override
void didUpdateHook(_OnListenableChangeHook oldHook) {
super.didUpdateHook(oldHook);

if (hook.listenable != oldHook.listenable) {
disposer?.call();
scheduleEffect();
}
}

@override
void build(BuildContext context) {}

@override
void dispose() => disposer?.call();

void scheduleEffect() {
void listener() => hook.listener();

hook.listenable.addListener(listener);
disposer = () => hook.listenable.removeListener(listener);
}

@override
String get debugLabel => 'useOnListenableChange';

@override
bool get debugSkipValue => true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO