player.js
3.89 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
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
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 !== null && _super.apply(this, arguments) || this;
_this._minutesReplication = 5;
_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;
return this.programRepository.replicate()
.then(function () {
_this._currentReplicationCounter = 0;
_this.trigger(_this.triggerProgramItemId, util_1.Util.calculateNextMinute());
})
.catch(function () {
_this.trigger(_this.triggerReplication, _this.replicationRetry);
});
};
Player.prototype.triggerProgramItemId = function () {
var _this = this;
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 != _this._currentProgramItemId) {
_this._currentProgramItemId = programItemId;
_this.emit('play', programItemId);
}
else if (_this._currentReplicationCounter >= _this._minutesReplication) {
_this.triggerReplication();
}
else {
_this.trigger(_this.triggerProgramItemId, util_1.Util.calculateNextMinute());
}
});
};
Player.prototype.trigger = function (func, milliseconds) {
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;