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

refactor: migrate widget side effects over to use.disposable #235

Merged
merged 2 commits into from
Oct 29, 2024
Merged
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
10 changes: 3 additions & 7 deletions packages/flutter_rearch/lib/src/side_effects/animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ AnimationController _animationController(
}) {
vsync ??= use.singleTickerProvider();

final controller = use.memo(
return use.disposable(
() => AnimationController(
vsync: vsync!,
duration: duration,
Expand All @@ -81,12 +81,8 @@ AnimationController _animationController(
animationBehavior: animationBehavior,
value: initialValue,
),
);
use.effect(() => controller.dispose, [controller]);

controller
(controller) => controller.dispose(),
)
..duration = duration
..reverseDuration = reverseDuration;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering to rewrite it this way:

AnimationController _animationController(
  WidgetSideEffectRegistrar use, {
  Duration? duration,
  Duration? reverseDuration,
  String? debugLabel,
  double initialValue = 0,
  double lowerBound = 0,
  double upperBound = 1,
  TickerProvider? vsync,
  AnimationBehavior animationBehavior = AnimationBehavior.normal,
}) {
  vsync ??= use.singleTickerProvider();

  return use.disposable(
    () => AnimationController(
      vsync: vsync!,
      duration: duration,
      reverseDuration: reverseDuration,
      debugLabel: debugLabel,
      lowerBound: lowerBound,
      upperBound: upperBound,
      animationBehavior: animationBehavior,
      value: initialValue,
    ),
    (controller) => controller.dispose(),
    [duration, reverseDuration, debugLabel, initialValue, lowerBound, upperBound, vsync, animationBehavior]
  );
}

I've seen examples of passing the arguments as dependencies. But in this case, I thought there might be a reason not to pass them as deps. Am I right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i made the decision that it should keep the same controller instance here regardless of whether those params change, which is why I have that string of ..s for duration and reverseDuration at the end.

Normally, you should put any captured variables in the dependencies list, but this case is an exception since we don't want to return a new controller (at least I don't think that would be the desired behavior).


return controller;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ TextEditingController _textEditingController(
WidgetSideEffectRegistrar use, {
String? initialText,
}) {
final controller = use.memo(() => TextEditingController(text: initialText));
use.effect(() => controller.dispose, [controller]);
return controller;
return use.disposable(
() => TextEditingController(text: initialText),
(controller) => controller.dispose(),
);
}
Loading