1
1
/**
2
2
*
3
3
* Asynchronous.js
4
- * @version : 0.5.0
4
+ * @version : 0.5.1
5
5
*
6
6
* Simple JavaScript class to manage asynchronous, parallel, linear, sequential and interleaved tasks
7
7
* https://github.com/foo123/asynchronous.js
@@ -47,11 +47,12 @@ var PROTO = "prototype", HAS = "hasOwnProperty"
47
47
, isBrowserFrame = isBrowser && ( window . self !== window . top )
48
48
, isAMD = ( "function" === typeof define ) && define . amd
49
49
, 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 )
50
53
, 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
55
56
, Thread , component = null , LOADED = { } , numProcessors = isNode ? require ( 'os' ) . cpus ( ) . length : 4
56
57
57
58
, URL = ! isNode ? ( "undefined" !== typeof root . webkitURL ? root . webkitURL : ( "undefined" !== typeof root . URL ? root . URL : null ) ) : null
221
222
}
222
223
}
223
224
225
+ if ( isInstantiatedThread )
226
+ {
224
227
if ( isSharedWorker )
225
228
{
226
229
root . close = NOP ;
@@ -247,6 +250,7 @@ else if ( isNodeProcess )
247
250
} ;
248
251
process . on ( 'message' , Listener ) ;
249
252
}
253
+ }
250
254
251
255
// Proxy to communication/asyc to another browser window
252
256
function formatOptions ( o )
@@ -652,9 +656,9 @@ var Asynchronous = function Asynchronous( interval, initThread ) {
652
656
self . $runmode = NONE ;
653
657
self . $running = false ;
654
658
self . $queue = [ ] ;
655
- if ( isThread && ( false !== initThread ) ) self . initThread ( ) ;
659
+ if ( isInstantiatedThread && ( false !== initThread ) ) self . initThread ( ) ;
656
660
} ;
657
- Asynchronous . VERSION = "0.5.0 " ;
661
+ Asynchronous . VERSION = "0.5.1 " ;
658
662
Asynchronous . Thread = Thread ;
659
663
Asynchronous . Task = Task ;
660
664
Asynchronous . BrowserWindow = BrowserWindow ;
@@ -666,10 +670,11 @@ Asynchronous.isPlatform = function( platform ) {
666
670
else if ( BROWSER === platform ) return isBrowser ;
667
671
return undef ;
668
672
} ;
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 ;
673
678
} ;
674
679
Asynchronous . path = path ;
675
680
Asynchronous . blob = blobURL ;
@@ -725,9 +730,9 @@ Asynchronous.load = function( component, imports, asInstance ) {
725
730
}
726
731
return null ;
727
732
} ;
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
731
736
? function ( scripts ) {
732
737
if ( scripts && scripts . length )
733
738
{
@@ -744,7 +749,7 @@ Asynchronous.importScripts = isThread
744
749
}
745
750
}
746
751
: NOP ;
747
- Asynchronous . close = isThread ? function ( ) { root . close ( ) ; } : NOP ;
752
+ Asynchronous . close = isInstantiatedThread ? function ( ) { root . close ( ) ; } : NOP ;
748
753
Asynchronous . log = "undefined" !== typeof console ? function ( s ) { console . log ( s || '' ) ; } : NOP ;
749
754
750
755
// async queue as serializer
@@ -786,7 +791,7 @@ Asynchronous[PROTO] = {
786
791
self . $queue = null ;
787
792
self . $runmode = NONE ;
788
793
self . $running = false ;
789
- if ( isThread && ( true === thread ) ) Asynchronous . close ( ) ;
794
+ if ( isInstantiatedThread && ( true === thread ) ) Asynchronous . close ( ) ;
790
795
return self ;
791
796
}
792
797
@@ -878,7 +883,7 @@ Asynchronous[PROTO] = {
878
883
879
884
, initThread : function ( ) {
880
885
var self = this ;
881
- if ( isThread )
886
+ if ( isInstantiatedThread )
882
887
{
883
888
self . $events = { } ;
884
889
Asynchronous . listen ( function ( msg ) {
@@ -917,7 +922,7 @@ Asynchronous[PROTO] = {
917
922
, send : function ( event , data ) {
918
923
if ( event )
919
924
{
920
- if ( isThread )
925
+ if ( isInstantiatedThread )
921
926
Asynchronous . send ( { event : event , data : data || null } ) ;
922
927
else if ( this . $thread )
923
928
this . $thread . postMessage ( { event : event , data : data || null } ) ;
@@ -1072,7 +1077,7 @@ Asynchronous[PROTO] = {
1072
1077
}
1073
1078
} ;
1074
1079
1075
- if ( isThread )
1080
+ if ( isInstantiatedThread )
1076
1081
{
1077
1082
/*globalScope.console = {
1078
1083
log: function( s ){
0 commit comments