From e56162ff9e54117ebf19c9a6e75d9be13334c538 Mon Sep 17 00:00:00 2001 From: Benjamin Wegener Date: Sun, 19 Nov 2023 17:15:17 +0000 Subject: [PATCH] feat(core) auto register `on...` options as event listener callbacks --- lib/core/Cetra.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/core/Cetra.js b/lib/core/Cetra.js index 2f7e102..36ce009 100644 --- a/lib/core/Cetra.js +++ b/lib/core/Cetra.js @@ -21,6 +21,27 @@ class Cetra extends Emitter { constructor(options = new Map()) { super(); this.#identifier = this.uid(); + + if (!(options instanceof Map) && typeof options == 'object') { + options = new Map(Object.entries(options)); + } + + for (const key of Cetra.#defaultSettings.keys()) { + let value = options.get(key); + if (typeof value == 'undefined') { + value = Cetra.#defaultSettings.get(key); + } + this.#internals + .get('settings') + .set(key, value); + } + + + for (const [key, cb] of options) { + if (!key.startsWith('on') || typeof cb != 'function') continue; + this.on(key, cb); + } + this.#init(); } @@ -61,7 +82,7 @@ class Cetra extends Emitter { ); Cetra.#instances.set(this.#identifier, this); - this.emit('afterInit', this); + this.emit('onAfterInit', this); } /**