@@ -41,12 +41,12 @@ class RequesterResponder {
4141 ( ) : { send ?: < T > ( message : Message < T > ) => any }
4242 }
4343 = ( ) => { throw new Error ( 'getProcess is abstract' ) ; return null ; }
44-
45-
44+
45+
4646 ///////////////////////////////// REQUESTOR /////////////////////////
47-
47+
4848 private currentListeners : { [ messages : string ] : { [ id : string ] : PromiseDeferred < any > } } = { } ;
49-
49+
5050 /** process a message from the child */
5151 protected processResponse ( m : any ) {
5252 var parsed : Message < any > = m ;
@@ -67,39 +67,39 @@ class RequesterResponder {
6767 delete this . currentListeners [ parsed . message ] [ parsed . id ] ;
6868 }
6969 }
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)
7575 */
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 > {
7777 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)
7878 return ( data ) => {
7979 var message = func . name ;
80-
80+
8181 // If we don't have a child exit
8282 if ( ! that . getProcess ( ) ) {
8383 console . log ( 'PARENT ERR: no child when you tried to send :' , message ) ;
8484 return < any > Promise . reject ( new Error ( "No worker active to recieve message: " + message ) ) ;
8585 }
86-
86+
8787 // Initialize if this is the first call of this type
8888 if ( ! that . currentListeners [ message ] ) this . currentListeners [ message ] = { } ;
89-
89+
9090 // Create an id unique to this call and store the defered against it
9191 var id = createId ( ) ;
9292 var defer = Promise . defer < Response > ( ) ;
9393 that . currentListeners [ message ] [ id ] = defer ;
94-
94+
9595 // Send data to worker
9696 that . getProcess ( ) . send ( { message : message , id : id , data : data , request : true } ) ;
9797 return defer . promise ;
9898 } ;
9999 }
100-
100+
101101 ////////////////////////////////// RESPONDER ////////////////////////
102-
102+
103103 private responders : { [ message : string ] : < Query , Response > ( query : Query ) => Promise < Response > } = { } ;
104104
105105 protected processRequest = ( m : any ) => {
@@ -115,7 +115,7 @@ class RequesterResponder {
115115 } catch ( err ) {
116116 responsePromise = Promise . reject ( { method : message , message : err . message , stack : err . stack , details : err . details || { } } ) ;
117117 }
118-
118+
119119 responsePromise
120120 . then ( ( response ) => {
121121 this . getProcess ( ) . send ( {
@@ -154,12 +154,12 @@ class RequesterResponder {
154154export class Parent extends RequesterResponder {
155155
156156 private child : childprocess . ChildProcess ;
157- private node = process . execPath ;
158-
157+ private node = process . execPath ;
158+
159159 /** If we get this error then the situation if fairly hopeless */
160160 private gotENOENTonSpawnNode = false ;
161161 protected getProcess = ( ) => this . child ;
162-
162+
163163 /** start worker */
164164 startWorker ( childJsPath : string , terminalError : ( e : Error ) => any ) {
165165 try {
@@ -211,7 +211,7 @@ export class Parent extends RequesterResponder {
211211 terminalError ( err ) ;
212212 }
213213 }
214-
214+
215215 /** stop worker */
216216 stopWorker ( ) {
217217 if ( ! this . child ) return ;
@@ -231,10 +231,10 @@ export class Child extends RequesterResponder {
231231
232232 constructor ( ) {
233233 super ( ) ;
234-
235- // Keep alive
234+
235+ // Keep alive
236236 this . keepAlive ( ) ;
237-
237+
238238 // Start listening
239239 process . on ( 'message' , ( message : Message < any > ) => {
240240 if ( message . request ) {
@@ -245,7 +245,7 @@ export class Child extends RequesterResponder {
245245 }
246246 } ) ;
247247 }
248-
248+
249249 /** keep the child process alive while its connected and die otherwise */
250250 private keepAlive ( ) {
251251 setInterval ( ( ) => {
@@ -255,4 +255,4 @@ export class Child extends RequesterResponder {
255255 }
256256 } , 1000 ) ;
257257 }
258- }
258+ }
0 commit comments