From 5b14b6b6c127bc0dcb1093a16bb01581844bd4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Kr=C3=B6ner?= Date: Fri, 16 Aug 2024 16:55:44 +0200 Subject: [PATCH] store transformers alongside attribute names in metadata --- src/decorators.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/decorators.ts b/src/decorators.ts index 3b2aea1..0e3eeb2 100644 --- a/src/decorators.ts +++ b/src/decorators.ts @@ -9,7 +9,7 @@ import { } from "./types.js"; type Metadata = { - attributes: Set; + attributes: Map>; methods: WeakMap, Method>; }; @@ -17,23 +17,19 @@ type Metadata = { function getMetadata( key: "attributes", context: { readonly metadata: DecoratorMetadata }, -): Set; +): Metadata["attributes"]; function getMetadata( key: "methods", context: { readonly metadata: DecoratorMetadata }, -): WeakMap, Method>; +): Metadata["methods"]; function getMetadata( key: "attributes" | "methods", context: { readonly metadata: DecoratorMetadata }, -): any { - const metadata = ((context.metadata[METADATA] as Metadata) ??= { - attributes: new Set(), +): Metadata["methods" | "attributes"] { + return ((context.metadata[METADATA] as Metadata) ??= { + attributes: new Map(), methods: new WeakMap(), - }); - if (key === "attributes") { - return metadata.attributes; - } - return metadata.methods; + })[key]; } // Explained in @enhance() @@ -113,7 +109,7 @@ export function enhance(): ( static get observedAttributes(): string[] { return [ ...originalObservedAttributes, - ...getMetadata("attributes", context), + ...getMetadata("attributes", context).keys(), ]; } @@ -684,7 +680,7 @@ export function attr( // Add the name to the set of all observed attributes, even if "reflective" // is false. The content attribute must in all cases be observed to enable // the message bus to emit events. - getMetadata("attributes", context).add(contentAttrName); + getMetadata("attributes", context).set(contentAttrName, transformer); // If the attribute needs to be observed and the accessor initializes, // register the attribute handler callback with the current element