@@ -41,12 +41,12 @@ class RequesterResponder {
41
41
( ) : { send ?: < T > ( message : Message < T > ) => any }
42
42
}
43
43
= ( ) => { throw new Error ( 'getProcess is abstract' ) ; return null ; }
44
-
45
-
44
+
45
+
46
46
///////////////////////////////// REQUESTOR /////////////////////////
47
-
47
+
48
48
private currentListeners : { [ messages : string ] : { [ id : string ] : PromiseDeferred < any > } } = { } ;
49
-
49
+
50
50
/** process a message from the child */
51
51
protected processResponse ( m : any ) {
52
52
var parsed : Message < any > = m ;
@@ -67,39 +67,39 @@ class RequesterResponder {
67
67
delete this . currentListeners [ parsed . message ] [ parsed . id ] ;
68
68
}
69
69
}
70
-
71
- /**
72
- * Takes a sync named function
73
- * and returns a function that will execute the function by name on the child
74
- * if the child has a responder registered
70
+
71
+ /**
72
+ * Takes a sync named function
73
+ * and returns a function that will execute this function by name using IPC
74
+ * (will only work if the process on the other side has this function as a registered responder)
75
75
*/
76
- childQuery < Query , Response > ( func : ( query : Query ) => Promise < Response > ) : ( data : Query ) => Promise < Response > {
76
+ sendToIpc < Query , Response > ( func : ( query : Query ) => Promise < Response > ) : ( data : Query ) => Promise < Response > {
77
77
var that = this ; // Needed because of a bug in the TS compiler (Don't change the previous line to labmda ^ otherwise this becomes _this but _this=this isn't emitted)
78
78
return ( data ) => {
79
79
var message = func . name ;
80
-
80
+
81
81
// If we don't have a child exit
82
82
if ( ! that . getProcess ( ) ) {
83
83
console . log ( 'PARENT ERR: no child when you tried to send :' , message ) ;
84
84
return < any > Promise . reject ( new Error ( "No worker active to recieve message: " + message ) ) ;
85
85
}
86
-
86
+
87
87
// Initialize if this is the first call of this type
88
88
if ( ! that . currentListeners [ message ] ) this . currentListeners [ message ] = { } ;
89
-
89
+
90
90
// Create an id unique to this call and store the defered against it
91
91
var id = createId ( ) ;
92
92
var defer = Promise . defer < Response > ( ) ;
93
93
that . currentListeners [ message ] [ id ] = defer ;
94
-
94
+
95
95
// Send data to worker
96
96
that . getProcess ( ) . send ( { message : message , id : id , data : data , request : true } ) ;
97
97
return defer . promise ;
98
98
} ;
99
99
}
100
-
100
+
101
101
////////////////////////////////// RESPONDER ////////////////////////
102
-
102
+
103
103
private responders : { [ message : string ] : < Query , Response > ( query : Query ) => Promise < Response > } = { } ;
104
104
105
105
protected processRequest = ( m : any ) => {
@@ -115,7 +115,7 @@ class RequesterResponder {
115
115
} catch ( err ) {
116
116
responsePromise = Promise . reject ( { method : message , message : err . message , stack : err . stack , details : err . details || { } } ) ;
117
117
}
118
-
118
+
119
119
responsePromise
120
120
. then ( ( response ) => {
121
121
this . getProcess ( ) . send ( {
@@ -154,12 +154,12 @@ class RequesterResponder {
154
154
export class Parent extends RequesterResponder {
155
155
156
156
private child : childprocess . ChildProcess ;
157
- private node = process . execPath ;
158
-
157
+ private node = process . execPath ;
158
+
159
159
/** If we get this error then the situation if fairly hopeless */
160
160
private gotENOENTonSpawnNode = false ;
161
161
protected getProcess = ( ) => this . child ;
162
-
162
+
163
163
/** start worker */
164
164
startWorker ( childJsPath : string , terminalError : ( e : Error ) => any ) {
165
165
try {
@@ -211,7 +211,7 @@ export class Parent extends RequesterResponder {
211
211
terminalError ( err ) ;
212
212
}
213
213
}
214
-
214
+
215
215
/** stop worker */
216
216
stopWorker ( ) {
217
217
if ( ! this . child ) return ;
@@ -231,10 +231,10 @@ export class Child extends RequesterResponder {
231
231
232
232
constructor ( ) {
233
233
super ( ) ;
234
-
235
- // Keep alive
234
+
235
+ // Keep alive
236
236
this . keepAlive ( ) ;
237
-
237
+
238
238
// Start listening
239
239
process . on ( 'message' , ( message : Message < any > ) => {
240
240
if ( message . request ) {
@@ -245,7 +245,7 @@ export class Child extends RequesterResponder {
245
245
}
246
246
} ) ;
247
247
}
248
-
248
+
249
249
/** keep the child process alive while its connected and die otherwise */
250
250
private keepAlive ( ) {
251
251
setInterval ( ( ) => {
@@ -255,4 +255,4 @@ export class Child extends RequesterResponder {
255
255
}
256
256
} , 1000 ) ;
257
257
}
258
- }
258
+ }
0 commit comments