time-events-manager
Version:
View and Manage Javascript's timeout and interval collections
336 lines (304 loc) • 11.4 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 5);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
;
exports.originalSetTimeout = setTimeout;
exports.originalSetInterval = setInterval;
exports.originalClearInterval = clearInterval;
exports.originalClearTimeout = clearTimeout;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
;
(function (TimeoutStatus) {
TimeoutStatus[TimeoutStatus["Scheduled"] = 0] = "Scheduled";
TimeoutStatus[TimeoutStatus["Completed"] = 1] = "Completed";
})(exports.TimeoutStatus || (exports.TimeoutStatus = {}));
var TimeoutStatus = exports.TimeoutStatus;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
;
var interval_collection_model_1 = __webpack_require__(4);
exports.intervalCollection = new interval_collection_model_1.IntervalCollection();
window.setInterval = function (handler, interval) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
return exports.intervalCollection.add(function () {
handler();
}, interval, args);
};
window.clearInterval = function (id) {
exports.intervalCollection.remove(id);
};
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
;
var timeout_collection_model_1 = __webpack_require__(6);
exports.timeoutCollection = new timeout_collection_model_1.TimeoutCollection();
window.setTimeout = function (handler, timeout) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
return exports.timeoutCollection.add(handler, timeout, args);
};
window.clearTimeout = function (id) {
exports.timeoutCollection.remove(id);
};
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
;
var override_1 = __webpack_require__(0);
var IntervalCollection = (function () {
function IntervalCollection() {
this._intervalCollection = [];
}
IntervalCollection.prototype.add = function (handler, interval) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
var id = override_1.originalSetInterval.apply(window, [handler, interval, args]);
this._intervalCollection.push({ id: id, handler: handler, interval: interval, arguments: args, timestamp: Date.now() });
return id;
};
IntervalCollection.prototype.remove = function (id) {
var intervalIndex = this._getIntervalIndexById(id);
if (intervalIndex !== -1) {
this._intervalCollection.splice(intervalIndex, 1);
}
override_1.originalClearInterval.apply(window, [id]);
};
IntervalCollection.prototype.get = function (index) {
return this._intervalCollection[index];
};
IntervalCollection.prototype.getAll = function () {
return this._intervalCollection;
};
IntervalCollection.prototype.getById = function (id) {
return this._intervalCollection[this._getIntervalIndexById(id)];
};
IntervalCollection.prototype.removeAll = function () {
this._intervalCollection.forEach(function (interval) {
override_1.originalClearInterval.apply(window, [interval.id]);
});
this._intervalCollection = [];
};
IntervalCollection.prototype._getIntervalIndexById = function (intervalId) {
for (var i = 0; i < this._intervalCollection.length; i++) {
if (this._intervalCollection[i].id === intervalId) {
return i;
}
}
return -1;
};
return IntervalCollection;
}());
exports.IntervalCollection = IntervalCollection;
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
;
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(__webpack_require__(2));
__export(__webpack_require__(3));
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
;
var override_1 = __webpack_require__(0);
var timeout_status_model_1 = __webpack_require__(1);
var timeout_model_1 = __webpack_require__(7);
var TimeoutCollection = (function () {
function TimeoutCollection() {
this._timeoutCollection = [];
}
TimeoutCollection.prototype.add = function (handler, timeout) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
var newTimeout = new timeout_model_1.Timeout(handler, timeout, args);
var id = override_1.originalSetTimeout.apply(window, [this._getWrappedHandler(newTimeout.uuid, handler), timeout, args]);
newTimeout.id = id;
this._timeoutCollection.push(newTimeout);
return id;
};
TimeoutCollection.prototype.remove = function (id) {
var timeoutIndex = this._getTimeoutIndexById(id);
if (timeoutIndex !== -1) {
this._timeoutCollection.splice(timeoutIndex, 1);
}
override_1.originalClearTimeout.apply(window, [id]);
};
TimeoutCollection.prototype.get = function (index) {
return this._timeoutCollection[index];
};
TimeoutCollection.prototype.getScheduled = function () {
return this._timeoutCollection.filter(function (value) {
return value.status === timeout_status_model_1.TimeoutStatus.Scheduled;
});
};
TimeoutCollection.prototype.getCompleted = function () {
return this._timeoutCollection.filter(function (value) {
return value.status === timeout_status_model_1.TimeoutStatus.Completed;
});
};
TimeoutCollection.prototype.getAll = function () {
return this._timeoutCollection;
};
TimeoutCollection.prototype.getById = function (id) {
return this._timeoutCollection[this._getTimeoutIndexById(id)];
};
TimeoutCollection.prototype.removeAll = function () {
this._timeoutCollection.forEach(function (timeout) {
override_1.originalClearTimeout.apply(window, [timeout.id]);
});
this._timeoutCollection = [];
};
TimeoutCollection.prototype._getWrappedHandler = function (timeoutUuid, handler) {
var _this = this;
return (function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
_this._timeoutCollection[_this._getTimeoutIndexByUuid(timeoutUuid)].status = timeout_status_model_1.TimeoutStatus.Completed;
return handler.apply(window, args);
});
};
TimeoutCollection.prototype._getTimeoutIndexById = function (timeoutId) {
for (var i = 0; i < this._timeoutCollection.length; i++) {
if (this._timeoutCollection[i].id === timeoutId) {
return i;
}
}
return -1;
};
TimeoutCollection.prototype._getTimeoutIndexByUuid = function (uuid) {
for (var i = 0; i < this._timeoutCollection.length; i++) {
if (this._timeoutCollection[i].uuid === uuid) {
return i;
}
}
return -1;
};
return TimeoutCollection;
}());
exports.TimeoutCollection = TimeoutCollection;
/***/ }),
/* 7 */
/***/ (function(module, exports, __webpack_require__) {
;
var timeout_status_model_1 = __webpack_require__(1);
var Timeout = (function () {
function Timeout(handler, timeout) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
this.uuid = this._generateUuid();
this.handler = handler;
this.timeout = timeout;
this.arguments = args;
this.id = null;
this.timestamp = Date.now();
this.status = timeout_status_model_1.TimeoutStatus.Scheduled;
}
Timeout.prototype._generateUuid = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
return Timeout;
}());
exports.Timeout = Timeout;
/***/ })
/******/ ]);
});