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(protocol): respond with a function call instead of return value #1017

Merged
merged 22 commits into from
Sep 6, 2023

Conversation

lucasfernog
Copy link
Member

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Docs
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change?

  • Yes
  • No

Checklist

  • This PR will resolve #___
  • A change file is added if any packages will require a version bump due to this PR per the instructions in the readme.
  • I have added a convincing reason for adding this feature, if necessary
  • It can be built on all targets and pass CI/CD.

Other information

This enhances the custom protocol handler to allow working asynchronously instead of freezing the main thread for longer tasks. I wanted to do this for a long time but now with Tauri using custom protocols for IPC, we actually need this (my biggest issue right now is using the camera on mobile, it was freezing the app).

@lucasfernog lucasfernog requested a review from a team as a code owner September 5, 2023 19:24
amrbashir
amrbashir previously approved these changes Sep 5, 2023
Copy link
Member

@amrbashir amrbashir left a comment

Choose a reason for hiding this comment

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

LGTM! but I am wondering if we can keep the same DX as before, e.g. allow either responding early or just return the response from the closure, for example:

.with_custom_protocol("wry".into(), move |request, api| {
  // do work
  api.respond(response);
  // do morework
})

and

.with_custom_protocol("wry".into(), move |request, api| {
  // do work
  Ok(response)
})

@lucasfernog
Copy link
Member Author

That would be more confusing.. you can only respond once, so if we wanna keep the return value, we'd need to make it an Option where None means you're using the api parameter (yikes).

@amrbashir
Copy link
Member

Right, that would be confusing, how about two separate APIs then? maybe with_custom_protocol_responder?

@lucasfernog
Copy link
Member Author

Good idea! Let me know what you think.

src/webview/mod.rs Outdated Show resolved Hide resolved
src/webview/mod.rs Outdated Show resolved Hide resolved
@@ -135,7 +153,7 @@ pub struct WebViewAttributes {
/// [bug]: https://bugs.webkit.org/show_bug.cgi?id=229034
pub custom_protocols: Vec<(
String,
Box<dyn Fn(&Request<Vec<u8>>) -> Result<Response<Cow<'static, [u8]>>>>,
Box<dyn Fn(Request<Vec<u8>>, Response) -> Result<()>>,
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't make sense to return Result here if you want to be async. Response should be the one to determine the Result.

Copy link
Member Author

Choose a reason for hiding this comment

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

pushed @wusyong

Copy link
Member

Choose a reason for hiding this comment

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

I mean the implementation should also handle this.
If it's responder dealing with it, it should decide which finish method should be called.

Copy link
Member

Choose a reason for hiding this comment

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

We don't necessarily need to add .respond_with_err() variant, since you can just create a response with status code between 400-599 and you should be fine. However, I do agree that this function shouldn't return a result to allow the responder to respond even if this function errored and the responder were sent somewhere else.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah at least it's less confusing for the user where you can call respond() and return an error later and it would crash the app.

Copy link
Member Author

Choose a reason for hiding this comment

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

about manually creating error responses: that's what I'm trying to convince @wusyong :D

src/webview/mod.rs Outdated Show resolved Hide resolved
examples/custom_protocol.rs Outdated Show resolved Hide resolved
@@ -135,7 +153,7 @@ pub struct WebViewAttributes {
/// [bug]: https://bugs.webkit.org/show_bug.cgi?id=229034
pub custom_protocols: Vec<(
String,
Box<dyn Fn(&Request<Vec<u8>>) -> Result<Response<Cow<'static, [u8]>>>>,
Box<dyn Fn(Request<Vec<u8>>, RequestAsyncResponder) -> Result<()>>,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Box<dyn Fn(Request<Vec<u8>>, RequestAsyncResponder) -> Result<()>>,
Box<dyn Fn(Request<Vec<u8>>, RequestAsyncResponder)>,

Since the decision is removing Result, let's change it and remove the underlying function it calls.

Copy link
Member

@amrbashir amrbashir Sep 6, 2023

Choose a reason for hiding this comment

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

we need to add custom_protocols_async field here and keep the existing one as is to avoid a breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

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

let me know if it looks good now @wusyong

@lucasfernog lucasfernog merged commit 4bdf1c3 into dev Sep 6, 2023
7 checks passed
@lucasfernog lucasfernog deleted the refactor/custom-protocol-response branch September 6, 2023 13:50
@github-actions github-actions bot mentioned this pull request Sep 6, 2023
wusyong pushed a commit that referenced this pull request Sep 6, 2023
…lue (#1017)

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
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.

None yet

3 participants