From bca79e9f3466e13e84d94d5efc2bea2370086e64 Mon Sep 17 00:00:00 2001 From: alin Date: Thu, 25 Jan 2024 14:00:17 +0100 Subject: [PATCH] Improve internal type hint Registry entry has now its own type definition --- packages/support/src/reflections/reflect.ts | 29 +++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/packages/support/src/reflections/reflect.ts b/packages/support/src/reflections/reflect.ts index 60f5cdf1..a5e9017e 100644 --- a/packages/support/src/reflections/reflect.ts +++ b/packages/support/src/reflections/reflect.ts @@ -6,12 +6,25 @@ import Encoder, { EncodedReflection } from "./Encoder"; import {Constructor} from "@aedart/contracts"; import { isset } from "@aedart/support/misc"; +/** + * Internal registry entry + * + * ``` + * 0 = Meta Key, + * 1 = Weak Reference to owner, + * ``` + */ +type RegistryEntry = [ + Key, + WeakRef +]; + /** * Internal registry of targets and metadata key. * - * @type {WeakMap ]>} + * @type {WeakMap} */ -const registry: WeakMap ]> = new WeakMap]>(); +const registry: WeakMap = new WeakMap(); /** * Store a "reflection" of the target element, as metadata @@ -45,7 +58,7 @@ export function reflect() // Save the key and target owner in an internal registry for target, so it can // be looked it up again... - const registryEntry: [Key, WeakRef] = [ key, new WeakRef(owner) ]; + const registryEntry: RegistryEntry = [ key, new WeakRef(owner) ]; registry.set(target, registryEntry); // In situations when a base class' method is reflected, but overwritten in a child @@ -78,7 +91,7 @@ export function reflect() */ export function getReflection(target: object): Reflection | undefined { - const entry: [ Key, WeakRef ] | undefined = findEntry(target); + const entry: RegistryEntry | undefined = findEntry(target); if (entry === undefined) { return undefined; } @@ -88,7 +101,7 @@ export function getReflection(target: object): Reflection | undefined return undefined; } - const encoded: EncodedReflection | undefined = getMeta(owner, entry[0]); + const encoded: EncodedReflection | undefined = getMeta(owner, entry[0]); if (encoded === undefined) { return undefined; } @@ -111,11 +124,11 @@ export function getReflection(target: object): Reflection | undefined * * @param {object} target * - * @returns {[Key, WeakRef] | undefined} + * @returns {RegistryEntry | undefined} */ -function findEntry(target: object): [ Key, WeakRef ] | undefined +function findEntry(target: object): RegistryEntry | undefined { - const entry: [ Key, WeakRef ] | undefined = registry.get(target); + const entry: RegistryEntry | undefined = registry.get(target); // In case that target is a class (that has metadata), it could have a parent that // has reflection defined. If so, then we attempt to obtain an entry for given