Skip to content

Commit 54bbbc8

Browse files
author
Nikos M
committed
v.0.5.1
* make sure to handle instantiatedThreads instead of just threads (so Asynchronous can be loaded as utility in a thread without being necesarily instatiated automaticaly)
1 parent 32b433a commit 54bbbc8

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

asynchronous.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* Asynchronous.js
4-
* @version: 0.5.0
4+
* @version: 0.5.1
55
*
66
* Simple JavaScript class to manage asynchronous, parallel, linear, sequential and interleaved tasks
77
* https://github.com/foo123/asynchronous.js
@@ -47,11 +47,12 @@ var PROTO = "prototype", HAS = "hasOwnProperty"
4747
,isBrowserFrame = isBrowser && (window.self !== window.top)
4848
,isAMD = ("function" === typeof define ) && define.amd
4949
,supportsMultiThread = isNode || ("function" === typeof Worker) || ("function" === typeof SharedWorker)
50+
,root = isNode ? this : (isSharedWorker || isWebWorker ? self||this : window||this)
51+
,scope = isNode ? module.$deps : (isSharedWorker || isWebWorker ? self||this : window||this)
52+
,globalScope = isNode ? global : (isSharedWorker || isWebWorker ? self||this : window||this)
5053
,isThread = isNodeProcess || isSharedWorker || isWebWorker
51-
,Listener = isThread ? function Listener( msg ) { if ( Listener.handler ) Listener.handler( isNodeProcess ? msg : msg.data ); } : NOP
52-
,root = isNode ? this : (isSharedWorker || isWebWorker ? self : window||this)
53-
,scope = isNode ? module.$deps : (isSharedWorker || isWebWorker ? self : window||this)
54-
,globalScope = isNode ? global : (isSharedWorker || isWebWorker ? self : window||this)
54+
,isInstantiatedThread = (isNodeProcess && (0 === process.listenerCount('message'))) || (isSharedWorker && !root.onconnect) || (isWebWorker && !root.onmessage)
55+
,Listener = isInstantiatedThread ? function Listener( msg ) { if ( Listener.handler ) Listener.handler( isNodeProcess ? msg : msg.data ); } : NOP
5556
,Thread, component = null, LOADED = {}, numProcessors = isNode ? require('os').cpus( ).length : 4
5657

5758
,URL = !isNode ? ("undefined" !== typeof root.webkitURL ? root.webkitURL : ("undefined" !== typeof root.URL ? root.URL : null)) : null
@@ -221,6 +222,8 @@ else
221222
}
222223
}
223224

225+
if ( isInstantiatedThread )
226+
{
224227
if ( isSharedWorker )
225228
{
226229
root.close = NOP;
@@ -247,6 +250,7 @@ else if ( isNodeProcess )
247250
};
248251
process.on('message', Listener);
249252
}
253+
}
250254

251255
// Proxy to communication/asyc to another browser window
252256
function formatOptions( o )
@@ -652,9 +656,9 @@ var Asynchronous = function Asynchronous( interval, initThread ) {
652656
self.$runmode = NONE;
653657
self.$running = false;
654658
self.$queue = [ ];
655-
if ( isThread && (false !== initThread) ) self.initThread( );
659+
if ( isInstantiatedThread && (false !== initThread) ) self.initThread( );
656660
};
657-
Asynchronous.VERSION = "0.5.0";
661+
Asynchronous.VERSION = "0.5.1";
658662
Asynchronous.Thread = Thread;
659663
Asynchronous.Task = Task;
660664
Asynchronous.BrowserWindow = BrowserWindow;
@@ -666,10 +670,11 @@ Asynchronous.isPlatform = function( platform ) {
666670
else if ( BROWSER === platform ) return isBrowser;
667671
return undef;
668672
};
669-
Asynchronous.isThread = function( platform ) {
670-
if ( NODE === platform ) return isNodeProcess;
671-
else if ( BROWSER === platform ) return isSharedWorker || isWebWorker;
672-
return isThread;
673+
Asynchronous.isThread = function( platform, instantiated ) {
674+
instantiated = true === instantiated ? isInstantiatedThread : true;
675+
if ( NODE === platform ) return instantiated && isNodeProcess;
676+
else if ( BROWSER === platform ) return instantiated && (isSharedWorker || isWebWorker);
677+
return instantiated && isThread;
673678
};
674679
Asynchronous.path = path;
675680
Asynchronous.blob = blobURL;
@@ -725,9 +730,9 @@ Asynchronous.load = function( component, imports, asInstance ) {
725730
}
726731
return null;
727732
};
728-
Asynchronous.listen = isThread ? function( handler ){ Listener.handler = handler; } : NOP;
729-
Asynchronous.send = isThread ? function( data ){ root.postMessage( data ); } : NOP;
730-
Asynchronous.importScripts = isThread
733+
Asynchronous.listen = isInstantiatedThread ? function( handler ){ Listener.handler = handler; } : NOP;
734+
Asynchronous.send = isInstantiatedThread ? function( data ){ root.postMessage( data ); } : NOP;
735+
Asynchronous.importScripts = isInstantiatedThread
731736
? function( scripts ){
732737
if ( scripts && scripts.length )
733738
{
@@ -744,7 +749,7 @@ Asynchronous.importScripts = isThread
744749
}
745750
}
746751
: NOP;
747-
Asynchronous.close = isThread ? function( ){ root.close( ); } : NOP;
752+
Asynchronous.close = isInstantiatedThread ? function( ){ root.close( ); } : NOP;
748753
Asynchronous.log = "undefined" !== typeof console ? function( s ){ console.log( s||'' ); } : NOP;
749754

750755
// async queue as serializer
@@ -786,7 +791,7 @@ Asynchronous[PROTO] = {
786791
self.$queue = null;
787792
self.$runmode = NONE;
788793
self.$running = false;
789-
if ( isThread && (true === thread) ) Asynchronous.close( );
794+
if ( isInstantiatedThread && (true === thread) ) Asynchronous.close( );
790795
return self;
791796
}
792797

@@ -878,7 +883,7 @@ Asynchronous[PROTO] = {
878883

879884
,initThread: function( ) {
880885
var self = this;
881-
if ( isThread )
886+
if ( isInstantiatedThread )
882887
{
883888
self.$events = { };
884889
Asynchronous.listen(function( msg ) {
@@ -917,7 +922,7 @@ Asynchronous[PROTO] = {
917922
,send: function( event, data ) {
918923
if ( event )
919924
{
920-
if ( isThread )
925+
if ( isInstantiatedThread )
921926
Asynchronous.send({event: event, data: data || null});
922927
else if ( this.$thread )
923928
this.$thread.postMessage({event: event, data: data || null});
@@ -1072,7 +1077,7 @@ Asynchronous[PROTO] = {
10721077
}
10731078
};
10741079

1075-
if ( isThread )
1080+
if ( isInstantiatedThread )
10761081
{
10771082
/*globalScope.console = {
10781083
log: function( s ){

0 commit comments

Comments
 (0)