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