feature: synchronous communication with dart from javascript #67
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related issues
#53
Description
The javascript code calls the Dart code via the function
sendMessage
. Dart listens to these messages viajavascriptRuntime.onMessage(channelName, callback)
.The problem is: the function provided to
callback
has the return type ofvoid
, i.e. the JS code can call dart, but it cannot get a return value.From the JS code, if we call:
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
hadvoid
as a return type when it should bedynamic
.This PR changes the return type of
onMessage
todynamic
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.