1 | var assert = require('assert');
|
2 | var fs = require('fs');
|
3 |
|
4 | function HappyFakeCompiler(id, sendMessageImpl) {
|
5 | assert(typeof sendMessageImpl === 'function');
|
6 |
|
7 | this._id = id;
|
8 | this._sendMessageImpl = sendMessageImpl;
|
9 | this._requests = {};
|
10 | this._messageId = 0;
|
11 | this.options = {};
|
12 | this.inputFileSystem = fs;
|
13 | }
|
14 |
|
15 | var HFCPt = HappyFakeCompiler.prototype;
|
16 |
|
17 | HFCPt.configure = function(compilerOptions) {
|
18 | assert(compilerOptions && typeof compilerOptions === 'object');
|
19 |
|
20 | this.options = compilerOptions;
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | HFCPt.resolve = function(context, resource, done) {
|
37 | this._sendMessage('resolve', {
|
38 | context: context,
|
39 | resource: resource
|
40 | }, done);
|
41 | };
|
42 |
|
43 |
|
44 | HFCPt._handleResponse = function(message) {
|
45 | var callback;
|
46 |
|
47 | if (!message.id || !this._requests[message.id]) return;
|
48 |
|
49 | assert(message.payload.hasOwnProperty('error'),
|
50 | "Compiler message payload must contain an @error field!");
|
51 |
|
52 | assert(message.payload.hasOwnProperty('result'),
|
53 | "Compiler message payload must contain a @result field!");
|
54 |
|
55 | callback = this._requests[message.id];
|
56 | delete this._requests[message.id];
|
57 |
|
58 | callback(message.payload.error, message.payload.result);
|
59 | };
|
60 |
|
61 |
|
62 | HFCPt._sendMessage = function(type, payload, done) {
|
63 | var messageId = [ this._id, ++this._messageId ].join('__');
|
64 |
|
65 | this._requests[messageId] = done;
|
66 | this._sendMessageImpl({
|
67 | name: 'COMPILER_REQUEST',
|
68 | data: {
|
69 | id: messageId,
|
70 | type: type,
|
71 | payload: payload,
|
72 | }
|
73 | });
|
74 | };
|
75 |
|
76 | module.exports = HappyFakeCompiler; |
\ | No newline at end of file |