1
+ /* globals setImmediate, clearImmediate, setTimeout, clearTimeout */
2
+ /* jshint -W083, -W098 */
1
3
"use strict" ;
2
4
3
5
/*
@@ -29,7 +31,7 @@ var RECOVER = "Recover"; // Continue with `Either Error a` (via attempt)
29
31
var RESUME = "Resume" ; // Continue indiscriminately
30
32
var FINALIZED = "Finalized" ; // Marker for finalization
31
33
32
- function Aff ( tag , _1 , _2 , _3 ) {
34
+ function Aff ( tag , _1 , _2 , _3 ) {
33
35
this . tag = tag ;
34
36
this . _1 = _1 ;
35
37
this . _2 = _2 ;
@@ -117,6 +119,33 @@ exports._delay = function () {
117
119
} ;
118
120
} ( ) ;
119
121
122
+ function runEff ( eff ) {
123
+ try {
124
+ eff ( ) ;
125
+ } catch ( error ) {
126
+ setTimeout ( function ( ) {
127
+ throw error ;
128
+ } , 0 ) ;
129
+ }
130
+ }
131
+
132
+ function runSync ( left , right , eff ) {
133
+ try {
134
+ return right ( eff ( ) ) ;
135
+ } catch ( error ) {
136
+ return left ( error ) ;
137
+ }
138
+ }
139
+
140
+ function runAsync ( left , eff , k ) {
141
+ try {
142
+ return eff ( k ) ( ) ;
143
+ } catch ( error ) {
144
+ k ( left ( error ) ) ( ) ;
145
+ return nonCanceler ;
146
+ }
147
+ }
148
+
120
149
// Thread state machine
121
150
var BLOCKED = 0 ; // No effect is running.
122
151
var PENDING = 1 ; // An async effect is running.
@@ -164,7 +193,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
164
193
// accidentally resuming the same thread. A common example may be invoking
165
194
// the provided callback in `makeAff` more than once, but it may also be an
166
195
// async effect resuming after the thread was already cancelled.
167
- function run ( localRunTick ) {
196
+ function run ( localRunTick ) {
168
197
while ( 1 ) {
169
198
switch ( status ) {
170
199
case BINDSTEP :
@@ -363,14 +392,16 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
363
392
case COMPLETED :
364
393
tmp = false ;
365
394
for ( var k in joins ) {
366
- tmp = true ;
367
- runEff ( joins [ k ] ( step ) ) ;
395
+ if ( { } . hasOwnProperty . call ( joins , k ) ) {
396
+ tmp = true ;
397
+ runEff ( joins [ k ] ( step ) ) ;
398
+ }
368
399
}
369
400
joins = tmp ;
370
401
// If we have an unhandled exception, and no other thread has joined
371
402
// then we need to throw the exception in a fresh stack.
372
403
if ( isLeft ( step ) && ! joins ) {
373
- setTimeout ( function ( ) {
404
+ setTimeout ( function ( ) {
374
405
// Guard on joins because a completely synchronous thread can
375
406
// still have an observer.
376
407
if ( ! joins ) {
@@ -390,7 +421,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
390
421
}
391
422
}
392
423
393
- function addJoinCallback ( cb ) {
424
+ function addJoinCallback ( cb ) {
394
425
var jid = joinId ++ ;
395
426
joins [ jid ] = cb ;
396
427
return function ( error ) {
@@ -400,7 +431,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
400
431
} ;
401
432
}
402
433
403
- function kill ( error ) {
434
+ function kill ( error ) {
404
435
return new Aff ( ASYNC , function ( cb ) {
405
436
return function ( ) {
406
437
// Shadow the canceler binding because it can potentially be
@@ -447,7 +478,7 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
447
478
} ) ;
448
479
}
449
480
450
- function join ( ) {
481
+ function join ( ) {
451
482
return new Aff ( ASYNC , function ( cb ) {
452
483
return function ( ) {
453
484
if ( status === COMPLETED ) {
@@ -468,30 +499,3 @@ exports._launchAff = function (isLeft, fromLeft, fromRight, left, right, aff) {
468
499
} ;
469
500
} ;
470
501
} ;
471
-
472
- function runEff ( eff ) {
473
- try {
474
- eff ( ) ;
475
- } catch ( error ) {
476
- setTimeout ( function ( ) {
477
- throw error ;
478
- } , 0 ) ;
479
- }
480
- }
481
-
482
- function runSync ( left , right , eff ) {
483
- try {
484
- return right ( eff ( ) ) ;
485
- } catch ( error ) {
486
- return left ( error ) ;
487
- }
488
- }
489
-
490
- function runAsync ( left , eff , k ) {
491
- try {
492
- return eff ( k ) ( ) ;
493
- } catch ( error ) {
494
- k ( left ( error ) ) ( ) ;
495
- return nonCanceler ;
496
- }
497
- }
0 commit comments