-
Notifications
You must be signed in to change notification settings - Fork 59
Лебедев Никита #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Лебедев Никита #54
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
| 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)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Функция |
||
|
|
||
| return this; | ||
| }, | ||
|
|
||
| /** | ||
| * Уведомить о событии | ||
| * @param {String} event | ||
| * @returns {Object} | ||
| */ | ||
| emit: function (event) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно удалять события |
||
| console.info(event); | ||
| var listeners = this.events[event]; | ||
|
|
||
| if (listeners) { | ||
| listeners | ||
| .filter(function (listener) { | ||
| return listener.timesCalled !== listener.times; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут проверяешь на |
||
| }) | ||
| .forEach(function (listener) { | ||
| listener.callHandler(); | ||
| }); | ||
| } | ||
|
|
||
| var subEventPosition = event.lastIndexOf('.'); | ||
| if (subEventPosition !== -1) { | ||
| event = event.substring(0, subEventPosition); | ||
| this.emit(event); | ||
| } | ||
|
|
||
| return this; | ||
| }, | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
| }, | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
| } | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Для отрицательных чисел должно работать просто как метод
on