From 9d47dc0f7e1f485f19d58392cffe7a2df0edacb6 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 22 Apr 2019 17:40:09 +0400 Subject: [PATCH] =?UTF-8?q?Task=20JS-9=20completed=20[=D0=A1=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D1=8C=D0=B5=D0=B2]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7dca45f..bdb1824 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,17 @@ module.exports = { - + actions: [], /** * @param {String} event * @param {Object} subscriber * @param {Function} handler */ on: function (event, subscriber, handler) { - + this.actions.push({ + event, + subscriber, + handler + }) + return this; }, /** @@ -14,13 +19,23 @@ module.exports = { * @param {Object} subscriber */ off: function (event, subscriber) { - + for (var i = this.actions.length -1; i > 0; i--) { + if ((this.actions[i].event === event) && (this.actions[i].subscriber === subscriber)) { + this.actions.splice(i, 1); + } + } + return this }, /** * @param {String} event */ emit: function (event) { - + for (var i = 0; i < this.actions.length; i++) { + if (this.actions[i].event === event) { + this.actions[i].handler.call(this.actions[i].subscriber) + } + } + return this } };