-
Notifications
You must be signed in to change notification settings - Fork 53
Natalia Makishvili #59
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?
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 |
|---|---|---|
| @@ -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]; | ||
| var parentEventItems = collection[parentName]; | ||
|
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. Почему ты считаешь что родительское событие уже есть в коллекции? |
||
|
|
||
| parentEventItems.forEach(function (item) { | ||
| if (item.student === student) { | ||
| eventItem.callbackParent = item.callback; | ||
|
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. А если будут два подписчика типа: то при вызове события |
||
| } | ||
| }); | ||
| } | ||
| 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; | ||
|
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. А почему тут |
||
| }); | ||
| } else { | ||
| events = [eventName]; | ||
| } | ||
|
|
||
| events.forEach(function (event) { | ||
| collection[event].forEach(function (item, i) { | ||
| if (item.student === student) { | ||
| collection[event].splice(i, 1); | ||
| } | ||
| }); | ||
| }); | ||
|
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. По коду не понял как работает подписка. |
||
| }, | ||
|
|
||
| /** | ||
| * Вызывает колбеки, которые в 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; | ||
| } | ||
| }; | ||
| }; | ||
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.
В идеале бы сделать поддержку имен сколь угодно большой степени вложенности.