-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
aspect.ts
58 lines (50 loc) · 1.23 KB
/
aspect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { IConstruct } from 'constructs';
const ASPECTS_SYMBOL = Symbol.for('cdk-aspects');
/**
* Represents an Aspect
*/
export interface IAspect {
/**
* All aspects can visit an IConstruct
*/
visit(node: IConstruct): void;
}
/**
* Aspects can be applied to CDK tree scopes and can operate on the tree before
* synthesis.
*/
export class Aspects {
/**
* Returns the `Aspects` object associated with a construct scope.
* @param scope The scope for which these aspects will apply.
*/
public static of(scope: IConstruct): Aspects {
let aspects = (scope as any)[ASPECTS_SYMBOL];
if (!aspects) {
aspects = new Aspects();
Object.defineProperty(scope, ASPECTS_SYMBOL, {
value: aspects,
configurable: false,
enumerable: false,
});
}
return aspects;
}
private readonly _aspects: IAspect[];
private constructor() {
this._aspects = [];
}
/**
* Adds an aspect to apply this scope before synthesis.
* @param aspect The aspect to add.
*/
public add(aspect: IAspect) {
this._aspects.push(aspect);
}
/**
* The list of aspects which were directly applied on this scope.
*/
public get all(): IAspect[] {
return [...this._aspects];
}
}