UNPKG

1.64 kBJavaScriptView Raw
1/*
2 * Copyright 2012 Amadeus s.a.s.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var packageJson = require('../../package.json');
17
18var 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
31JsonConsole.prototype = {};
32
33JsonConsole.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
44JsonConsole.prototype.addResult = JsonConsole.prototype.send;
45
46JsonConsole.prototype.onHeartbeat = function (value) {
47 this._heartbeatTimeout = null;
48 this.send({
49 event: 'heartbeat'
50 });
51};
52
53module.exports = JsonConsole;