wardens
Version:
A framework for resource management
82 lines (69 loc) • 2.47 kB
text/typescript
import { createWithContext, destroy } from './resource-lifecycle';
import { ContextHandle, InheritedContext } from './inherited-context';
import { RevokableResource } from './global-weakrefs';
import { ResourceFactory, ParametrizedResourceFactory } from './utility-types';
/**
* An instance of this class is passed to resources as they're being
* provisioned. It allows them to provision other resources while keeping
* track of ownership and lifetimes.
*/
export default class ResourceControls {
constructor(
state: InheritedContext,
ownedResources: Set<object>,
freeze: RevokableResource['curfew'],
) {
this.
this.
this.
}
/** Provision an owned resource and make sure it doesn't outlive us. */
public create = async <Controls extends object, Args extends Array<unknown>>(
factory:
| ParametrizedResourceFactory<Controls, Args>
| ResourceFactory<Controls>,
...args: Args
): Promise<Controls> => {
if (this.
throw new Error('Cannot create new resources after teardown.');
}
const context = Object.create(this.
const controls = await createWithContext(context, factory, ...args);
this.
return controls;
};
/**
* Tear down a resource. Happens automatically when resource owners are
* deallocated.
*/
public destroy = async (resource: object) => {
if (this.
throw new Error('Resource already destroyed.');
}
if (!this.
throw new Error('You do not own this resource.');
}
this.
this.
await destroy(resource);
};
/** Store a value in context. Anything down the chain can read it. */
public setContext = <Value>(
context: ContextHandle<Value>,
value: Value,
): void => {
this.
};
/** Retrieve a value from context, or a default if it is unset. */
public getContext = <Value>(context: ContextHandle<Value>): Value => {
const id = ContextHandle.getId(context);
if (id in this.
return this.
}
return ContextHandle.getDefaultValue(context);
};
}