ultralight-ux / Ultralight

Lightweight, high-performance HTML renderer for game and app developers.
https://ultralig.ht
4.69k stars 197 forks source link

Extra event required in order to pass values back to JS? #204

Open Rjvs opened 5 years ago

Rjvs commented 5 years ago

We are trying to make JS promises that are fulfilled in C but we seem to need to create an extra event to kick the event loop, which is a little inelegant. Are we missing something?

Example:

  function myFunction() {
    var obj = {someJson:2};
    setInterval(null, 1000); // <- would prefer to not need this
    return new Promise(function(resolve, reject) {
      cFunction(obj, resolve, reject)
    }).then(function(value) {
        doSomething();
        }
    });
  }
Rjvs commented 4 years ago

@adamjs JS <-> C++ Promises could make an excellent addition to the Tutorials you're working on.

adamjs commented 4 years ago

Hi Robert,

Finally got around to investigating this, I modified Tutorial 4 to use Promises instead and had the C++ callback immediately invoke the Resolve function without any issues. I've shared the code in a Gist:

https://gist.github.com/adamjs/d689ad6d291a1cdd4e923c63a66cc22d

Please test it out and see if your results are the same (the app should display 'Hello' after you click the button).

Are you using the C API / JavaScriptCore API or the AppCore JSHelpers API? Which platform are you on? (I tested on Windows 10)

Also, after the C++ callback is invoked, are you caching the Resolve function for asynchronous invocation later? (I tested this too and was successful however I did find a bug in AppCore's JSFunction implementation-- any cached JSFunctions that are returned from a JS callback need to have their JSContextRef's replaced with the View's main JSContextRef otherwise JavaScriptCore will crash. I'm going to fix.).

Rjvs commented 4 years ago

Thanks Adam. Using a combination of the C API and AppCore JSHelpers, concentrating on Win10.

I'll take a closer look through the Gist and see what we're doing differently.