Web Workers

233 views
Skip to first unread message

Chester

unread,
Jul 17, 2018, 11:41:38 AM7/17/18
to DroidScript
How do I call the app object inside a dedicated/shared worker.

importScripts return prompt not defined when I try to import the app.js

Is there any other way to call app object inside a worker?

Steve Garman

unread,
Jul 17, 2018, 12:03:55 PM7/17/18
to DroidScript
Sadly not.

The thread a worker runs in does not have access to the DOM that provides the java<==>JavaScript bridge.

Dave

unread,
Jul 17, 2018, 1:29:30 PM7/17/18
to DroidScript
You may like to know that DroidScript has its own version of webworkers which allow you to access the GUI.  This is done using the _Thread( file ) function which is somewhat experimental still as not many people have used it yet.  It may not be useful for your needs if you need to share access to the same controls.

You just pass the name of your script file to the function and it runs it in a separate thread.

Note: When using the _Thread method, each script engine is independent of the other and controls created in a worker thread will not be accessible in the main thread and visa versa.



Dave

unread,
Jul 17, 2018, 1:35:19 PM7/17/18
to DroidScript
In theory, you could share access to the same controls by saving their id's at the app level using the app.SetData() method, or via file or app.SaveText().  

Maybe dumping the results of app.GetObjects() to a json string into app.SetData() would be worth a try. (I'd be interested to know if this works, if anyone would like to try it)

Chester

unread,
Jul 17, 2018, 2:40:56 PM7/17/18
to DroidScript
Thanks.
I will try it and inform my progress my here

Jason Custer

unread,
Jan 27, 2020, 6:24:56 AM1/27/20
to DroidScript
Just curious, do the new thread and original (main) thread have a way to communicate? IE web worker's postMessage and onmessage? If someone can explain this, I'll be happy to put an SPK on the droidstore and send the link.
Thx!

Steve Garman

unread,
Jan 28, 2020, 5:03:20 PM1/28/20
to DroidScript
I don't think _Thread() is available in recent versions of DroidScript.

It uses the NoDom engine which may or may not return at some time in the future.

Jason Custer

unread,
Jan 29, 2020, 11:45:50 AM1/29/20
to DroidScript
Okay, thank you for getting back to me. I'll experiment with web workers and see how it goes, if I can figure it out I'll submit an spk to the droidscript store. Film at eleven!

Steve Garman

unread,
Jan 30, 2020, 5:22:19 AM1/30/20
to DroidScript
That would be very useful Jadon.

I gave up trying to use workers in DS a long time ago but I'd be fascinated to see if they are any more straightforward now.

Kallikratidas

unread,
Jan 30, 2020, 8:08:13 PM1/30/20
to DroidScript
I found a way to call the app object inside a worker.
Check this SPK and see.
In order to be able to use it also for app building, like using layouts and canvas, you need to use a synchronized api because the default web workers api is asynchronous.
Check this page:
https://www.html5rocks.com/en/tutorials/file/filesystem-sync/
App Worker.spk

Kallikratidas

unread,
Jan 31, 2020, 10:41:00 AM1/31/20
to DroidScript
Of course, the best to do is to send from the worker a stringified code and eval it in the main app.

post message("MyFunctionInMainApp('Hi',"+new Date().get time()+");");

Jason Custer

unread,
Jan 31, 2020, 2:56:57 PM1/31/20
to DroidScript
Hi all. I created an SPK too, it's attached. The problem I'm seeing is that my worker is on the same thread as the main app. I'm a web worker newbie, so maybe my code is wrong? I also thought I read that the android webview doesn't support workers on a seperate thread, but this was in reference to cordova. Hmmm, I see why people give up on this idea. If using another thread is what I want, it looks like I too will be working on a background service.
Any suggestions/corrections appreciated. Just experementing, I don't need it for a project. Just wanted to expand my brain.
Thanks!
Web Worker.spk
Reply all
Reply to author
Forward
0 new messages