Skip to content

Allow Blocking Calls to be Called Async #215

@PanayotCankov

Description

@PanayotCankov

There are some native APIs that execute as blocking calls. Currently the JavaScript execution has a locking mechanism implemented and we support JavaScript execution in multiple threads, however the JavaScript in these scenarios is serialized. Which means the two threads does not execute simultaneously, but instead the JavaScript parts wait for each-other. The native code however executes without locking the JS.

We should research if we can implement a function similar to "call" and "apply" on our methods that would run the native part of a blocking call on a second thread, and once complete, continue on the thread it was started.

For example if we can create "callAsync" method that returns a promise and consume it like:

var n = NSClass.alloc().init();

// Works for 5 seconds blocking the original thread and returns:
var result = n.getDataBlocking(5);
console.log(result);

// "async" creates a promise, executes the getDataBlocking() on a background thread,
// the original thread continues execution, when complete "callAsync" returns to the
// original thread and resolves the promise, providing the result.
n.getDataBlocking.async(n, 5).then(function(result) {
    // 5 sec. later this gets executed on the original thread. 
    console.log(result);
});
// The original thread continues immediately.
console.log("performing next statement");

This should be in no way substitute for multi-threading using WebWorkers.

WARNING: This approach will degrade significantly if the native code actually performs chatty calls back to JavaScript!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions