From 5a6e550d77b0bd7546c75146aed4699b1bbb85cb Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 24 Oct 2017 04:44:58 -0700 Subject: [PATCH 1/1] Add some support for asynchronousish construction. --- lib/index.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/index.ts b/lib/index.ts index 41da6f5..3d7f603 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -28,6 +28,10 @@ export abstract class Resource { this.finalizes = []; } + public async _(): Promise { + return this; + } + public retain(): void { ++this.retainers; } @@ -99,6 +103,18 @@ export function using(resource: Type, code: (resou } } +export function construct(target: any, property: string, descriptor: PropertyDescriptor) { + const old = descriptor.value; + descriptor.value = async function(this: Resource): Promise { + const parent = target.__proto__; + await parent._.call(this); try { + return await old.call(this); + } catch (error) { + parent.finalize.call(this); + throw error; } + }; +} + interface Waitable { size: number; values(): IterableIterator; -- 2.45.2