From 7f09e472254260d35a9bbc6fb4c5285da5da811a Mon Sep 17 00:00:00 2001 From: Tsvetkova Valeria Date: Sun, 13 Nov 2016 16:55:47 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- emitter.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/emitter.js b/emitter.js index c17c92f..52118e1 100644 --- a/emitter.js +++ b/emitter.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализованы методы several и through */ -getEmitter.isStar = true; +getEmitter.isStar = false; module.exports = getEmitter; /** @@ -12,33 +12,76 @@ module.exports = getEmitter; * @returns {Object} */ function getEmitter() { + return { + getChildrenEvents: function (event) { + var events = event.split('.'); + for (var i = 1; i < events.length; i++) { + events[i] = events[i - 1].concat('.').concat(events[i]); + } + + return events.reverse(); + }, + + subscriptions: {}, + /** * Подписаться на событие * @param {String} event * @param {Object} context * @param {Function} handler + * @returns {Object} */ on: function (event, context, handler) { - console.info(event, context, handler); + + if (!this.subscriptions.hasOwnProperty(event)) { + this.subscriptions[event] = []; + } + + this.subscriptions[event].push({ + context: context, + handler: handler.bind(context) + }); + + return this; }, /** * Отписаться от события * @param {String} event * @param {Object} context + * @returns {Object} */ off: function (event, context) { - console.info(event, context); + for (var subscription in this.subscriptions) { + if (this.subscriptions.hasOwnProperty(subscription) && (subscription === event || + subscription.indexOf(event.concat('.') !== -1))) { + this.subscriptions[event] = this.subscriptions[event].filter(function (name) { + return name.context !== context; + }); + } + } + + return this; }, /** * Уведомить о событии * @param {String} event + * @returns {Object} */ emit: function (event) { - console.info(event); + var subscriptions = this.subscriptions; + (this.getChildrenEvents(event)).forEach(function (subscription) { + if (subscriptions.hasOwnProperty(subscription)) { + subscriptions[subscription].forEach(function (element) { + element.handler(); + }); + } + }); + + return this; }, /** From 0842df60c523fdf49c759564f08100854751e267 Mon Sep 17 00:00:00 2001 From: Tsvetkova Valeria Date: Sun, 13 Nov 2016 22:31:44 +0500 Subject: [PATCH 2/2] emit --- emitter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emitter.js b/emitter.js index 52118e1..6e370eb 100644 --- a/emitter.js +++ b/emitter.js @@ -56,7 +56,7 @@ function getEmitter() { off: function (event, context) { for (var subscription in this.subscriptions) { if (this.subscriptions.hasOwnProperty(subscription) && (subscription === event || - subscription.indexOf(event.concat('.') !== -1))) { + subscription.indexOf(event.concat('.') === 0))) { this.subscriptions[event] = this.subscriptions[event].filter(function (name) { return name.context !== context; });