Skip to content

Commit 726060c

Browse files
authored
fix(mixins-preview): apply mixins in order (#36847)
### Issue # (if applicable) Related RFC: aws/aws-cdk-rfcs#824 ### Reason for this change Aligns the implementation with the latest version of the [Mixins RFC](aws/aws-cdk-rfcs#824). The `.with()` method previously iterated over constructs first, then mixins. This meant that mixins were not guaranteed to be applied in the order they were passed to the method. Additionally, if a mixin added new constructs to the tree, subsequent mixins in the same call could unexpectedly visit those newly added constructs. ### Description of changes Changed the loop order so that mixins are applied in order. The list of constructs is now captured at the start of the call, ensuring that constructs added by a mixin will not be visited by subsequent mixins. Users who need subsequent mixins to apply to added constructs can use multiple `.with()` calls. Also improved variable naming for clarity (`c` → `construct`, `m` → `mixin`). ### Describe any new or updated permissions being added N/A ### Description of how you validated changes Existing tests pass. This behavior is explicitly tested in aws/constructs#2843. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 4914dad commit 726060c

File tree

1 file changed

+12
-5
lines changed
  • packages/@aws-cdk/mixins-preview/lib

1 file changed

+12
-5
lines changed

packages/@aws-cdk/mixins-preview/lib/with.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ declare module 'constructs' {
1414
}
1515

1616
// Hack the prototype to add .with() method
17-
(Construct.prototype as any).with = function(this: IConstruct, ...mixin: IMixin[]): IConstruct {
18-
for (const c of this.node.findAll()) {
19-
for (const m of mixin) {
20-
if (m.supports(c)) {
21-
applyMixin(c, m);
17+
// Changed the loop order so that mixins are applied in order.
18+
// The list of constructs is now captured at the start of the call,
19+
// ensuring that constructs added by a mixin will not be visited by subsequent mixins.
20+
//
21+
// This code is a hard copy of code from this PR https://github.com/aws/constructs/pull/2843
22+
// and is intended to be removed once the code is available in upstream.
23+
(Construct.prototype as any).with = function(this: IConstruct, ...mixins: IMixin[]): IConstruct {
24+
const allConstructs = this.node.findAll();
25+
for (const mixin of mixins) {
26+
for (const construct of allConstructs) {
27+
if (mixin.supports(construct)) {
28+
applyMixin(construct, mixin);
2229
}
2330
}
2431
}

0 commit comments

Comments
 (0)