Skip to content

Workers API #532

Open
Open

Description

@Plamen5kov

Support for Workers

For iOS solution please check - NativeScript/ios-jsc#620

Description

General guidelines for the Workers implementation effort and specification in the context of NativeScript.
We have an existing issue here, but the purpose here is to show the road map that the team intends to follow in developing the Workers functionality. The general scenarios we want to support are listed at the bottom.

Limitations

In NativeScript we don’t need to implement all details of the web workers specification, because some of these details are only related to the browser, and won’t have any meaning in the context of a NativeScript application. The features and syntax that will follow has the purpose of describing the adoption of the web workers specification in NativeScript. In this document we will describe and list what we intend to support.

Guidelines from the specification to follow

context

Notice that onmessage and postMessage() need to be hung off the Worker object when used in the main script thread, but not when used in the worker. This is because, inside the worker, the worker is effectively the global scope.

messaging specifics

Data passed between the main thread and workers is copied, not shared. Objects are serialized as they're handed to the worker, and subsequently, deserialized on the other end. The page and worker do not share the same instance, so the end result is that a duplicate is created on each end. Most browsers implement this feature as structured cloning.
The structured cloning algorithm can accept JSON and a few things that JSON can't — like circular references.

messaging transfer ownership

Passing data by transferring ownership (transferable objects): in nativescript we can try to pass native objects by transferring ownership on them.

onerror spec

The error event has the following three fields that are of interest:
message: A human-readable error message.
filename: The name of the script file in which the error occurred.
lineno: The line number of the script file on which the error occurred.

importScripts

ImportScripts in a nativescript context mean require. If we want to support this syntax we need an alias for require.

Syntax

The following example will be the general syntax to use when working with Workers. Syntax is based on web worker specification.

<app_name>/app/main.js

// create new worker
var myWorker = new Worker("worker.js");

// on worker error handler
myWorker.onerror = function (e) {
 //do on worker error
}

// receive messages from worker
myWorker.onmessage = function (e) {
  result.textContent = e.data;
}
....
// send messages to worker
myWorker.postMessage("message will be sent to worker");

//The worker thread is killed immediately without an opportunity to complete its operations or clean up after itself.
myWorker.terminate();

myWorker.onclose = function () {
//on close (clean up)
}

<app_name>/app/worker.js

onmessage = function(e) {
  postMessage(e.data); // worker -> main
}

//worker closes it self
close();

Supported APIs:

Worker Object:

  • Worker.onerror
  • Worker.onmessage
  • Worker.postMessage()
  • Worker.terminate()

Worker Global Object:

  • WorkerGlobalObject.self
  • WorkerGlobalObject.onmessage
  • WorkerGlobalObject.onerror
  • WorkerGlobalObject.onclose
  • WorkerGlobalObject.close()
  • WorkerGlobalObject.postMessage()
  • WorkerGlobalObject.importScripts()

Implementation steps:

  • Run pure javascript file on new thread
    • Initialize the android runtime on new thread
    • Create a run loop on the newly created thread and schedule the runtime on it
  • Enable require of pure javascript files from new thread
  • Implement error handling per thread
    • Try/catch should work like it does in the main thread
    • When error is not caught, call onerror handler of the worker object on the main thread
    • Throw error when calling UI APIs on worker thread (if possible)
  • Call native non-ui APIs from javascript file running on thread
    • android runtime tests pass on worker thread
  • Implement messaging API to communicate between threads (main -> worker, worker->main)
    • Simple object
    • Simple object and an ArrayBuffer as a second parameter
    • Native objects (change of ownership instead of copy)
  • Enable tns-module support (will extend later)
  • Debugging
    • Resarch what should be done on the backend side in order to support worker debugging
    • Debugging support in NativeScriptAppInspector client
    • Debugging support in VSCode
  • Add tests
    • Callback tests
    • Parameters/Properties/Ctor tests
    • Lifecycle tests
    • GC tests
  • Update documentation

Activity

  1. NathanaelA commented on Aug 9, 2016

    @NathanaelA
  2. blagoev commented on Aug 12, 2016

    @blagoev
    Contributor

    Also consider researching support for shared array buffers and atomics
    as per the spec https://github.com/tc39/ecmascript_sharedmem

    This is already under a flag in Chrome 48+

  3. NathanaelA commented on Aug 20, 2016

    @NathanaelA
  4. petekanev commented on Aug 20, 2016

    @petekanev
    Contributor

    @NathanaelA while experimenting with the worker API, I too wished that I could pass just a string, but could not. In the end it all boils down to a string, that is compiled in v8; only one has a source, and the other - doesn't, so it should be possible. Can't say yet if it will be in on the end of the first iteration of the WebWorkers support.

  5. added this to the 2.3.0 milestone on Aug 25, 2016
  6. roblav96 commented on Aug 30, 2016

    @roblav96

    @NathanaelA How does one go about experimenting with the preview channel of the WebWorkers?

  7. petekanev commented on Aug 30, 2016

    @petekanev
    Contributor

    @roblav96 when they are ready, they will be available in the master branch for others to test and experiment with.
    You can normally fetch the latest unstable changes from the master by invoking tns platform add android@next

  8. roblav96 commented on Aug 30, 2016

    @roblav96

    @Pip3r4o So are you saying I can run tns platform add android@next to experiment with the WebWorkers? :D

  9. petekanev commented on Aug 30, 2016

    @petekanev
    Contributor

    @roblav96 as soon as they make it into the master branch, yes. We will let you know when that is.

  10. NathanaelA commented on Aug 30, 2016

    @NathanaelA
  11. roblav96 commented on Aug 30, 2016

    @roblav96

    @Pip3r4o Awesome! Thanks so much!

    How might one go about applying for dev branch access? I would really like to become more apart of the community. Hopefully going to nativescript developer day will help with that.

  12. 26 remaining items

  13. dxshindeo commented on Jan 4, 2017

    @dxshindeo

    Since this issue is about threads, I guess the following question fits here as well - when using tns-core-modules/connectivity, can we leave its code in "main file" (main thread) or do we have to put it in worker (new thread), for max performance?

  14. petekanev commented on Jan 4, 2017

    @petekanev
    Contributor

    @dxshindeo I think leaving the calls to the main thread is fairly safe and should not be offloaded to a worker thread. In fact a new thread will likely have a negative impact to the app's available memory if it only deals with the connectivity functionality.

  15. dxshindeo commented on Jan 4, 2017

    @dxshindeo

    Big thanks for clearing that up!

  16. changed the title [-][Proposal]: Support for Workers[/-] [+]Workers API[/+] on Mar 12, 2018
  17. mohammadrafigh commented on May 28, 2018

    @mohammadrafigh

    is there any way to use transferable list as current state? postMessage accepts just 1 argument and throws exception when I provide a transferable list.

  18. dxshindeo commented on May 31, 2018

    @dxshindeo

    last I checked you can only transfer primitives

  19. petekanev commented on Jun 8, 2018

    @petekanev
    Contributor

    You can transfer objects recursively, but they can only contain primitives.

  20. farfromrefug commented on Dec 18, 2018

    @farfromrefug
    Contributor

    Is there any plan to move forward on this? I would love to be able to pass native object.
    Also i asked on the module repo but got no answer. Is there a way to combine this with a service? Which means having an Android background service running on its own thread? (bluetooth, udp, tcp ...)

  21. darind commented on Dec 18, 2018

    @darind
    Collaborator

    @farfromrefug, currently there are some technical limitations which prevent from passing native objects back and forth background workers.

    As a workaround you could store your native objects into some java dictionary and only pass the key to the worker thread.

    For example you could define a centralized dictionary:

    package com.myapp;
    
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    public final class WorkersContext {
        private final static Map<String, Object> container = new ConcurrentHashMap<String, Object>();
    
        public static Object getValue(String key) {
            return container.get(key);
        }
    
        public static void setValue(String key, Object value) {
            container.put(key, value);
        }
    }

    which you could use to persist your native object:

    var worker = new Worker("./EvalWorker.js");
    var nativeObject = new java.io.File("aaa.js");
    com.myapp.WorkersContext.setValue("my_key", nativeObject);
    var message = {
        value: { key: "my_key" }
    };
    worker.postMessage(message);

    and then in your background worker retrieve the stored native object:

    onmessage = function(msg) {
        var key = msg.data.value.key;
        var nativeObject = com.myapp.WorkersContext.getValue(key);
        // ... use the native object here
    }
  22. farfromrefug commented on Dec 19, 2018

    @farfromrefug
    Contributor

    @darind that's actually a nice idea and easy enough to implement. Would need to find a counterpart for iOS though.
    Do you have any idea about the service thing? Right now i do have a service extending android.app.Service And in this i run my bluetooth code. But the fact that any js is run on the main thread makes me really wonder about the real use of my service right now.

  23. jerbob92 commented on Jan 10, 2019

    @jerbob92

    Does anyone else have issues using existing code NativeScript/Angular code in the worker?
    I always get "Error: The AngularCompilerPlugin was not found. The @ngtools/webpack loader requires the plugin.", and I can't seem to get around it.

  24. es-eph commented on Oct 31, 2019

    @es-eph

    Hello, I'm having troubles using the geolocation plugin (nativescript-geolocation) inside a worker. I got the following error:

    Error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

    The error shows when i use the required plugin and execute one of it's functions:

    const geolocation = require("nativescript-geolocation");
    const { Accuracy } = require("tns-core-modules/ui/enums");
    ...
    async function getLocation() {

    await geolocation
        .getCurrentLocation({                           <= here explodes.
            desiredAccuracy: Accuracy.high,
            maximumAge: 5000,
            timeout: 20000
        })
        .then(res => {
    

    ...

    Is there a way around? or am I doing something wrong?

    Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions