smarttime/dist/smarttime.classes.timer.js

46 lines
3.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smarttime.plugins");
const smarttime_classes_timestamp_1 = require("./smarttime.classes.timestamp");
class Timer {
constructor(timeInMillisecondsArg) {
/**
* the state of the timer
*/
this.state = 'initiated';
// a deferred triggeted when Timer has completed
this.completedDeferred = plugins.smartq.defer();
this.timeInMilliseconds = timeInMillisecondsArg;
this.completed = this.completedDeferred.promise;
}
get timeLeft() {
return this.timeInMilliseconds - this.pausedAt.change;
}
/**
* starts the timer
*/
start() {
if (!this.startedAt) {
this.currentTimeout = setTimeout(() => {
this.completedDeferred.resolve();
}, this.timeInMilliseconds);
this.startedAt = new smarttime_classes_timestamp_1.TimeStamp();
}
else {
throw new Error('timer has been started before. Please use resume instead');
}
}
pause() {
clearTimeout(this.currentTimeout);
this.pausedAt = smarttime_classes_timestamp_1.TimeStamp.fromTimeStamp(this.startedAt);
}
resume() {
if (this.startedAt) {
this.currentTimeout = setTimeout(() => {
this.completedDeferred.resolve();
}, this.timeLeft);
}
}
}
exports.Timer = Timer;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnR0aW1lLmNsYXNzZXMudGltZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHRpbWUuY2xhc3Nlcy50aW1lci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLCtDQUErQztBQUUvQywrRUFBMEQ7QUFJMUQ7SUFxQ0UsWUFBWSxxQkFBNkI7UUEvQnpDOztXQUVHO1FBQ0ksVUFBSyxHQUFlLFdBQVcsQ0FBQztRQXlCdkMsZ0RBQWdEO1FBQ3hDLHNCQUFpQixHQUFHLE9BQU8sQ0FBQyxNQUFNLENBQUMsS0FBSyxFQUFRLENBQUM7UUFHdkQsSUFBSSxDQUFDLGtCQUFrQixHQUFHLHFCQUFxQixDQUFDO1FB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