You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// echo.js // `appcelerator-modules/ti.worker` APIworker.addEventListener('message',function(e){console.log('worker.addEventListener',JSON.stringify(e.data));worker.postMessage(e.data)});// also worksworker.onmessage=function(e){console.log('worker.onmessage',JSON.stringify(e.data));worker.postMessage(e.data)};
or
// echo.js Web Workers APIonmessage=function(e){console.log('onmessage',JSON.stringify(e.data));postMessage(e.data)};// also worksaddEventListener('message',function(e){console.log('addEventListener',JSON.stringify(e.data));postMessage(e.data)});
How it works:
Added v8::Locker for v8::Isolate
All Isolate specific templates/objects/functions/etc stored in map by thread id as a key (or Isolate itself, where appropriate).
After WorkerProxy creation it creates V8Worker instance which spawns a new thread.
In the new thread V8Worker creates new Isolate (and Locker), context and does "bootstrap", almost as if it was original V8Runtime
V8Worker reads scripts content (file name passed as parameter to WorkerProxy constructor/factory) and executes it in newly created "environment" (and of course in the new thread)
WorkerProxy
method postMessage
The postMessage() method of the Worker interface sends a message to the worker's inner scope. This accepts a single parameter, which is the data to send to the worker.
worker.postMessage(message);
message - The object to deliver to the worker; this will be in the data field in the event delivered to the onmessage handler (and listener added with addEventListener method). This may be any value or JavaScript object handled (WITHOUT CYCLES).
method terminate
The terminate() method of the Worker interface immediately terminates the Worker. This does not offer the worker an opportunity to finish its operations; it is simply stopped at once.
property onmessage
NOT IMPLEMENTED use worker.addEventListener('message', function(e) {...});
event message
Fired after call to postMessage inside worker. Field data contains data sended from worker.
event error
Fired when file specified at WorkerProxy creation can't be found or when it's empty.
Worker Global Scope
method postMessage
The postMessage() method of the WorkerGlobalScope interface sends a message to the main thread that spawned it.
method close
Discards any tasks queued in the WorkerGlobalScope's event loop, effectively closing this particular scope.
methods addEventListener and removeEventListener
Use them with event type message
property onmessage
The onmessage property of the WorkerGlobalScope interface represents an EventHandlerfunction to be called when the message event occurs and bubbles through the Worker — i.e. when a message is sent to the worker using the Worker.postMessage method.
event message
Fired after call to postMessage on WorkerProxy. Field data contains data sended from main thread.
Module appcelerator-modules/ti.worker partly API compliance
WorkerGlobalScope exposed as worker object on itself
method postMessage
method terminate
method nextTick (Not implemented. I would recommend to use Promise.resolve().then(...))
method addEventListener('message', ...)
event message
event terminated (Not implemented)
TODO:
EventTarget interface on WorkerGlobalScope (addEventListener/removeEventListener...)
Next steps (maybe some time later):
Debugger refactoring to support worker threads debugging
- Lock isolates for multithreading
- Group static cache and templates by isolate
- Run module code in separate thread using newly created isolate
- Web Workers API: "postMessage" and "onmessage" functions on WorkerGlobalScope
- ti.worker API: "worker" object on WorkerGlobalScope (with "postMessage" and
"onmessage" methods on it)
- Implement `EventTarget` interface on WorkerGlobalScope
It's turned out that we can't create worker with new Worker('file.js'), because when there is no Ti.Worker string in the application code, build system will not include "worker module" classes in the apk (for "production" builds). So for now Ti.Worker.createWorker is the only way to create new worker.
Also fixed onmessage handler (was called twice)
Added tests.
@ffMathy If you mean the code: I know at least one person (except me) who uses it. Everything should work as described in the first message. You can build it yourself or grab prebuilt SDK from here.
Let me know if you'll have any problems with it.
🎉 Another contribution from our awesome community member, drauggres! Thanks again for helping us make Titanium SDK better. 👍
📖
✅ All tests are passing
Nice one! All 4350 tests are passing.
(There are 471 tests skipped)
📖
🚨 This PR has one or more commits with warnings/errors for commit messages not matching our configuration. You may want to squash merge this PR and edit the message to match our conventions, or ask the original developer to modify their history.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JIRA: https://jira.appcelerator.org/browse/TIMOB-4790
This PR also includes changes for TIMOB-24549 and TIMOB-26239.
or
How it works:
v8::Locker
forv8::Isolate
Isolate
specific templates/objects/functions/etc stored in map by thread id as a key (orIsolate
itself, where appropriate).WorkerProxy
creation it createsV8Worker
instance which spawns a new thread.V8Worker
creates newIsolate
(andLocker
), context and does "bootstrap", almost as if it was originalV8Runtime
V8Worker
reads scripts content (file name passed as parameter toWorkerProxy
constructor/factory) and executes it in newly created "environment" (and of course in the new thread)WorkerProxy
method
postMessage
message
- The object to deliver to the worker; this will be in thedata
field in the event delivered to theonmessage
handler (and listener added withaddEventListener
method). This may be any value or JavaScript object handled (WITHOUT CYCLES).method
terminate
property
onmessage
NOT IMPLEMENTED use
worker.addEventListener('message', function(e) {...});
event
message
Fired after call to
postMessage
inside worker. Fielddata
contains data sended from worker.event
error
Fired when file specified at
WorkerProxy
creation can't be found or when it's empty.Worker Global Scope
method
postMessage
method
close
methods
addEventListener
andremoveEventListener
Use them with event type
message
property
onmessage
event
message
Fired after call to
postMessage
onWorkerProxy
. Fielddata
contains data sended from main thread.Module
appcelerator-modules/ti.worker
partly API complianceWorkerGlobalScope
exposed asworker
object on itselfpostMessage
terminate
method(Not implemented. I would recommend to usenextTick
Promise.resolve().then(...)
)addEventListener('message', ...)
message
event(Not implemented)terminated
TODO:
EventTarget
interface onWorkerGlobalScope
(addEventListener
/removeEventListener
...)Next steps (maybe some time later):