player.js 3.89 KB
"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;