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

Don't marshal Task<[]> of supported colletion items #138

Open
elringus opened this issue Jan 13, 2024 · 0 comments
Open

Don't marshal Task<[]> of supported colletion items #138

elringus opened this issue Jan 13, 2024 · 0 comments
Labels
enhancement New feature or enhancement external The issue is caused by a third-party

Comments

@elringus
Copy link
Owner

elringus commented Jan 13, 2024

.NET's JS interop currently is only able to marshal types with single-level nesting:

— hence we have to handle otherwise natively-supported arrays (eg, Task<byte[]>) in a special manner.

Workaround 1

Lift the array to object and marshal as JSType.Any:

[JSExport] [return: JSMarshalAs<JSType.Promise<JSType.Any>>]
private static async Task<object> GetArrayAsync ()
{
    await Task.Delay(1);
    return new[] { "foo", "bar", "baz" };
}

This is currently employed under the hood and doesn't require any action from end-user, but costs an extra allocation when unmarshalling from JS to C#.

Workaround 2

Wrap the call into two: first to wait for the async operation, second to get the array in a blocking manner, eg:

[JSFunction] Task OpenFile (string uri);
[JSFunction] byte[] GetOpenFileContent (string uri);

Task<byte[]> ReadFile (string uri)
{
    await OpenFile(uri);
    return GetOpenFileContent(uri);
}
@elringus elringus added the enhancement New feature or enhancement label Jan 13, 2024
@elringus elringus added this to the First stable release milestone Jan 13, 2024
@elringus elringus added help wanted Community help is most appreciated external The issue is caused by a third-party labels Jan 15, 2024
@elringus elringus removed the help wanted Community help is most appreciated label Feb 14, 2024
@elringus elringus changed the title Don't serialize Task<[]> of supported colletion items Don't marshal Task<[]> of supported colletion items May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement external The issue is caused by a third-party
Projects
None yet
Development

No branches or pull requests

1 participant