File tree 4 files changed +60
-108
lines changed
4 files changed +60
-108
lines changed Original file line number Diff line number Diff line change
1
+ exports . messageChannel = function ( ) {
2
+ var triggerCallback = createNextTickTrigger ( arguments ) ;
3
+ var channel = new MessageChannel ( ) ;
4
+ channel . port1 . onmessage = function ( ) {
5
+ triggerCallback ( ) ;
6
+ channel . port1 . close ( ) ;
7
+ } ;
8
+ channel . port2 . postMessage ( '' ) ;
9
+ } ;
10
+
11
+ exports . setTimeout = function ( ) {
12
+ var triggerCallback = createNextTickTrigger ( arguments ) ;
13
+ setTimeout ( triggerCallback ) ;
14
+ } ;
15
+
16
+ function createNextTickTrigger ( args ) {
17
+ var callback = args [ 0 ] ;
18
+ var _args = [ ] ;
19
+ for ( var i = 1 ; i < args . length ; i ++ ) {
20
+ _args [ i - 1 ] = args [ i ] ;
21
+ }
22
+
23
+ return function triggerCallback ( ) {
24
+ callback . apply ( null , _args ) ;
25
+ } ;
26
+ }
Original file line number Diff line number Diff line change
1
+ var nextTickImpl = require ( './next-tick' ) ;
1
2
2
3
exports . doNothing = doNothing ;
3
4
function doNothing ( ) { }
@@ -85,31 +86,13 @@ exports.truthy = function(arg) {
85
86
return ! ! arg ;
86
87
} ;
87
88
88
- exports . nextTick = function ( callback ) {
89
- if ( typeof process !== 'undefined' && process . nextTick ) {
90
- return process . nextTick . apply ( null , arguments ) ;
91
- }
92
-
93
- var args = [ ] ;
94
- for ( var i = 1 ; i < arguments . length ; i ++ ) {
95
- args [ i - 1 ] = arguments [ i ] ;
96
- }
97
-
98
- if ( typeof MessageChannel === 'undefined' ) {
99
- return setTimeout ( triggerCallback ) ;
100
- }
101
-
102
- var channel = new MessageChannel ( ) ;
103
- channel . port1 . onmessage = function ( ) {
104
- triggerCallback ( ) ;
105
- channel . port1 . close ( ) ;
106
- } ;
107
- channel . port2 . postMessage ( '' ) ;
108
-
109
- function triggerCallback ( ) {
110
- callback . apply ( null , args ) ;
111
- }
112
- } ;
89
+ if ( typeof process !== 'undefined' && typeof process . nextTick === 'function' ) {
90
+ exports . nextTick = process . nextTick ;
91
+ } else if ( typeof MessageChannel !== 'undefined' ) {
92
+ exports . nextTick = nextTickImpl . messageChannel ;
93
+ } else {
94
+ exports . nextTick = nextTickImpl . setTimeout ;
95
+ }
113
96
114
97
exports . clone = function ( obj ) {
115
98
return ( obj === undefined ) ? undefined : JSON . parse ( JSON . stringify ( obj ) ) ;
Original file line number Diff line number Diff line change
1
+ var nextTickImpl = require ( '../lib/next-tick' ) ;
2
+ var expect = require ( 'chai' ) . expect ;
3
+
4
+ describe ( 'nextTick' , function ( ) {
5
+ [ 'messageChannel' , 'setTimeout' ] . forEach ( function ( name ) {
6
+ var tick = nextTickImpl [ name ] ;
7
+
8
+ it ( 'passes args' , function ( done ) {
9
+ tick ( function ( arg1 , arg2 , arg3 ) {
10
+ expect ( arg1 ) . to . equal ( 'foo' ) ;
11
+ expect ( arg2 ) . to . equal ( 123 ) ;
12
+ expect ( arg3 ) . to . be . undefined ;
13
+ done ( ) ;
14
+ } , 'foo' , 123 ) ;
15
+ } ) ;
16
+
17
+ it ( 'calls asynchronously' , function ( done ) {
18
+ var called = false ;
19
+ tick ( function ( ) {
20
+ called = true ;
21
+ done ( ) ;
22
+ } ) ;
23
+ expect ( called ) . to . be . false ;
24
+ } ) ;
25
+ } ) ;
26
+ } ) ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments