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

Feature Request - Async Javascript Binding Add support for returning Task #2758

Closed
perf2711 opened this issue May 6, 2019 · 4 comments · Fixed by #2771
Closed

Feature Request - Async Javascript Binding Add support for returning Task #2758

perf2711 opened this issue May 6, 2019 · 4 comments · Fixed by #2771

Comments

@perf2711
Copy link

perf2711 commented May 6, 2019

  • What version of the product are you using?
    73.1.130

  • What architecture x86 or x64?
    System architecture - x64, problem exists on both program architectures

  • On what operating system?
    Win10

  • Are you using WinForms, WPF or OffScreen?
    WPF

  • What steps will reproduce the problem?
    Bind a simple object, which has an async method. Run the method from Javascript.
    https://gist.github.com/perf2711/e96e69abad24c254d0192e88eb705709
    I hosted the index.html file through python3 -m http.server 8000.

  • What is the expected output? What do you see instead?

Synchronous methods work as they should, as well waiting for result from an asynchronous method in synchronous context. When running an async method, nothing is returned, promise never resolves and the object "locks up", preventing any additional calls.

This code was run inside the developer console.

>await TestBoundObject.syncMethod()
"SyncMethod"
>await TestBoundObject.syncMethod1()
"AsyncMethod1"
>await TestBoundObject.syncMethod2()
"AsyncMethod2"
>await TestBoundObject.syncMethod3()
"AsyncMethod3"
>await TestBoundObject.asyncMethod1()
>await TestBoundObject.asyncMethod2()
>await TestBoundObject.asyncMethod3()
>await TestBoundObject.syncMethod()
  • Please provide any additional information below.
    No exceptions are thrown. Breakpoints in each function are triggered.
    I don't see any information regarding not using async methods in bound objects in General usage, however, there are no examples of such case.

  • Does this problem also occur in the CEF Sample Application from http://opensource.spotify.com/cefbuilds/index.html?
    No, at least not in the Javascript Binding test. Anyway I think this issue cannot be reproduced in the sample app.

@amaitland
Copy link
Member

There are no examples as it's not currently support, the name is slightly misleading in this instance. Only the JavaScript side of the equation is actually asynchronous. It's difficult to implement this feature, you are welcome to submit a pull request.

The current supported method of executing long running tasks is to use a callback see https://github.com/cefsharp/CefSharp/blob/cefsharp/73/CefSharp.Example/JavascriptBinding/BoundObject.cs#L58

@amaitland amaitland changed the title Running async methods on bound objects does not return any result, and locks up Feature Request - Async Javascript Binding Add support for returning Task May 7, 2019
@amaitland
Copy link
Member

Had a quick look and it occurred to me that the CefSharpSettings.ConcurrentTaskExecution feature could be rewritten to support this without drastic changes.

A very rough implementation is at amaitland@a9f20ac

No where near production ready.

@perf2711
Copy link
Author

perf2711 commented May 7, 2019

Right, I thought it was supported, and I did something wrong.

However, it's odd that everything locks up when an async method is invoked. Shouldn't there be any warning/exception when it occurs?

@amaitland
Copy link
Member

amaitland commented Jun 8, 2019

Support has been added in #2771 There are some QUnit test cases, please submit a PR with any failing cases that need to be addressed.

You need to set the follow before creating your ChromiumWebBrowser instances. If you attempt to return a Task without enabling the following then you should get a detailed error message.

THIS FEATURE IS ONLY AVAILABLE WHEN REGISTERING AN OBJECT AS ASYNC e.g. isAsync: true

CefSharpSettings.ConcurrentTaskExecution = true;

Example

public async Task<string[]> AsyncDownloadFileAndSplitOnNewLines(string url)
{
	var webClient = new WebClient();
	var download = await webClient.DownloadStringTaskAsync(new Uri(url));

	var lines = download.Split('\n').Where(x => !string.IsNullOrEmpty(x.Trim())).ToArray();

	return lines;
}
const url = "https://raw.githubusercontent.com/cefsharp/CefSharp/master/.editorconfig";

boundAsync.asyncDownloadFileAndSplitOnNewLines(url).then(function (lines)
{
	//Do something with the result.
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants