Skip to content

Commit 5fb8851

Browse files
committed
abstracted child into a class in the workerlib
refs #51
1 parent c7e5b0e commit 5fb8851

File tree

4 files changed

+110
-92
lines changed

4 files changed

+110
-92
lines changed

lib/worker/child.js

+3-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
1-
var messages = require('./workerLib');
2-
messages.keepAlive();
3-
var responders = {};
4-
function processData(m) {
5-
var parsed = m;
6-
if (!parsed.message || !responders[parsed.message]) {
7-
return;
8-
}
9-
var message = parsed.message;
10-
try {
11-
var response = responders[message](parsed.data);
12-
}
13-
catch (err) {
14-
var error = { method: message, message: err.message, stack: err.stack, details: err.details || {} };
15-
}
16-
process.send({
17-
message: message,
18-
id: parsed.id,
19-
data: response,
20-
error: error
21-
});
22-
}
23-
process.on('message', function (data) {
24-
processData(data);
25-
});
1+
var workerLib = require('./workerLib');
2+
var child = new workerLib.Child();
263
var projectService = require('../main/lang/projectService');
27-
function addToResponders(func) {
28-
responders[func.name] = func;
29-
}
30-
Object.keys(projectService).filter(function (funcName) { return typeof projectService[funcName] == 'function'; }).forEach(function (funcName) { return addToResponders(projectService[funcName]); });
4+
child.registerAllFunctionsExportedFrom(projectService);

lib/worker/child.ts

+3-42
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,10 @@
11
///ts:ref=globals
22
/// <reference path="../globals.ts"/> ///ts:ref:generated
33

4-
import messages = require('./workerLib');
5-
6-
// Keepalive
7-
messages.keepAlive();
8-
9-
var responders: { [message: string]: (query: any) => any } = {};
10-
11-
// Note: child doesn't care about 'id'
12-
function processData(m: any) {
13-
var parsed: messages.Message<any> = m;
14-
if (!parsed.message || !responders[parsed.message]) {
15-
// TODO: handle this error scenario. Either the message is invalid or we do not have a registered responder
16-
return;
17-
}
18-
var message = parsed.message;
19-
try {
20-
var response = responders[message](parsed.data);
21-
} catch (err) {
22-
var error = { method: message, message: err.message, stack: err.stack, details: err.details || {} };
23-
}
24-
25-
process.send({
26-
message: message,
27-
id: parsed.id,
28-
data: response,
29-
error: error
30-
});
31-
}
32-
33-
process.on('message',(data) => {
34-
// console.log('child got:', data.toString());
35-
processData(data)
36-
});
37-
38-
///////////////// END INFRASTRUCTURE /////////////////////////////////////
4+
import workerLib = require('./workerLib');
395

6+
var child = new workerLib.Child();
407
///ts:import=projectService
418
import projectService = require('../main/lang/projectService'); ///ts:import:generated
42-
43-
function addToResponders<Query, Response>(func: (query: Query) => Response) {
44-
responders[func.name] = func;
45-
}
469
// Automatically include all functions from "projectService" as a responder
47-
Object.keys(projectService)
48-
.filter((funcName) => typeof projectService[funcName] == 'function')
49-
.forEach((funcName) => addToResponders(projectService[funcName]));
10+
child.registerAllFunctionsExportedFrom(projectService);

lib/worker/workerLib.js

+47-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@ function createId() {
88
return v.toString(16);
99
});
1010
}
11-
exports.orphanExitCode = 100;
12-
function keepAlive() {
13-
setInterval(function () {
14-
if (!process.connected) {
15-
process.exit(exports.orphanExitCode);
16-
}
17-
}, 1000);
18-
}
19-
exports.keepAlive = keepAlive;
11+
var orphanExitCode = 100;
2012
var Parent = (function () {
2113
function Parent() {
2214
this.currentListeners = {};
@@ -59,7 +51,7 @@ var Parent = (function () {
5951
});
6052
this.child.on('close', function (code) {
6153
console.log('ts worker exited with code:', code);
62-
if (code === exports.orphanExitCode) {
54+
if (code === orphanExitCode) {
6355
console.log('ts worker restarting');
6456
_this.startWorker(childJsPath, terminalError);
6557
}
@@ -108,3 +100,48 @@ var Parent = (function () {
108100
return Parent;
109101
})();
110102
exports.Parent = Parent;
103+
var Child = (function () {
104+
function Child() {
105+
var _this = this;
106+
this.responders = {};
107+
this.keepAlive();
108+
process.on('message', function (data) {
109+
_this.processData(data);
110+
});
111+
}
112+
Child.prototype.keepAlive = function () {
113+
setInterval(function () {
114+
if (!process.connected) {
115+
process.exit(orphanExitCode);
116+
}
117+
}, 1000);
118+
};
119+
Child.prototype.processData = function (m) {
120+
var parsed = m;
121+
if (!parsed.message || !this.responders[parsed.message]) {
122+
return;
123+
}
124+
var message = parsed.message;
125+
try {
126+
var response = this.responders[message](parsed.data);
127+
}
128+
catch (err) {
129+
var error = { method: message, message: err.message, stack: err.stack, details: err.details || {} };
130+
}
131+
process.send({
132+
message: message,
133+
id: parsed.id,
134+
data: response,
135+
error: error
136+
});
137+
};
138+
Child.prototype.addToResponders = function (func) {
139+
this.responders[func.name] = func;
140+
};
141+
Child.prototype.registerAllFunctionsExportedFrom = function (aModule) {
142+
var _this = this;
143+
Object.keys(aModule).filter(function (funcName) { return typeof aModule[funcName] == 'function'; }).forEach(function (funcName) { return _this.addToResponders(aModule[funcName]); });
144+
};
145+
return Child;
146+
})();
147+
exports.Child = Child;

lib/worker/workerLib.ts

+57-11
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,8 @@ function createId(): string {
3131
});
3232
}
3333

34-
export var orphanExitCode = 100;
35-
36-
/** call this from the child to keep it alive while its connected and die otherwise */
37-
export function keepAlive() {
38-
setInterval(() => {
39-
// We have been orphaned
40-
if (!(<any>process).connected) {
41-
process.exit(orphanExitCode);
42-
}
43-
}, 1000);
44-
}
34+
/** Used by parent and child for keepalive */
35+
var orphanExitCode = 100;
4536

4637
/** The parent */
4738
export class Parent {
@@ -160,3 +151,58 @@ export class Parent {
160151
}
161152
}
162153

154+
export class Child {
155+
private responders: { [message: string]: (query: any) => any } = {};
156+
157+
constructor() {
158+
// Keep alive
159+
this.keepAlive();
160+
161+
// Start listening
162+
process.on('message',(data) => {
163+
this.processData(data);
164+
});
165+
}
166+
167+
/** keep the child process alive while its connected and die otherwise */
168+
private keepAlive() {
169+
setInterval(() => {
170+
// We have been orphaned
171+
if (!(<any>process).connected) {
172+
process.exit(orphanExitCode);
173+
}
174+
}, 1000);
175+
}
176+
177+
// Note: child doesn't care about 'id'
178+
private processData(m: any) {
179+
var parsed: Message<any> = m;
180+
if (!parsed.message || !this.responders[parsed.message]) {
181+
// TODO: handle this error scenario. Either the message is invalid or we do not have a registered responder
182+
return;
183+
}
184+
var message = parsed.message;
185+
try {
186+
var response = this.responders[message](parsed.data);
187+
} catch (err) {
188+
var error = { method: message, message: err.message, stack: err.stack, details: err.details || {} };
189+
}
190+
191+
process.send({
192+
message: message,
193+
id: parsed.id,
194+
data: response,
195+
error: error
196+
});
197+
}
198+
199+
private addToResponders<Query, Response>(func: (query: Query) => Response) {
200+
this.responders[func.name] = func;
201+
}
202+
203+
registerAllFunctionsExportedFrom(aModule: any) {
204+
Object.keys(aModule)
205+
.filter((funcName) => typeof aModule[funcName] == 'function')
206+
.forEach((funcName) => this.addToResponders(aModule[funcName]));
207+
}
208+
}

0 commit comments

Comments
 (0)