Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Typescript Disposable (using statement)
// NOTE: This is now rolled up in a package and supports more scenarios: https://github.com/dsherret/using-statement
interface IDisposable {
dispose();
}
function using<T extends IDisposable>(resource: T, func: (resource: T) => void) {
try {
func(resource);
} finally {
resource.dispose();
}
}
// Example use:
class Camera implements IDisposable {
takePicture() { /* omitted */ }
// etc...
dispose() {
navigator.camera.cleanup();
}
}
using(new Camera(), (camera) => {
camera.takePicture();
});
@NickStrupat

This comment has been minimized.

Copy link

@NickStrupat NickStrupat commented Apr 18, 2016

Love it haha. I'm working on something you might be interested in, after seeing your .NET influences...

I'm prototyping a tool which takes a TypeScript definition file as input and outputs a C# file with typed wrappings around the corresponding JS calls via Edge. In short, Edge wraps an instance of Node. The goal is to open the flood gates to easy interop between .NET and the massive number of useful JS packages.

This also is why I've been poking around your ts-type-info project :) I've been using it with immense relief after seeing what the TS compiler API gives back!

@dsherret

This comment has been minimized.

Copy link
Owner Author

@dsherret dsherret commented May 15, 2016

@NickStrupat hey sorry for not responding... github didn't notify me about your message.

That's really cool! I'm glad to hear what someone is using that library for. I'm not too familiar with edge, but from hearing about it I think it would be perfect for that. There's no reason to maintain something like that manually when a definition file already exists so automating it would be ideal for sure. Let me know how it goes.

Yeah, I have spent way too many hours figuring out how to get information out of the TS compiler api haha! It's been a huge pain sometimes.

@SteveStrong

This comment has been minimized.

Copy link

@SteveStrong SteveStrong commented Mar 19, 2018

very well done

@nite

This comment has been minimized.

Copy link

@nite nite commented May 8, 2018

Just came here via a google search - any chance this is in the typescript future spec?

@maca134

This comment has been minimized.

Copy link

@maca134 maca134 commented Jul 15, 2018

Here is a modification i made to it, so it works with async/await

/**
 * Provides a mechanism for releasing resources.
 */
export interface IDisposable {
    dispose(): Promise<void>;
}

/**
 * Provides a convenient syntax that ensures the correct use of IDisposable objects
 */
export async function using<T extends IDisposable>(resource: T, func: (resource: T) => Promise<void>) {
    try {
        await func(resource);
    } finally {
        await resource.dispose();
    }
}

// example

(async () => {
    await using(ioc.get<IUdpClient>(types.IUdpClient), async (client) => {
        client.connect(endpoint);
        client.send({toBuffer: () => Buffer.from("hello world1")});
        let response = await client.receive();
        console.log(response);

        await Task.delay(1000);
        client.send({toBuffer: () => Buffer.from("hello world2")});
        response = await client.receive();
        console.log(response);


        console.log("using complete");
    });
    console.log("end");
})();
@stherrienaspnet

This comment has been minimized.

Copy link

@stherrienaspnet stherrienaspnet commented Sep 26, 2018

Really nice!

@mxs42

This comment has been minimized.

Copy link

@mxs42 mxs42 commented Oct 24, 2018

Very very nice, good and elegant solutions.

@zewa666

This comment has been minimized.

Copy link

@zewa666 zewa666 commented Feb 1, 2019

Awesome idea. @nite take a look at this beauty https://github.com/tc39/proposal-using-statement

@fitfinderaustralia

This comment has been minimized.

Copy link

@fitfinderaustralia fitfinderaustralia commented Mar 31, 2019

Very cool. Have you considered turning this into an NPM module? I agree that it should be a language construct.

@dsherret

This comment has been minimized.

Copy link
Owner Author

@dsherret dsherret commented May 9, 2019

@fitfinderaustralia done and it supports more scenarios such as async and generator functions: https://github.com/dsherret/using-statement

@fitfinderaustralia

This comment has been minimized.

Copy link

@fitfinderaustralia fitfinderaustralia commented May 12, 2019

Great work - I love it!

@giano

This comment has been minimized.

Copy link

@giano giano commented Sep 2, 2020

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.