Skip to content

Commit 798fee3

Browse files
committed
consistent naming for parent and child. Also childQuery is now sendToIpc (as it is going to be bidirectional)
refs #51
1 parent a7a334d commit 798fee3

9 files changed

+73
-65
lines changed

lib/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"./typings/status-bar/status-bar.d.ts",
5959
"./typings/text-buffer/text-buffer.d.ts",
6060
"./typings/tsd.d.ts",
61-
"./worker/childMain.ts",
61+
"./worker/child.ts",
6262
"./worker/lib/workerLib.ts",
6363
"./worker/parent.ts",
6464
"./worker/parentResponses.ts"
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var workerLib = require('./lib/workerLib');
2-
var projectService = require('../main/lang/projectService');
32
var child = new workerLib.Child();
3+
var parentResponses = require('./parentResponses');
4+
exports.plus1 = child.sendToIpc(parentResponses.plus1);
5+
var projectService = require('../main/lang/projectService');
46
child.registerAllFunctionsExportedFromAsResponders(projectService);

lib/worker/childMain.ts renamed to lib/worker/child.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
/// <reference path="../globals.ts"/> ///ts:ref:generated
33

44
import workerLib = require('./lib/workerLib');
5-
import projectService = require('../main/lang/projectService'); ///ts:import:generated
5+
66

77
// Initiate the child logic
88
var child = new workerLib.Child();
99

10+
/////////////////////////////////////// END INFRASTRUCTURE ////////////////////////////////////////////////////
11+
12+
import parentResponses = require('./parentResponses');
13+
export var plus1 = child.sendToIpc(parentResponses.plus1);
14+
1015
// Automatically include all functions from "projectService" as a responder
16+
import projectService = require('../main/lang/projectService');
1117
child.registerAllFunctionsExportedFromAsResponders(projectService);

lib/worker/lib/workerLib.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ var RequesterResponder = (function () {
7474
delete this.currentListeners[parsed.message][parsed.id];
7575
}
7676
};
77-
RequesterResponder.prototype.childQuery = function (func) {
77+
RequesterResponder.prototype.sendToIpc = function (func) {
7878
var _this = this;
7979
var that = this;
8080
return function (data) {

lib/worker/lib/workerLib.ts

+26-26
Original file line numberDiff line numberDiff line change
@@ -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 {
154154
export 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+
}

lib/worker/parent.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var spawn = childprocess.spawn;
44
var workerLib = require('./lib/workerLib');
55
var parent = new workerLib.Parent();
66
function startWorker() {
7-
parent.startWorker(__dirname + '/childMain.js', showError);
7+
parent.startWorker(__dirname + '/child.js', showError);
88
console.log('AtomTS worker started');
99
}
1010
exports.startWorker = startWorker;
@@ -23,19 +23,19 @@ function showError(error) {
2323
}
2424
}
2525
var projectService = require('../main/lang/projectService');
26-
exports.echo = parent.childQuery(projectService.echo);
27-
exports.quickInfo = parent.childQuery(projectService.quickInfo);
28-
exports.build = parent.childQuery(projectService.build);
29-
exports.errorsForFileFiltered = parent.childQuery(projectService.errorsForFileFiltered);
30-
exports.getCompletionsAtPosition = parent.childQuery(projectService.getCompletionsAtPosition);
31-
exports.emitFile = parent.childQuery(projectService.emitFile);
32-
exports.regenerateProjectGlob = parent.childQuery(projectService.regenerateProjectGlob);
33-
exports.formatDocument = parent.childQuery(projectService.formatDocument);
34-
exports.formatDocumentRange = parent.childQuery(projectService.formatDocumentRange);
35-
exports.getDefinitionsAtPosition = parent.childQuery(projectService.getDefinitionsAtPosition);
36-
exports.updateText = parent.childQuery(projectService.updateText);
37-
exports.errorsForFile = parent.childQuery(projectService.errorsForFile);
38-
exports.getSignatureHelps = parent.childQuery(projectService.getSignatureHelps);
39-
exports.getRenameInfo = parent.childQuery(projectService.getRenameInfo);
26+
exports.echo = parent.sendToIpc(projectService.echo);
27+
exports.quickInfo = parent.sendToIpc(projectService.quickInfo);
28+
exports.build = parent.sendToIpc(projectService.build);
29+
exports.errorsForFileFiltered = parent.sendToIpc(projectService.errorsForFileFiltered);
30+
exports.getCompletionsAtPosition = parent.sendToIpc(projectService.getCompletionsAtPosition);
31+
exports.emitFile = parent.sendToIpc(projectService.emitFile);
32+
exports.regenerateProjectGlob = parent.sendToIpc(projectService.regenerateProjectGlob);
33+
exports.formatDocument = parent.sendToIpc(projectService.formatDocument);
34+
exports.formatDocumentRange = parent.sendToIpc(projectService.formatDocumentRange);
35+
exports.getDefinitionsAtPosition = parent.sendToIpc(projectService.getDefinitionsAtPosition);
36+
exports.updateText = parent.sendToIpc(projectService.updateText);
37+
exports.errorsForFile = parent.sendToIpc(projectService.errorsForFile);
38+
exports.getSignatureHelps = parent.sendToIpc(projectService.getSignatureHelps);
39+
exports.getRenameInfo = parent.sendToIpc(projectService.getRenameInfo);
4040
var parentResponses = require('./parentResponses');
4141
parent.registerAllFunctionsExportedFromAsResponders(parentResponses);

lib/worker/parent.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import tsconfig = require('../main/tsconfig/tsconfig');
1111

1212
var parent = new workerLib.Parent();
1313
export function startWorker() {
14-
parent.startWorker(__dirname + '/childMain.js', showError);
14+
parent.startWorker(__dirname + '/child.js', showError);
1515
console.log('AtomTS worker started')
1616
}
1717

@@ -37,21 +37,21 @@ function showError(error: Error) {
3737
///ts:import=projectService
3838
import projectService = require('../main/lang/projectService'); ///ts:import:generated
3939

40-
export var echo = parent.childQuery(projectService.echo);
41-
export var quickInfo = parent.childQuery(projectService.quickInfo);
42-
export var build = parent.childQuery(projectService.build);
43-
export var errorsForFileFiltered = parent.childQuery(projectService.errorsForFileFiltered);
44-
export var getCompletionsAtPosition = parent.childQuery(projectService.getCompletionsAtPosition);
45-
export var emitFile = parent.childQuery(projectService.emitFile);
46-
export var regenerateProjectGlob = parent.childQuery(projectService.regenerateProjectGlob);
47-
export var formatDocument = parent.childQuery(projectService.formatDocument);
48-
export var formatDocumentRange = parent.childQuery(projectService.formatDocumentRange);
49-
export var getDefinitionsAtPosition = parent.childQuery(projectService.getDefinitionsAtPosition);
50-
export var updateText = parent.childQuery(projectService.updateText);
51-
export var errorsForFile = parent.childQuery(projectService.errorsForFile);
52-
export var getSignatureHelps = parent.childQuery(projectService.getSignatureHelps);
53-
export var getRenameInfo = parent.childQuery(projectService.getRenameInfo);
40+
export var echo = parent.sendToIpc(projectService.echo);
41+
export var quickInfo = parent.sendToIpc(projectService.quickInfo);
42+
export var build = parent.sendToIpc(projectService.build);
43+
export var errorsForFileFiltered = parent.sendToIpc(projectService.errorsForFileFiltered);
44+
export var getCompletionsAtPosition = parent.sendToIpc(projectService.getCompletionsAtPosition);
45+
export var emitFile = parent.sendToIpc(projectService.emitFile);
46+
export var regenerateProjectGlob = parent.sendToIpc(projectService.regenerateProjectGlob);
47+
export var formatDocument = parent.sendToIpc(projectService.formatDocument);
48+
export var formatDocumentRange = parent.sendToIpc(projectService.formatDocumentRange);
49+
export var getDefinitionsAtPosition = parent.sendToIpc(projectService.getDefinitionsAtPosition);
50+
export var updateText = parent.sendToIpc(projectService.updateText);
51+
export var errorsForFile = parent.sendToIpc(projectService.errorsForFile);
52+
export var getSignatureHelps = parent.sendToIpc(projectService.getSignatureHelps);
53+
export var getRenameInfo = parent.sendToIpc(projectService.getRenameInfo);
5454

5555
// Automatically include all functions from "parentResponses" as a responder
5656
import parentResponses = require('./parentResponses');
57-
parent.registerAllFunctionsExportedFromAsResponders(parentResponses);
57+
parent.registerAllFunctionsExportedFromAsResponders(parentResponses);

lib/worker/parentResponses.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
function plus1(query) {
2-
return { num: query.num + 1 };
2+
return Promise.resolve({ num: query.num + 1 });
33
}
44
exports.plus1 = plus1;

lib/worker/parentResponses.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// Functions that the parent allows the child to query
22

3-
export function plus1(query: { num: number }): { num: number } {
4-
return { num: query.num + 1 };
5-
}
3+
export function plus1(query: { num: number }): Promise<{ num: number }> {
4+
return Promise.resolve({ num: query.num + 1 });
5+
}

0 commit comments

Comments
 (0)