spatial-navigation
Version:
A javascript-based implementation of Spatial Navigation.
113 lines (90 loc) • 3.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var DispatchedEvent = function () {
function DispatchedEvent() {
_classCallCheck(this, DispatchedEvent);
this.propagationStopped = false;
}
_createClass(DispatchedEvent, [{
key: "isPropagationStopped",
value: function isPropagationStopped() {
return this.propagationStopped;
}
}, {
key: "stopPropagation",
value: function stopPropagation() {
this.propagationStopped = true;
}
}]);
return DispatchedEvent;
}();
var Event = function () {
function Event(name) {
_classCallCheck(this, Event);
this.name = name;
this.handlers = {};
}
_createClass(Event, [{
key: "addHandler",
value: function addHandler(handler) {
var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
if (!(priority in this.handlers)) {
this.handlers[String(priority)] = [];
}
this.handlers[String(priority)].push(handler);
}
}, {
key: "removeHandler",
value: function removeHandler(handler) {
var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
// TODO why we need to create new array by property?
if (!(priority in this.handlers)) {
this.handlers[String(priority)] = [];
}
var handlerIndex = this.handlers[priority].indexOf(handler);
if (~handlerIndex) {
// TODO why return result
return this.handlers[priority].splice(handlerIndex, 1);
}
}
/**
* Call all handlers in all priorities
*
* @param eventArgs
*/
// TODO why whe not using ...eventArgs? In EventAggregator we passing ...eventArgs, so here we use only first parameter
}, {
key: "call",
value: function call(eventArgs) {
if (!Object.keys(this.handlers).length) {
return;
}
var dispatchedEvent = new DispatchedEvent();
for (var priority in this.handlers) {
if (!this.handlers.hasOwnProperty(priority)) {
continue;
}
// TODO why isPropagationStopped used twice? Is it if propagation will be stopped in time of iteration?
if (dispatchedEvent.isPropagationStopped()) {
continue;
}
this.handlers[priority].forEach(function (handler) {
// TODO why isPropagationStopped used twice?
// if (dispatchedEvent.isPropagationStopped()) {
// break
// }
// TODO why whe not using ...eventArgs?
// TODO why we passing dispatchedEvent? We never use it!?
handler(eventArgs, dispatchedEvent);
});
}
}
}]);
return Event;
}();
exports.default = Event;
module.exports = exports["default"];