From 1224baf977e9f9699b025baa354c4fb99fbc5e27 Mon Sep 17 00:00:00 2001 From: Edoardo Cavazza Date: Thu, 16 Nov 2023 15:51:12 +0100 Subject: [PATCH] Refactor initialize method --- .changeset/nasty-rockets-return.md | 5 +++ src/Component.ts | 49 ++++++++++++++++-------------- 2 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 .changeset/nasty-rockets-return.md diff --git a/.changeset/nasty-rockets-return.md b/.changeset/nasty-rockets-return.md new file mode 100644 index 00000000..56d846df --- /dev/null +++ b/.changeset/nasty-rockets-return.md @@ -0,0 +1,5 @@ +--- +'@chialab/dna': patch +--- + +Move element initialization to `inizitialize` method. diff --git a/src/Component.ts b/src/Component.ts index 2822a0c5..1e5e8829 100644 --- a/src/Component.ts +++ b/src/Component.ts @@ -32,12 +32,12 @@ import { getRootContext, internalRender, render } from './render'; /** * A symbol which identify components. */ -const COMPONENT_SYMBOL: unique symbol = Symbol(); +const COMPONENT_SYMBOL: unique symbol = Symbol('component'); /** * A symbol which identify constructed elements. */ -const CONSTRUCTED_SYMBOL: unique symbol = Symbol(); +const CONSTRUCTED_SYMBOL: unique symbol = Symbol('constructed'); /** * An augmented node with component flags. @@ -174,49 +174,55 @@ export const extend = ({ + const computedListeners = getListeners(this).map((listener) => ({ ...listener, - callback: listener.callback.bind(element), + callback: listener.callback.bind(this), })); - setListeners(element, computedListeners); + setListeners(this, computedListeners); for (let i = 0, len = computedListeners.length; i < len; i++) { const { event, target, selector, callback, options } = computedListeners[i]; if (!target) { - element.delegateEventListener(event, selector, callback, options); + this.delegateEventListener(event, selector, callback, options); } } // setup properties - const computedProperties = getProperties(element); + const computedProperties = getProperties(this); for (const propertyKey in computedProperties) { - delete element[propertyKey]; + delete this[propertyKey]; const property = computedProperties[propertyKey]; if (typeof property.initializer === 'function') { - element[propertyKey] = property.initializer.call(element); + this[propertyKey] = property.initializer.call(this); } else if (typeof property.defaultValue !== 'undefined') { - element[propertyKey] = property.defaultValue; + this[propertyKey] = property.defaultValue; } } - const realm = attachRealm(element); - defineProperty(element, 'realm', { + const realm = attachRealm(this); + defineProperty(this, 'realm', { value: realm, configurable: true, }); - realm.observe(() => element.forceUpdate()); + realm.observe(() => this.forceUpdate()); - return element; + (this as WithComponentProto)[CONSTRUCTED_SYMBOL] = true; } - /** - * The callback for initialized components. - * It runs once when the component is created, at the end of the constructor. - */ - initialize() {} - /** * Invoked each time the Component is appended into a document-connected element. * This will happen each time the node is moved, and may happen before the element's contents have been fully parsed. @@ -579,7 +585,6 @@ export function define(name: string, constructor: ComponentConstructor, options? constructor(...args: any[]) { super(...args); if (new.target === Component) { - (this as WithComponentProto)[CONSTRUCTED_SYMBOL] = true; this.initialize(); } }