From: Jay Freeman (saurik) Date: Tue, 24 Oct 2017 11:44:58 +0000 (-0700) Subject: Add some support for asynchronousish construction. X-Git-Tag: v0.9.2^0 X-Git-Url: https://git.saurik.com/logizomai.git/commitdiff_plain/5a6e550d77b0bd7546c75146aed4699b1bbb85cb Add some support for asynchronousish construction. --- 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;