]> git.saurik.com Git - logizomai.git/commitdiff
Add some support for asynchronousish construction. v0.9.2
authorJay Freeman (saurik) <saurik@saurik.com>
Tue, 24 Oct 2017 11:44:58 +0000 (04:44 -0700)
committerJay Freeman (saurik) <saurik@saurik.com>
Tue, 24 Oct 2017 11:44:58 +0000 (04:44 -0700)
lib/index.ts

index 41da6f5a3917e0b24ba6b7626b63ebb654069963..3d7f6036b13ff6525683f1e86b6c2a07b0d78648 100644 (file)
@@ -28,6 +28,10 @@ export abstract class Resource {
         this.finalizes = [];
     }
 
+    public async _(): Promise<this> {
+        return this;
+    }
+
     public retain(): void {
         ++this.retainers;
     }
@@ -99,6 +103,18 @@ export function using<Type extends Resource, Value>(resource: Type, code: (resou
     }
 }
 
+export function construct(target: any, property: string, descriptor: PropertyDescriptor) {
+    const old = descriptor.value;
+    descriptor.value = async function(this: Resource): Promise<any> {
+        const parent = target.__proto__;
+        await parent._.call(this); try {
+            return await old.call(this);
+        } catch (error) {
+            parent.finalize.call(this);
+        throw error; }
+    };
+}
+
 interface Waitable<Value> {
     size: number;
     values(): IterableIterator<Value>;