UNPKG

1.77 kBJavaScriptView Raw
1var assert = require('assert');
2var fs = require('fs');
3
4function 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
15var HFCPt = HappyFakeCompiler.prototype;
16
17HFCPt.configure = function(compilerOptions) {
18 assert(compilerOptions && typeof compilerOptions === 'object');
19
20 this.options = compilerOptions;
21};
22
23/**
24 * @public
25 *
26 * @param {String} context
27 * @param {String} resource
28 * @param {Function} done
29 *
30 * @param {String} [done.error=null]
31 * A resolving error, if any.
32 *
33 * @param {String} done.filePath
34 * The resolved file path.
35 */
36HFCPt.resolve = function(context, resource, done) {
37 this._sendMessage('resolve', {
38 context: context,
39 resource: resource
40 }, done);
41};
42
43// @private
44HFCPt._handleResponse = function(message) {
45 var callback;
46
47 if (!message.id || !this._requests[message.id]) return; // not for us
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// @private
62HFCPt._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
76module.exports = HappyFakeCompiler;
\No newline at end of file