@offensichtbar-codestock/es6-flex-masonry-grid
Version:
122 lines (98 loc) • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.debounce = exports.ModuleEventManager = exports.Dispatcher = exports.DispatcherEvent = void 0;
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; }
var DispatcherEvent = /*#__PURE__*/function () {
function DispatcherEvent(eventName) {
_classCallCheck(this, DispatcherEvent);
this.eventName = eventName;
this.callbacks = [];
}
_createClass(DispatcherEvent, [{
key: "registerCallback",
value: function registerCallback(callback) {
this.callbacks.push(callback);
}
}, {
key: "unregisterCallback",
value: function unregisterCallback(callback) {
var index = this.callbacks.indexOf(callback);
if (index > -1) {
this.callbacks.splice(index, 1);
}
}
}, {
key: "fire",
value: function fire(data) {
var callbacks = this.callbacks.slice(0);
callbacks.forEach(function (callback) {
callback(data);
});
}
}]);
return DispatcherEvent;
}();
exports.DispatcherEvent = DispatcherEvent;
var Dispatcher = /*#__PURE__*/function () {
function Dispatcher() {
_classCallCheck(this, Dispatcher);
this.events = {};
}
_createClass(Dispatcher, [{
key: "dispatch",
value: function dispatch(eventName, data) {
var event = this.events[eventName];
if (event) {
event.fire(data);
}
}
}, {
key: "on",
value: function on(eventName, callback) {
var event = this.events[eventName];
if (!event) {
event = new DispatcherEvent(eventName);
this.events[eventName] = event;
}
event.registerCallback(callback);
}
}, {
key: "off",
value: function off(eventName, callback) {
var event = this.events[eventName];
if (event && event.callbacks.indexOf(callback) > -1) {
event.unregisterCallback(callback);
if (event.callbacks.length === 0) {
delete this.events[eventName];
}
}
}
}]);
return Dispatcher;
}();
exports.Dispatcher = Dispatcher;
var ModuleEventManager = new Dispatcher();
/**
* Debounces function calls triggered by event fired
* @param {function} callback
* @param {number} wait - interval
* @return {function}
*/
exports.ModuleEventManager = ModuleEventManager;
var debounce = function debounce(callback, wait) {
var timeoutId = null;
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(function () {
callback.apply(null, args);
}, wait);
};
};
exports.debounce = debounce;