UNPKG

684 BPlain TextView Raw
1import { Resource } from './pool.js';
2
3export abstract class ResourceDecorator<T extends Resource> implements Resource {
4 protected innerResource: T;
5
6 constructor(private readonly producer: () => T) {
7 this.innerResource = producer();
8 }
9
10 public async init(): Promise<void> {
11 await this.innerResource.init?.();
12 }
13
14 public async dispose(): Promise<void> {
15 await this.innerResource.dispose?.();
16 }
17 /**
18 * Disposes the current test runner and creates a new one
19 * To be used in decorators that need recreation.
20 */
21 protected async recover(): Promise<void> {
22 await this.dispose();
23 this.innerResource = this.producer();
24 return this.init();
25 }
26}