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

Convert Callback to be an enum to support Fn and FnOnce. #1125

Merged
merged 8 commits into from
Apr 25, 2020

Conversation

lukerandall
Copy link
Contributor

Closes #1039

@lukerandall
Copy link
Contributor Author

@jstarry Thanks for your advice. I had tripped wrapping the callaback in an Option, but without the RefCell I obviously couldn't get it to work. One resulting issue now is the build is failing due to clippy warning about the complexity of the type:

error: very complex type used. Consider factoring parts into `type` definitions
  --> yew/src/callback.rs:19:18
   |
19 |     CallbackOnce(Rc<RefCell<Option<Box<dyn FnOnce(IN)>>>>),
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |

I thought I'd solicit some input on how you'd break this up, since it's mostly about what to name the type, and how much of the type to extract. I was thinking something like:

type OptionalCallback<IN> = RefCell<Option<Box<dyn FnOnce(IN)>>>

@jstarry
Copy link
Member

jstarry commented Apr 24, 2020

Nice work!

How about this:

type CallbackOnce<IN> = RefCell<Option<Box<dyn FnOnce(IN)>>>;

Also can you please add this function: Scope::callback_once to make it easy for devs to create one time callbacks?

(ComponentLink is aliased to scope btw)

yew/src/callback.rs Outdated Show resolved Hide resolved
Copy link
Member

@jstarry jstarry left a comment

Choose a reason for hiding this comment

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

Just one small thing. Btw really appreciate the comment cleanup and your attention to detail!

yew/src/callback.rs Outdated Show resolved Hide resolved
@lukerandall
Copy link
Contributor Author

Thanks, I agree Callback::once looks a lot better! Now if only I could remember to run cargo fmt before pushing 🙈

@jstarry jstarry merged commit d799368 into yewstack:master Apr 25, 2020
@lukerandall lukerandall deleted the add-fnonce-callback branch April 25, 2020 14:22
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.

TimeoutService::spawn should take FnOnce callback
2 participants