From 00bc20681ad17e5fcd16638b0db6cf31c2e70946 Mon Sep 17 00:00:00 2001 From: Mohmn Date: Thu, 24 Nov 2022 03:50:37 +0530 Subject: [PATCH] feat: added checks on creating events and added method for unregistering events" --- src/Map.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Map.js b/src/Map.js index 81a5710..ddc6412 100644 --- a/src/Map.js +++ b/src/Map.js @@ -1296,11 +1296,27 @@ export default class Map { } on(eventName, handler) { - //TODO check event name enum - if (handler) { - log.info('register event:', eventName) - this.events.on(eventName, handler) + const events = Object.values(Map.REGISTERED_EVENTS) + if (!events.includes(eventName)) { + throw new Error(`unrecognisable event name`) } + if (typeof handler !== 'function') { + throw new Error(`handler isnt't of type function`) + } + log.info('register event:', eventName) + this.events.on(eventName, handler) + } + + off(eventName, handler) { + const events = Object.values(Map.REGISTERED_EVENTS) + if (!events.includes(eventName)) { + throw new Error(`unrecognisable event name`) + } + if (typeof handler !== 'function') { + throw new Error(`handler isnt't of type function`) + } + log.info('register event:', eventName) + this.events.off(eventName, handler) } selectTree(tree) {