1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var packageJson = require('../../package.json');
|
17 |
|
18 | var JsonConsole = function (stdout, heartbeatsDelay) {
|
19 | this.stdout = stdout;
|
20 | if (heartbeatsDelay > 0) {
|
21 | this._heartbeatDelay = heartbeatsDelay;
|
22 | this._heartbeatFunction = this.onHeartbeat.bind(this);
|
23 | }
|
24 | this._heartbeatTimeout = null;
|
25 | this.send({
|
26 | application: packageJson.name,
|
27 | version: packageJson.version
|
28 | });
|
29 | };
|
30 |
|
31 | JsonConsole.prototype = {};
|
32 |
|
33 | JsonConsole.prototype.send = function (json) {
|
34 | if (this._heartbeatTimeout != null) {
|
35 | clearTimeout(this._heartbeatTimeout);
|
36 | this._heartbeatTimeout = null;
|
37 | }
|
38 | this.stdout.write(JSON.stringify(json));
|
39 | if (this._heartbeatFunction) {
|
40 | this._heartbeatTimeout = setTimeout(this._heartbeatFunction, this._heartbeatDelay);
|
41 | }
|
42 | };
|
43 |
|
44 | JsonConsole.prototype.addResult = JsonConsole.prototype.send;
|
45 |
|
46 | JsonConsole.prototype.onHeartbeat = function (value) {
|
47 | this._heartbeatTimeout = null;
|
48 | this.send({
|
49 | event: 'heartbeat'
|
50 | });
|
51 | };
|
52 |
|
53 | module.exports = JsonConsole;
|