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
65 changes: 60 additions & 5 deletions emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,85 @@ module.exports = getEmitter;
*/
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);
var times = arguments[3] || Infinity;
var frequency = arguments[4] || 1;

Choose a reason for hiding this comment

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

Для отрицательных чисел должно работать просто как метод on


if (!this.events[event]) {
this.events[event] = [];
}

this.events[event].push({
context: context,
handler: handler,
times: times,
timesCalled: 0,
frequency: frequency,
callHandler: function () {
if (this.timesCalled % this.frequency === 0 && this.timesCalled < this.times) {
this.handler.call(this.context);
}
this.timesCalled += 1;
}
});

return this;
},

/**
* Отписаться от события
* @param {String} event
* @param {Object} context
* @returns {Object}
*/
off: function (event, context) {
console.info(event, context);
Object.keys(this.events)
.filter(function (evt) {
return evt === event || evt.startsWith(event + '.');
})
.forEach(function (evt) {
this.events[evt] = this.events[evt].filter(function (listener) {
return listener.context !== context;
});
}.bind(this));
Copy link

@ninjagrizzly ninjagrizzly Nov 14, 2016

Choose a reason for hiding this comment

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

Функция forEach принимает вторым аргументом контекст, то есть можно писать так
forEach(function (args) {...}, this)


return this;
},

/**
* Уведомить о событии
* @param {String} event
* @returns {Object}
*/
emit: function (event) {

Choose a reason for hiding this comment

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

Можно удалять события several из общего списка после вызова их n раз

console.info(event);
var listeners = this.events[event];

if (listeners) {
listeners
.filter(function (listener) {
return listener.timesCalled !== listener.times;

Choose a reason for hiding this comment

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

Тут проверяешь на !== и еще внутри функции callHandler проверяется условие this.timesCalled < this.times. Можешь пояснить зачем так?

})
.forEach(function (listener) {
listener.callHandler();
});
}

var subEventPosition = event.lastIndexOf('.');
if (subEventPosition !== -1) {
event = event.substring(0, subEventPosition);
this.emit(event);
}

return this;
},

/**
Expand All @@ -48,9 +101,10 @@ function getEmitter() {
* @param {Object} context
* @param {Function} handler
* @param {Number} times – сколько раз получить уведомление
* @returns {Object}
*/
several: function (event, context, handler, times) {
console.info(event, context, handler, times);
return this.on(event, context, handler, times);
},

/**
Expand All @@ -60,9 +114,10 @@ function getEmitter() {
* @param {Object} context
* @param {Function} handler
* @param {Number} frequency – как часто уведомлять
* @returns {Object}
*/
through: function (event, context, handler, frequency) {
console.info(event, context, handler, frequency);
return this.on(event, context, handler, undefined, frequency);
}
};
}