import { Injectable } from '@angular/core';
/**
 * Notifier timer service
 *
 * This service acts as a timer, needed due to the still rather limited setTimeout JavaScript API. The timer service can start and stop a
 * timer. Furthermore, it can also pause the timer at any time, and resume later on. The timer API workd promise-based.
 */
export var NotifierTimerService = (function () {
    /**
     * Constructor
     */
    function NotifierTimerService() {
        this.now = 0;
        this.remaining = 0;
    }
    /**
     * Start (or resume) the timer
     *
     * @param   {number}             duration Timer duration, in ms
     * @returns {Promise<undefined>}          Promise, resolved once the timer finishes
     */
    NotifierTimerService.prototype.start = function (duration) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            // For the first run ...
            _this.remaining = duration;
            // Setup, then start the timer
            _this.finishPromiseResolver = resolve;
            _this.continue();
        });
    };
    /**
     * Pause the timer
     */
    NotifierTimerService.prototype.pause = function () {
        clearTimeout(this.timerId);
        this.remaining -= new Date().getTime() - this.now;
    };
    /**
     * Continue the timer
     */
    NotifierTimerService.prototype.continue = function () {
        var _this = this;
        this.now = new Date().getTime();
        this.timerId = setTimeout(function () {
            _this.finish();
        }, this.remaining);
    };
    /**
     * Stop the timer
     */
    NotifierTimerService.prototype.stop = function () {
        clearTimeout(this.timerId);
        this.remaining = 0;
    };
    /**
     * Finish up the timeout by resolving the timer promise
     */
    NotifierTimerService.prototype.finish = function () {
        this.finishPromiseResolver();
    };
    NotifierTimerService.decorators = [
        { type: Injectable },
    ];
    /** @nocollapse */
    NotifierTimerService.ctorParameters = function () { return []; };
    return NotifierTimerService;
}());
//# sourceMappingURL=notifier-timer.service.js.map