Archive
Smart Mobile Studio, workers by code tip
Webworkers are essentially the browsers answer to classical threads. Although I cant for the life of me understand why the W3C have gone to such lengths to utterly screw up their standard, I can do my best to rectify their mess with easy Smart functions 🙂
In short, here is the challenge: Web workers must be stored in a separate file. So you can’t tell the browser to just take an object or method and “use that as a thread”. I wish you could do that, it would make my life easier – but sadly you can’t. You are expected to have separately compiled JavaScript worker-files, which know nothing about the main application. That last part is actually quite OK, since you don’t want multiple threads messing up your GUI.
The second challenge is that you can’t even share variables. All communication between a worker and your main application must be performed via messaging. So a worker can only receive and send messages. To start some “work” you essentially send over a text-based command (use JSON to stringify records for instance) telling the worker what to do.
Creating workers by code
Turns out there are legal ways of bypassing the first Webworker rules, namely that of a separate file. You can write your script to a blob (binary large object) and then use the URL composer to generate a URL for your object. This will be a unique resource locator which the browser can use to load data from. In short, here is how you do it:
function makeWebWorker(Script:String):THandle; var URL: Variant; BLOB: Variant; WORKER: Variant; mTemp: Variant; begin asm @URL = window.URL || window.webkitURL; @BLOB = window.Blob || window.webkitBlob; @WORKER = window.Worker || window.webkitWorker; var @mTemp = new @BLOB([@Script]); @result = new @WORKER(( @URL).createObjectURL(@mTemp)); end; end;
You can now create as many workers as you like by code, like this:
// create the worker var FWorker:=makeWebWorker( #"onMessage: function (msg) { postmessage(msg); }"); // setup message handler for when the worker sends us messages FWorker.onMessage := procedure (msg:string) begin writeln("Message back from worker!"); writeln(msg); end; // send message to worker FWorker.postmessage("Hello this is a test!");
And now you are probably thinking — oh why can’t i write Smart Pascal worker code?
Well there are several reasons for that. First of all, since the worker is 100% isolated it means that it wont recognize the RTL. The RTL would have to be loaded into the worker in order to function properly. And even then there are problems, because the VMT would be different between the main program and the worker program.
However, a completely clean, non RTL dependent piece of smart pascal will work! But the only way to get that done is to isolate it in a separate file.
But the trick above is handy, especially if all you want to do is check for features without interrupting the main program. And if you know JS fairly well you can also write some serious message-handling in pure JS which can be reflected by objects on the other side.
Another example
Below if a fully-blown example which successfully establishes a worker, installs a message-handler on both ends, and sends a message through. And it’s all done from the same source-code. Please note that getting the source of a function does not work on class members, only stand-alone functions.
Enjoy the code!
function makeWebWorker(Script:String):THandle; var URL: Variant; BLOB: Variant; WORKER: Variant; mTemp: Variant; begin asm @URL = window.URL || window.webkitURL; @BLOB = window.Blob || window.webkitBlob; @WORKER = window.Worker || window.webkitWorker; var @mTemp = new @BLOB([@Script]); @result = new @WORKER(( @URL).createObjectURL(@mTemp)); end; end; procedure TestCode; var mContext: THandle; begin asm @mContext = this; end; mContext.onmessage := procedure (msg:variant) begin asm console.log(@msg); end; end; end; procedure TForm1.W3Button3Click(Sender: TObject); var mRef: TProcedureRef; mCode: String; begin mRef:=@testcode; asm @mCode = @mref; end; mCode += "TestCode();"; var mWorker := makeWebWorker(mCode); mWorker.onmessage:=Procedure (msg:variant) begin writeln("We got a message back:" + String(msg.data)); end; mWorker.onerror:=procedure (msg:Variant) begin showmessage("An error occured!"); end; mWorker.postMessage("Hello dude!"); end;
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
You must be logged in to post a comment.