openwebf / mercury

A library with integrated JavaScript engine and extension utils for Flutter Apps.
Apache License 2.0
15 stars 3 forks source link

Documentation #21

Open MulverineX opened 5 months ago

mochar commented 5 months ago

Is there an example we can look at in the meantime?

MulverineX commented 5 months ago

https://github.com/openwebf/mercury/blob/main/mercury%2Fexample%2Flib%2Fmain.dart

https://github.com/RefractureMedia/refracture-music/blob/main/packages%2Fclient-flutter%2Flib%2Fapp%2Fcore.dart https://github.com/RefractureMedia/refracture-music/blob/main/packages%2Fclient-flutter%2Flib%2Fmain.dart#L8

mochar commented 4 months ago

@MulverineX From these examples it is not clear how one can get the results back from javascript calls. Perhaps with MercuryDispatcher but the workings of it I did not manage to figure out. Any help is appreciated

MulverineX commented 4 months ago

@mochar

final result = evaluateScripts('path.to.function("bar")')`

This is how Flutter land issues events to JS land and receives a response back synchronously


controller.context.dispatcher?.subscribe('example', (args) {
            evaluateScripts(`${args[0]['callback']}('hellooooo')`)
          });
let baz = (foo) => console.log(foo)

mercury.dispatcher.dispatch('example', { callback: `baz`});

This is how JS land issues events to Flutter land and receives a response back with a callback (there is no synchronous response mechanism)

MulverineX commented 4 months ago

If you would like to have synchronous response from Dart somehow for simpler code, you're welcome to create a feature request issue for that, just keep in mind it won't be a priority, it's more likely it'll be implemented if we receive a PR. Remember that you can wrap callback systems like this with a Promise to make it cleaner in asynchronous functions.

mochar commented 4 months ago

@MulverineX Thanks a lot!!