-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): hide dependencyRoots
from public API
#2668
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
import { IConstruct } from "./construct"; | ||
|
||
/** | ||
* A set of constructs that can be depended upon | ||
* Trait marker for classes that can be depended upon | ||
* | ||
* The presence of this interface indicates that an object has | ||
* an `IDependableTrait` implementation. | ||
* | ||
* This interface can be used to take an (ordering) dependency on a set of | ||
* constructs. An ordering dependency implies that the resources represented by | ||
* those constructs are deployed before the resources depending ON them are | ||
* deployed. | ||
*/ | ||
export interface IDependable { | ||
/** | ||
* The set of constructs that form the root of this dependable | ||
* | ||
* All resources under all returned constructs are included in the ordering | ||
* dependency. | ||
*/ | ||
readonly dependencyRoots: IConstruct[]; | ||
// Empty, this interface is a trait marker | ||
} | ||
|
||
/** | ||
|
@@ -27,17 +24,61 @@ export interface IDependable { | |
export class ConcreteDependable implements IDependable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh nice. for some reason I thought it is just introduced now. |
||
private readonly _dependencyRoots = new Array<IConstruct>(); | ||
|
||
constructor() { | ||
const self = this; | ||
DependableTrait.implement(this, { | ||
get dependencyRoots() { return self._dependencyRoots; }, | ||
}); | ||
} | ||
|
||
/** | ||
* Add a construct to the dependency roots | ||
*/ | ||
public add(construct: IConstruct) { | ||
this._dependencyRoots.push(construct); | ||
} | ||
} | ||
|
||
/** | ||
* Trait for IDependable | ||
* | ||
* Traits are interfaces that are privately implemented by objects. Instead of | ||
* showing up in the public interface of a class, they need to be queried | ||
* explicitly. This is used to implement certain framework features that are | ||
* not intended to be used by Construct consumers, and so should be hidden | ||
* from accidental use. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example on how this is used |
||
export abstract class DependableTrait { | ||
/** | ||
* Register `instance` to have the given DependableTrait | ||
* | ||
* Should be called in the class constructor. | ||
*/ | ||
public static implement(instance: IDependable, trait: 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); | ||
} | ||
|
||
/** | ||
* Retrieve the current set of dependency roots | ||
* Return the matching DependableTrait for the given class instance. | ||
*/ | ||
public get dependencyRoots(): IConstruct[] { | ||
return this._dependencyRoots; | ||
public static get(instance: IDependable): DependableTrait { | ||
const ret = DependableTrait.traitMap.get(instance); | ||
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[]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put a wee comment explaining what this does