Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,90 @@
* Сделано задание на звездочку
* Реализованы методы several и through
*/
getEmitter.isStar = true;
getEmitter.isStar = false;
module.exports = getEmitter;


function getAllEvents(event) {
var splitted = event.split('.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Название не отражает, что лежит в переменной

var events = [splitted[0]];
for (var i = 1; i < splitted.length; i++) {
events.push([events[i - 1], splitted[i]].join('.'));
}

return events;
}

/**
* Возвращает новый emitter
* @returns {Object}
*/
function getEmitter() {
return {
_events: [],

/**
* Подписаться на событие
* @param {String} event
* @param {Object} context
* @param {Function} handler
* @returns {Object}
*/
on: function (event, context, handler) {
console.info(event, context, handler);
if (!this._events.hasOwnProperty(event)) {
this._events[event] = [];
}
this._events[event].push({
context: context,
handler: handler.bind(context)
});

return this;
},

/**
* Отписаться от события
* @param {String} event
* @param {Object} context
* @returns {Object}
*/
off: function (event, context) {
console.info(event, context);
var eventsToOff = Object.keys(this._events).filter(
function (key) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай переименуем key

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и еще эту строку можно к предыдущей приписать, так обычно пишут

return key === event ||
(key.length > event.length &&
key.substr(0, event.length + 1) === event + '.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вроде из условий

(key.length > event.length && key.substr(0, event.length + 1) === event + '.')

достаточно написать последнее

}
);
for (var i = 0; i < eventsToOff.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему бы не использовать forEach?

this._events[eventsToOff[i]] = this._events[eventsToOff[i]].filter(
function (item) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут item переименовать надо

return item.context !== context;
}
);
}

return this;
},

/**
* Уведомить о событии
* @param {String} event
* @returns {Object}
*/
emit: function (event) {
console.info(event);
var events = getAllEvents(event).reverse();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если ты сплитишь event, а потом реверсишь, то почему бы сразу не пойти с конца?

for (var i = 0; i < events.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и тут вроде forEach хорошо впишется, тогда не придется создавать новую переменную строчкой ниже

var currEvent = events[i];
if (!this._events[currEvent]) {
continue;
}
this._events[currEvent].forEach(function (item) {
item.handler();
});
}

return this;
},

/**
Expand Down