Skip to content

Commit

Permalink
fix(core): Record DependableTrait directly on instance (#2962)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RomainMuller authored Jun 20, 2019
1 parent 5b5ca48 commit e535929
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/@aws-cdk/cdk/lib/dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class ConcreteDependable implements IDependable {
}
}

const DEPENDABLE_SYMBOL = Symbol.for('@aws-cdk/core.DependableTrait');

/**
* Trait for IDependable
*
Expand Down Expand Up @@ -68,27 +70,25 @@ 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<IDependable, DependableTrait>();

/**
* The set of constructs that form the root of this dependable
*
* All resources under all returned constructs are included in the ordering
* dependency.
*/
public abstract readonly dependencyRoots: IConstruct[];
}
}

0 comments on commit e535929

Please sign in to comment.