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

Conversation

m-sadegh-sh
Copy link
Contributor

No description provided.


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).

Copy link
Owner

@GregoryConrad GregoryConrad left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this PR; these couple side effects were written before I made use.disposable.


controller
(controller) => controller.dispose(),
)
..duration = duration
..reverseDuration = reverseDuration;
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 use.disposable(
() => TextEditingController(text: initialText),
(controller) => controller.dispose(),
[initialText],
Copy link
Owner

Choose a reason for hiding this comment

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

The controller should not be recreated if initialText changes, so would you mind removing the [initialText], line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Owner

Choose a reason for hiding this comment

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

TY

@GregoryConrad GregoryConrad changed the title Use the shorter .disposable syntax refactor: migrate widget side effects over to use.disposable Oct 29, 2024
@GregoryConrad GregoryConrad enabled auto-merge (squash) October 29, 2024 13:06
@GregoryConrad GregoryConrad merged commit 04f54cc into GregoryConrad:main Oct 29, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants