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: synchronous communication with dart from javascript #67

Merged
merged 2 commits into from
Nov 11, 2021

Conversation

Tiagoperes
Copy link
Contributor

@Tiagoperes Tiagoperes commented Nov 7, 2021

Related issues

#53

Description

The javascript code calls the Dart code via the function sendMessage. Dart listens to these messages via javascriptRuntime.onMessage(channelName, callback).

The problem is: the function provided to callback has the return type of void, i.e. the JS code can call dart, but it cannot get a return value.

From the JS code, if we call:

const result = sendMessage(channelName);

Then result should get the return value of the message handler, written in Dart.

There are workarounds for this problem, but I can't think of any that doesn't involve transforming the JS code into async, which can not be viable in some cases. Furthermore, any solution other than having a return value for sendMessage will be more complicated than it should be.

@LucaBoss74 added support for this on QuickJS in #54, but it still didn't work for JavascriptCore. Also, the callback for onMessage had void as a return type when it should be dynamic.

This PR changes the return type of onMessage to dynamic and includes support for returning values from message events on JavascriptCore.

I think with this PR the full support for this feature should be ready. @abner can you check please if what I did is correct? This is the first time I mess with dart:ffi and I might have overlooked something.

Tested with Android (forceJavascriptCoreOnAndroid: true) and iOS.

@abner
Copy link
Owner

abner commented Nov 9, 2021

Hi @Tiagoperes, thanks for the Pull Request. I will check it and would merge if it is OK.

Related issues

#53

Description

The javascript code calls the Dart code via the function sendMessage. Dart listens to these messages via javascriptRuntime.onMessage(channelName, callback).

The problem is: the function provided to callback has the return type of void, i.e. the JS code can call dart, but it cannot get a return value.

From the JS code, if we call:

const result = sendMessage(channelName);

Then result should get the return value of the message handler, written in Dart.

There are workarounds for this problem, but I can't think of any that doesn't involve transforming the JS code into async, which can not be viable in some cases. Furthermore, any solution other than having a return value for sendMessage will be more complicated than it should be.

@LucaBoss74 added support for this on QuickJS in #54, but it still didn't work for JavascriptCore. Also, the callback for onMessage had void as a return type when it should be dynamic.

This PR changes the return type of onMessage to dynamic and includes support for returning values from message events on JavascriptCore.

I think with this PR the full support for this feature should be ready. @abner can you check please if what I did is correct? This is the first time I mess with dart:ffi and I might have overlooked something.

Tested with Android (forceJavascriptCoreOnAndroid: true) and iOS.

@abner abner merged commit aa08136 into abner:master Nov 11, 2021
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