From 75e1d4333644569d12409a09ab96362cafdb9fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=91=A8=F0=9F=8F=BC=E2=80=8D=F0=9F=92=BB=20Romain=20M?= =?UTF-8?q?arcadier-Muller?= Date: Thu, 20 Jun 2019 15:53:05 +0200 Subject: [PATCH] fix(core): Record DependableTrait directly on instance 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 +}