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:
parent
dacff3db48
commit
d2b5ce9252
1 changed files with 17 additions and 3 deletions
|
@ -25,9 +25,9 @@ export abstract class Resource {
|
||||||
public readonly children: 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);
|
Resource.parentScope.push(parent);
|
||||||
try {
|
try {
|
||||||
return body();
|
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.
|
* Creates a new initialized resource object.
|
||||||
*/
|
*/
|
||||||
|
@ -147,7 +161,7 @@ export abstract class ComponentResource extends Resource {
|
||||||
constructor(t: string, name: string, props: ComputedValues,
|
constructor(t: string, name: string, props: ComputedValues,
|
||||||
init: () => void | ComputedValues | undefined, dependsOn?: Resource[]) {
|
init: () => void | ComputedValues | undefined, dependsOn?: Resource[]) {
|
||||||
super();
|
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
|
// 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
|
// 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.
|
// for now we simply cram both values (outputs) and props (inputs) together into the same property bag.
|
||||||
|
|
Loading…
Reference in a new issue