From e535929b39fa4ec39d0a12964332b5b1102caff5 Mon Sep 17 00:00:00 2001 From: Romain Marcadier-Muller Date: Thu, 20 Jun 2019 16:57:23 +0200 Subject: [PATCH] fix(core): Record DependableTrait directly on instance (#2962) The use of the WeakMap caused difficulties for construct library authors, because they could occasionally operate on a different instance of the @aws-cdk/cdk library. Fixes #2713 --- packages/@aws-cdk/cdk/lib/dependency.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/cdk/lib/dependency.ts b/packages/@aws-cdk/cdk/lib/dependency.ts index a62ce86f49285..e64f0692ce7e4 100644 --- a/packages/@aws-cdk/cdk/lib/dependency.ts +++ b/packages/@aws-cdk/cdk/lib/dependency.ts @@ -39,6 +39,8 @@ export class ConcreteDependable implements IDependable { } } +const DEPENDABLE_SYMBOL = Symbol.for('@aws-cdk/core.DependableTrait'); + /** * Trait for IDependable * @@ -68,22 +70,20 @@ export abstract class DependableTrait { // I would also like to reference classes (to cut down on the list of objects // we need to manage), but we can't do that either since jsii doesn't have the // concept of a class reference. - DependableTrait.traitMap.set(instance, trait); + (instance as any)[DEPENDABLE_SYMBOL] = trait; } /** * Return the matching DependableTrait for the given class instance. */ public static get(instance: IDependable): DependableTrait { - const ret = DependableTrait.traitMap.get(instance); + const ret = (instance as any)[DEPENDABLE_SYMBOL]; if (!ret) { throw new Error(`${instance} does not implement DependableTrait`); } return ret; } - private static traitMap = new WeakMap(); - /** * The set of constructs that form the root of this dependable * @@ -91,4 +91,4 @@ export abstract class DependableTrait { * dependency. */ public abstract readonly dependencyRoots: IConstruct[]; -} \ No newline at end of file +}