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
74 changes: 72 additions & 2 deletions emitter.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,93 @@
module.exports = function () {
var collection = {};
var countSeveral = 0;
var countEmit = 0;
var countThrough;

return {
/**
* Наполняет collection данными про вызванное событие и соотвествующие ему данные
* @param eventName {String}
* @param student {Object}
* @param callback {Function}
*/
on: function (eventName, student, callback) {
if (!collection[eventName]) {
collection[eventName] = [];
}

var eventItem = {
student: student,
callback: callback.bind(student)
};

if (eventName.indexOf('.') !== -1) {
var parentName = eventName.split('.')[0];

Choose a reason for hiding this comment

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

В идеале бы сделать поддержку имен сколь угодно большой степени вложенности.

var parentEventItems = collection[parentName];

Choose a reason for hiding this comment

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

Почему ты считаешь что родительское событие уже есть в коллекции?
Может ли так случиться что его нет?


parentEventItems.forEach(function (item) {
if (item.student === student) {
eventItem.callbackParent = item.callback;

Choose a reason for hiding this comment

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

А если будут два подписчика типа:

lecturer.on('slide.funny', daria, function () {
    console.log('Новый смешной слайд!');
});
lecturer.on('slide.sad', daria, function () {
    console.log('Новый грустный слайд!');
});

то при вызове события slide только один колбек сработает?

}
});
}
collection[eventName].push(eventItem);
},

/**
* Удаляет оъект из collection
* @param eventName {String}
* @param student {Object}
*/
off: function (eventName, student) {
var events;
if (eventName.indexOf('.') === -1) {
events = Object.keys(collection).filter(function (name) {
return name.indexOf(eventName) !== -1;

Choose a reason for hiding this comment

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

А почему тут indexOf а не точное соответствие?

});
} else {
events = [eventName];
}

events.forEach(function (event) {
collection[event].forEach(function (item, i) {
if (item.student === student) {
collection[event].splice(i, 1);
}
});
});

Choose a reason for hiding this comment

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

По коду не понял как работает подписка.
Объясни на словах

},

/**
* Вызывает колбеки, которые в collection соотвествуют полученному имени события
* @param eventName {String}
*/
emit: function (eventName) {
countEmit++;

if (countSeveral && countEmit > countSeveral) {
return;
}

if (countThrough && countEmit % countThrough !== 0) {
return;
}

var eventItems = collection[eventName];
eventItems && eventItems.forEach(function (item) {
item.callback();
item.callbackParent && item.callbackParent();
});
},

several: function (eventName, student, callback, n) {

this.on(eventName, student, callback);
countSeveral = isNaN(n) ? 0 : n;
},

through: function (eventName, student, callback, n) {

this.on(eventName, student, callback);
countThrough = n;
}
};
};