Add a Resource.runInParentlessScope function

As part of adding components, we sometimes want to allocate things
that are guaranteed not to get attributed to the calling component's
initialization code.  This includes lazily allocated pooled resources.
In those cases, we can invoke Resource.runInParentlessScope to
temporarily squelch the parent.  Also renames withParent to
runInParentScope to be more symmetric and explicit about what it does.
This commit is contained in:
joeduffy 2017-10-18 07:39:03 -07:00
parent dacff3db48
commit d2b5ce9252

View file

@ -25,9 +25,9 @@ export abstract class Resource {
public readonly children: Resource[];
/**
* withParent executes a callback, body, and any resources allocated within become parent's children.
* runInParentScope executes a callback, body, and any resources allocated within become parent's children.
*/
protected static withParent<T>(parent: Resource, body: () => T): T {
public static runInParentScope<T>(parent: Resource, body: () => T): T {
Resource.parentScope.push(parent);
try {
return body();
@ -37,6 +37,20 @@ export abstract class Resource {
}
}
/**
* runInParentlessScope executes a callback, body, in a scope where no parent is active. This can be useful
* if there's an active parent but you want to run some code that allocates "anonymous" resources.
*/
public static runInParentlessScope<T>(body: () => T): T {
Resource.parentScope.push(undefined);
try {
return body();
}
finally {
Resource.parentScope.pop();
}
}
/**
* Creates a new initialized resource object.
*/
@ -147,7 +161,7 @@ export abstract class ComponentResource extends Resource {
constructor(t: string, name: string, props: ComputedValues,
init: () => void | ComputedValues | undefined, dependsOn?: Resource[]) {
super();
const values: void | ComputedValues | undefined = Resource.withParent(this, init);
const values: void | ComputedValues | undefined = Resource.runInParentScope(this, init);
// IDEA: in the future, it would be nice to split inputs and outputs in the Pulumi metadata. This would let
// us display them differently. That implies fairly sizable changes to the RPC interfaces, however, so
// for now we simply cram both values (outputs) and props (inputs) together into the same property bag.