Nativescript: Worker on iOS Can't find variable: onmessage

Created on 14 Mar 2017  ยท  5Comments  ยท  Source: NativeScript/NativeScript

Hi, i'm trying to use worker on iOS but i'm getting this error only on iOS(on android run well)

Logs:

Objective-C class name "UIDocumentInteractionControllerDelegateImpl" is already in use - using "UIDocumentInteractionControllerDelegateImpl1" instead.
Objective-C class name "TimerTargetImpl" is already in use - using "TimerTargetImpl1" instead.
1   0x10fa0933c -[TNSRuntime executeModule:referredBy:]
2   0x10fa1561a NativeScript::WorkerMessagingProxy::workerPerformWork()
3   0x113206301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
4   0x1131fc22c __CFRunLoopDoSources0
5   0x1131fb6e3 __CFRunLoopRun
6   0x1131fb0f8 CFRunLoopRunSpecific
7   0x1132b2501 CFRunLoopRun
8   0x10fa16ba5 NativeScript::WorkerMessagingProxy::workerThreadInitialize(std::__1::shared_ptr<NativeScript::WorkerMessagingProxy>, WTF::String const&, WTF::String const&, WTF::String const&)
9   0x10fa1624b NativeScript::WorkerMessagingProxy::workerThreadMain(std::__1::shared_ptr<NativeScript::WorkerMessagingProxy>, WTF::String const&, WTF::String const&, WTF::String const&)
10  0x10fa1791c std::__1::__function::__func<std::__1::__bind<void (&)(std::__1::shared_ptr<NativeScript::WorkerMessagingProxy>, WTF::String const&, WTF::String const&, WTF::String const&), std::__1::shared_ptr<NativeScript::WorkerMessagingProxy>&, WTF::String const&, WTF::String const&, WTF::String const&>, std::__1::allocator<std::__1::__bind<void (&)(std::__1::shared_ptr<NativeScript::WorkerMessagingProxy>, WTF::String const&, WTF::String const&, WTF::String const&), std::__1::shared_ptr<NativeScript::WorkerMessagingProxy>&, WTF::String const&, WTF::String const&, WTF::String const&> >, void ()>::operator()()
11  0x10fa3de92 WTF::threadEntryPoint(void*)
12  0x10fa3e0bf WTF::wtfThreadEntryPoint(void*)
13  0x114269aab _pthread_body
14  0x1142699f7 _pthread_body
15  0x1142691fd thread_start
JS ERROR ReferenceError: Can't find variable: onmessage
An unhandled error occurred in worker: file:///app/providers/encription-service-worker.js, line: 7 : ReferenceError: Can't find variable: onmessage

So this is my worker code written in typescript

import 'globals'; // necessary to bootstrap tns modules on the new thread
let client;
import { ClientAuthCeS } from '@ces-sec/client';
import { jwk } from '@ces-sec/jose';


// tslint:disable-next-line:only-arrow-functions
declare function postMessage(message: any, targetOrigin?: string, transfer?: any[]): void;

onmessage = (msg) => {

    switch (msg.data.action) {
    case 'init':
        let key = new jwk.JWKRSA(msg.data.key);
        client = new ClientAuthCeS(key);
        break;
    case 'encode':
        try {
            postMessage({ resultType: 'encode', value: client.encodeRequest(msg.data.body) });
        } catch (error) {
            console.error('error on encrition service', error);
        }
        break;
    case 'decode':
        postMessage({
            resultType: 'decode',
            value: client.parseResponse(msg.data.body, msg.data.value)
        });
        break;
    default:
        console.error('Action not implemented:' + msg.data.action);
        break;
    }
};

onerror = (error) => {
    console.error('on ecryption worker error', error);
};

And this is the generated js code:

"use strict";
require("globals"); // necessary to bootstrap tns modules on the new thread
console.log('encryption service worker');
var client;
var client_1 = require("@ces-sec/client");
var jose_1 = require("@ces-sec/jose");
onmessage = function (msg) {
    switch (msg.data.action) {
        case 'init':
            var key = new jose_1.jwk.JWKRSA(msg.data.key);
            client = new client_1.ClientAuthCeS(key);
            break;
        case 'encode':
            try {
                postMessage({ resultType: 'encode', value: client.encodeRequest(msg.data.body) });
            }
            catch (error) {
                console.error('error on encrition service', error);
            }
            break;
        case 'decode':
            postMessage({
                resultType: 'decode',
                value: client.parseResponse(msg.data.body, msg.data.value)
            });
            break;
        default:
            console.error('Action not implemented:' + msg.data.action);
            break;
    }
};
onerror = function (error) {
    console.error('on ecryption worker error', error);
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZW5jcmlwdGlvbi1zZXJ2aWNlLXdvcmtlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbImVuY3JpcHRpb24tc2Vydmlj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IG5ldyBDbGllbnRBdXRoQ2VTKGtleSk7XG4gICAgICAgIGJyZWFrO1xuICAgIGNhc2UgJ2VuY29kZSc6XG4gICAgICAgIHRyeSB7XG4gICAgICAgICAgICBwb3N0TWVzc2FnZSh7IHJlc3VsdFR5cGU6ICdlbmNvZGUnLCB2YWx1ZTogY2xpZW50LmVuY29kZVJlcXVlc3QobXNnLmRhdGEuYm9keSkgfSk7XG4gICAgICAgIH0gY2F0Y2ggKGVycm9yKSB7XG4gICAgICAgICAgICBjb25zb2xlLmVycm9yKCdlcnJvciBvbiBlbmNyaXRpb24gc2VydmljZScsIGVycm9yKTtcbiAgICAgICAgfVxuICAgICAgICBicmVhaztcbiAgICBjYXNlICdkZWNvZGUnOlxuICAgICAgICBwb3N0TWVzc2FnZSh7XG4gICAgICAgICAgICByZXN1bHRUeXBlOiAnZGVjb2RlJyxcbiAgICAgICAgICAgIHZhbHVlOiBjbGllbnQucGFyc2VSZXNwb25zZShtc2cuZGF0YS5ib2R5LCBtc2cuZGF0YS52YWx1ZSlcbiAgICAgICAgfSk7XG4gICAgICAgIGJyZWFrO1xuICAgIGRlZmF1bHQ6XG4gICAgICAgIGNvbnNvbGUuZXJyb3IoJ0FjdGlvbiBub3QgaW1wbGVtZW50ZWQ6JyArIG1zZy5kYXRhLmFjdGlvbik7XG4gICAgICAgIGJyZWFrO1xuICAgIH1cbn07XG5cbm9uZXJyb3IgPSAoZXJyb3IpID0+IHtcbiAgICBjb25zb2xlLmVycm9yKCdvbiBlY3J5cHRpb24gd29ya2VyIGVycm9yJywgZXJyb3IpO1xufTsiXX0=

I can see why isn't working, i have searched on issues and SO but could't find anything on this, any help is appreciated

tns info

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Component        โ”‚ Current version โ”‚ Latest version โ”‚ Information โ”‚
โ”‚ nativescript     โ”‚ 2.5.2           โ”‚ 2.5.2          โ”‚ Up to date  โ”‚
โ”‚ tns-core-modules โ”‚ 2.5.1           โ”‚ 2.5.1          โ”‚ Up to date  โ”‚
โ”‚ tns-android      โ”‚ 2.5.0           โ”‚ 2.5.0          โ”‚ Up to date  โ”‚
โ”‚ tns-ios          โ”‚ 2.5.0           โ”‚ 2.5.0          โ”‚ Up to date  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Most helpful comment

@NickIliev i was aware, i was not talking about that require('globals');, i was talking about onmessage =

so my implementation was like this before the fix:

import 'globals'; // necessary to bootstrap tns modules on the new thread
...
onmessage = (msg) => {
    ...
};

onerror = (error) => {
    ...
};

After the fix

import 'globals'; // necessary to bootstrap tns modules on the new thread
...
global.onmessage = (msg) => {
    ...
};

global.onerror = (error) => {
    ...
};

this was not mentioned on the docs

All 5 comments

Ok, fixed this buy using global.onmessage = ... (see in this article), it works on both android and ios, should't this be in the docs ? Or i'm missing something and i should't adopt this approach ?

Hey @danielgek you are not missing anything - it is mentioned in the snippet for the multithreading article with this line

require('globals'); // necessary to bootstrap tns modules on the new thread

@NickIliev i was aware, i was not talking about that require('globals');, i was talking about onmessage =

so my implementation was like this before the fix:

import 'globals'; // necessary to bootstrap tns modules on the new thread
...
onmessage = (msg) => {
    ...
};

onerror = (error) => {
    ...
};

After the fix

import 'globals'; // necessary to bootstrap tns modules on the new thread
...
global.onmessage = (msg) => {
    ...
};

global.onerror = (error) => {
    ...
};

this was not mentioned on the docs

@NickIliev I faced the exact same issue with NativeScript CLI 2.5.2 / Core Modules 2.5.1, @danielgek's solution worked for me. Please consider documenting it.

Note: Issue only happens when we want to use core modules inside worker and use require('globals').

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings