Skip to content

Commit

Permalink
Add useDisposable hook (#392)
Browse files Browse the repository at this point in the history
* Add `useDisposable` hook

* Replace keys

* Remove explicit type
  • Loading branch information
n-bernat authored Dec 11, 2024
1 parent dc85e1e commit 0c86030
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/leancode_hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased
# 0.1.0

- **Breaking**: Replace all `keys` that were declared as [optional positional parameter](https://dart.dev/language/functions#optional-positional-parameters) with a [named parameter](https://dart.dev/language/functions#named-parameters) that defaults to `const List<Object?>[]`. Affected hooks: `useBloc`, `usePostFrameEffect`, `useTapGestureRecognizer`.
- Add `useDisposable` hook.
- Bump `leancode_lint` dev dependency to `12.0.0`.
- Bump `custom_lint` dev dependency to `0.6.4`.

Expand Down
1 change: 1 addition & 0 deletions packages/leancode_hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ so you won't have to depend on it.
- [useBlocState](lib/src/use_bloc_state.dart)
- [useDebounce](lib/src/use_debounce.dart)
- [useDeclarativeTextEditingController](lib/src/use_decarative_text_editing_controller.dart)
- [useDisposable](lib/src/use_disposable.dart)
- [useFocused](lib/src/use_focused.dart)
- [usePostFrameEffect](lib/src/use_post_frame_effect.dart)
- [useStreamListener](lib/src/use_stream_listener.dart)
Expand Down
1 change: 1 addition & 0 deletions packages/leancode_hooks/lib/leancode_hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export 'src/use_bloc_listener.dart';
export 'src/use_bloc_state.dart';
export 'src/use_debounce.dart';
export 'src/use_decarative_text_editing_controller.dart';
export 'src/use_disposable.dart';
export 'src/use_focused.dart';
export 'src/use_post_frame_effect.dart';
export 'src/use_stream_listener.dart';
Expand Down
19 changes: 19 additions & 0 deletions packages/leancode_hooks/lib/src/use_disposable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

/// Creates and memoizes an instance created using [builder].
/// On unmount, executes [dispose] in order to cleanup resources.
T useDisposable<T>({
required ValueGetter<T> builder,
required void Function(T) dispose,
List<Object?> keys = const [],
}) {
final value = useMemoized(builder, keys);

useEffect(
() => () => dispose(value),
keys,
);

return value;
}
2 changes: 1 addition & 1 deletion packages/leancode_hooks/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: leancode_hooks
version: 0.0.6
version: 0.1.0
homepage: https://github.com/leancodepl/flutter_corelibrary/tree/master/packages/leancode_hooks
repository: https://github.com/leancodepl/flutter_corelibrary
description: >-
Expand Down

0 comments on commit 0c86030

Please sign in to comment.