Skip to content

Commit e535929

Browse files
authored
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
1 parent 5b5ca48 commit e535929

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

packages/@aws-cdk/cdk/lib/dependency.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export class ConcreteDependable implements IDependable {
3939
}
4040
}
4141

42+
const DEPENDABLE_SYMBOL = Symbol.for('@aws-cdk/core.DependableTrait');
43+
4244
/**
4345
* Trait for IDependable
4446
*
@@ -68,27 +70,25 @@ export abstract class DependableTrait {
6870
// I would also like to reference classes (to cut down on the list of objects
6971
// we need to manage), but we can't do that either since jsii doesn't have the
7072
// concept of a class reference.
71-
DependableTrait.traitMap.set(instance, trait);
73+
(instance as any)[DEPENDABLE_SYMBOL] = trait;
7274
}
7375

7476
/**
7577
* Return the matching DependableTrait for the given class instance.
7678
*/
7779
public static get(instance: IDependable): DependableTrait {
78-
const ret = DependableTrait.traitMap.get(instance);
80+
const ret = (instance as any)[DEPENDABLE_SYMBOL];
7981
if (!ret) {
8082
throw new Error(`${instance} does not implement DependableTrait`);
8183
}
8284
return ret;
8385
}
8486

85-
private static traitMap = new WeakMap<IDependable, DependableTrait>();
86-
8787
/**
8888
* The set of constructs that form the root of this dependable
8989
*
9090
* All resources under all returned constructs are included in the ordering
9191
* dependency.
9292
*/
9393
public abstract readonly dependencyRoots: IConstruct[];
94-
}
94+
}

0 commit comments

Comments
 (0)