player.js
4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var events_1 = require("events");
var util_1 = require("./util");
var STATE_START = "start";
var STATE_STOP = "stop";
var Player = (function (_super) {
__extends(Player, _super);
function Player() {
var _this = _super.call(this) || this;
_this._minutesReplication = 3;
_this._replicationRetry = 10000;
_this._currentProgramItemId = '';
_this._currentReplicationCounter = 0;
_this._state = STATE_STOP;
return _this;
}
Object.defineProperty(Player.prototype, "state", {
set: function (st) {
this._state = st;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Player.prototype, "programManager", {
get: function () {
return this._programManager;
},
set: function (pm) {
this._programManager = pm;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Player.prototype, "programRepository", {
get: function () {
return this._programRepository;
},
set: function (pr) {
this._programRepository = pr;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Player.prototype, "minutesReplication", {
get: function () {
return this._minutesReplication;
},
set: function (mr) {
this._minutesReplication = mr;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Player.prototype, "replicationRetry", {
get: function () {
return this._replicationRetry;
},
set: function (rr) {
this._replicationRetry = rr;
},
enumerable: true,
configurable: true
});
Player.prototype.triggerReplication = function () {
var _this = this;
console.info("digsig-player-service: trigger replication");
return this.programRepository.replicate()
.then(function (changes) {
_this._currentReplicationCounter = 0;
_this.trigger(function () { _this.triggerProgramItemId(changes); });
})
.catch(function (error) {
_this.trigger(function () { _this.triggerReplication(); }, _this.replicationRetry);
_this.emit('error', error);
});
};
Player.prototype.triggerProgramItemId = function (changes) {
var _this = this;
if (changes === void 0) { changes = false; }
console.info("digsig-player-service: trigger program item id");
this.programManager.getCurrentProgramItemId()
.then(function (programItemId) {
_this._currentReplicationCounter++;
// if there is a new program item id trigger play
// else (1) calculate next potential program change point
// or (2) trigger replication
if (programItemId && (programItemId != _this._currentProgramItemId || changes)) {
_this._currentProgramItemId = programItemId;
_this.emit('play', programItemId);
}
if (_this._currentReplicationCounter >= _this._minutesReplication) {
_this.triggerReplication();
}
else {
_this.trigger(function () { _this.triggerProgramItemId(); }, util_1.Util.calculateNextMinute());
}
});
};
Player.prototype.trigger = function (func, milliseconds) {
if (milliseconds === void 0) { milliseconds = 0; }
if (this._state === STATE_START) {
setTimeout(function () { func(); }, milliseconds);
}
};
Player.prototype.start = function () {
if (this._state === STATE_STOP) {
this.triggerReplication();
this._state = STATE_START;
}
};
Player.prototype.stop = function () {
this._state = STATE_STOP;
};
return Player;
}(events_1.EventEmitter));
exports.Player = Player;
//# sourceMappingURL=player.js.map