1

I'm creating an application in me where I want to download data from the internet. To avoid blocking of main thread I created a WorkerScript file. This works fine, but I need to abstract certain parts (like authorization, proper header, etc) to general-purpose functions.

Unfortunately, I haven't found a way to import the js file to the js file that serves as a worker thread. ".import" causes syntax error.

How can I achieve this?

Thank you.

Edit:

This is the relevant part from qml file:

Page {
    WorkerScript {
          id: myWorker
          source: Qt.resolvedUrl("loadDetails.js")
          onMessage:  {}
      }
}

And this is loadDetails.js:

.import "jsonrpc.js" as Jrpc
WorkerScript.onMessage = function(message) {
    // This is where I want to call functions from included file
}

3 Answers 3

3

See documentation provided by Oleg:

Worker script can not use .import syntax.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I missed that. Is there some other way to import functions in worker scripts?
@Jakub Koudelka: Maybe Qt.include() is a solution. I'm (also) beginner with QML ;)
0

I guess you have missed a qualifier in the import statement. The proper syntax is:

.import "filename.js" as Qualifier

See Qt Documentation for your case.

3 Comments

Syntax should be ok, I use it like this .import "jsonrpc.js" as Jrpc, but then get "filename:1: SyntaxError: Unexpected token ."
@JakubKoudelka, in which file exactly the error occurs? Can you show your beginnings of the files with import statements?
I added code samples to my question. The problem is caused at loadDedails.js:1.
0

This is more of a meta answer, as it is also addressed to the other answerers.

The Qt Documentation can at times be ambiguous. In the WorkerScript Documentation it states that:

Worker scripts that are plain JavaScript sources can not use .import syntax. Scripts that are ECMAScript modules can freely use import and export statements.

Except that doesn't seem to be the case. I have tried all different kinds of imports:

// example.mjs
.import "test.mjs" as Test
.import * as Test from "test.mjs";
import "test.mjs" as Test
import * as Test from "test.mjs";
import { test } from "test.mjs"

So it's obvious that it doesn't work, but the documentations states that it does - and that it doesn't...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.