@@ -4,42 +4,48 @@ export interface QueuedSender {
4
4
send : ( msg : any ) => void ;
5
5
}
6
6
7
+ const isWindows = / ^ w i n / . test ( process . platform ) ;
8
+ const logOnError = error => { if ( error ) { console . error ( error ) ; } } ;
9
+
7
10
// Wrapper around process.send() that will queue any messages if the internal node.js
8
11
// queue is filled with messages and only continue sending messages when the internal
9
12
// queue is free again to consume messages.
10
13
// On Windows we always wait for the send() method to return before sending the next message
11
14
// to workaround https://github.com/nodejs/node/issues/7657 (IPC can freeze process)
12
15
export function createQueuedSender ( childProcess : ChildProcess | NodeJS . Process ) : QueuedSender {
13
- let msgQueue = [ ] ;
14
- let isSending = false ;
15
-
16
- const doSendLoop = function ( ) : void {
17
- const msg = msgQueue . shift ( ) ;
18
-
19
- childProcess . send ( msg , error => {
20
- if ( error ) {
21
- console . error ( error ) ;
22
- }
16
+ if ( isWindows ) {
17
+ let msgQueue = [ ] ;
18
+ let isSending = false ;
23
19
20
+ const cb = error => {
21
+ logOnError ( error ) ;
24
22
if ( msgQueue . length > 0 ) {
25
23
setImmediate ( doSendLoop ) ;
26
24
} else {
27
25
isSending = false ;
28
26
}
29
- } ) ;
30
- } ;
27
+ } ;
31
28
32
- const send = function ( msg : any ) : void {
33
- msgQueue . push ( msg ) ; // add to the queue if the process cannot handle more messages
29
+ const doSendLoop = function ( ) : void {
30
+ childProcess . send ( msgQueue . shift ( ) , cb ) ;
31
+ } ;
34
32
35
- if ( isSending ) {
36
- return ;
37
- }
33
+ const send = function ( msg : any ) : void {
34
+ msgQueue . push ( msg ) ; // add to the queue if the process cannot handle more messages
35
+ if ( isSending ) {
36
+ return ;
37
+ }
38
38
39
- isSending = true ;
39
+ isSending = true ;
40
+ doSendLoop ( ) ;
41
+ } ;
40
42
41
- doSendLoop ( ) ;
42
- } ;
43
+ return { send } ;
44
+ } else {
45
+ const send = function ( msg : any ) : void {
46
+ childProcess . send ( msg , logOnError ) ;
47
+ } ;
43
48
44
- return { send } ;
49
+ return { send } ;
50
+ }
45
51
}
0 commit comments