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
48 changes: 44 additions & 4 deletions emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,81 @@
* Сделано задание на звездочку
* Реализованы методы several и through
*/
getEmitter.isStar = true;
getEmitter.isStar = false;
module.exports = getEmitter;

/**
* Возвращает новый emitter
* @returns {Object}
*/
function getEmitter() {
var students = [];

return {

/**
* Подписаться на событие
* @param {String} event
* @param {Object} context
* @param {Function} handler
* @returns {Object}
*/
on: function (event, context, handler) {
console.info(event, context, handler);
students.push({ event: event,
context: context,
handler: handler }
);

return this;
},

/**
* Отписаться от события
* @param {String} event
* @param {Object} context
* @returns {Object}
*/
off: function (event, context) {
console.info(event, context);
if (typeof (event) === 'string' && typeof (context) === 'object') {

Choose a reason for hiding this comment

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

Почему ты делаешь проверку именно так?
Что будет, если context будет объектом, но не валидным?

students = students.filter(function (subscriber) {

Choose a reason for hiding this comment

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

Лишняя строка

return (subscriber.context !== context || subscriber.event !== event) &&
!(subscriber.event.indexOf(event + '.') === 0);
});
}

return this;
},

/**
* Уведомить о событии
* @param {String} event
* @returns {Object}
*/
emit: function (event) {
console.info(event);
var emitEvent = [];
var splitEvent = event.split('.');
var flag = 1;

Choose a reason for hiding this comment

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

Непонятно из названия, для чего конкретно этот флаг.


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

Choose a reason for hiding this comment

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

Может, стоит попробовать переписать этот блок поаккуратнее? Я сходу не могу понять, что у тебя тут происходит

var event_ = splitEvent[0];
for (var j = 1; j < flag; j++) {
event_ += '.' + splitEvent[j];
}
flag++;
emitEvent.push(event_);
}
emitEvent.reverse();

emitEvent.forEach(function (eventt) {
students.forEach(function (subscriber) {
if (subscriber.event === eventt) {
subscriber.handler.call(subscriber.context);
}
});
});

return this;
},

/**
Expand Down