teko-oauth2
Version:
Teko Identity OAuth 2 Javascript Library for Web App Client
68 lines (54 loc) • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
require("regenerator-runtime/runtime");
var _constants = require("./constants");
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
// A count down timer (like setTimeout),
// using interval check to fix limitation of setTimeout
// when computer goes to sleep.
var Timer = /*#__PURE__*/function () {
function Timer(name, callback) {
var frequency = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _constants.DEFAULT_TIMER_CHECK_FREQUENCY;
_classCallCheck(this, Timer);
this._name = name;
this._callback = callback;
this._frequency = frequency;
this._timer = null;
this._expiration = null;
}
_createClass(Timer, [{
key: "init",
value: function init(duration) {
this.cancel();
if (!duration) duration = this._frequency;
duration = parseInt(Math.max(duration, 0));
this._expiration = Date.now() + duration;
var checkFrequency = Math.min(duration, this._frequency);
this._timer = setInterval(this._checkFire.bind(this), checkFrequency);
}
}, {
key: "cancel",
value: function cancel() {
if (this._timer) {
clearInterval(this._timer);
this._timer = null;
}
}
}, {
key: "_checkFire",
value: function _checkFire() {
var diff = this._expiration - Date.now();
if (diff <= 0) {
this.cancel();
this._callback();
}
}
}]);
return Timer;
}();
exports["default"] = Timer;