Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dsherret/using_statement
"Using statement" in JavaScript and TypeScript.
https://github.com/dsherret/using_statement
Last synced: 2 months ago
JSON representation
"Using statement" in JavaScript and TypeScript.
- Host: GitHub
- URL: https://github.com/dsherret/using_statement
- Owner: dsherret
- License: mit
- Created: 2019-05-09T01:21:09.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-19T05:39:36.000Z (about 2 years ago)
- Last Synced: 2024-10-04T11:46:07.788Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 46.9 KB
- Stars: 68
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# using_statement
[![npm version](https://badge.fury.io/js/using-statement.svg)](https://badge.fury.io/js/using-statement)
[![CI](https://github.com/dsherret/using-statement/workflows/CI/badge.svg)](https://github.com/dsherret/using_statement/actions?query=workflow%3ACI)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/using_statement/mod.ts)Function call that acts like a
[using statement](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement).With Deno:
```ts
import { using } from "https://deno.land/x/using_statement/mod.ts";
```Or with Node:
```
npm install --save using-statement
```## Example
Before:
```ts
const camera = new Camera();
try {
outputPicture(camera.takePictureSync());
} finally {
camera.dispose();
}
```After:
```ts
import { using } from "https://deno.land/x/using_statement/mod.ts";using(new Camera(), (camera) => {
outputPicture(camera.takePictureSync());
});
```## Features
- Supports synchronous, asynchronous, and generator functions.
- Handles exceptions to ensure the resource is properly disposed.
- Accepts objects with a `dispose()`, `close()`, or `unsubscribe()` method.
- Allows asynchronously disposing when using a synchronous or asynchronous
function.## Examples
Setup:
```ts
// Camera.ts
export class Camera {
takePictureSync() {
// ...etc...
return pictureData;
}async takePicture() {
// ...etc...
return pictureData;
}dispose() {
// clean up the resource this class is holding
}
}
```Synchronous example:
```ts
import { using } from "https://deno.land/x/using_statement/mod.ts";
import { Camera } from "./Camera.ts";using(new Camera(), (camera) => {
const picture = camera.takePictureSync();
outputPicture(picture); // some function that outputs the picture
});// camera is disposed here
```Asynchronous example:
```ts
import { using } from "https://deno.land/x/using_statement/mod.ts";
import { Camera } from "./Camera.ts";await using(new Camera(), async (camera) => {
const picture = await camera.takePicture();
outputPicture(picture);
});// camera is disposed here
```Generator function example:
```ts
import { using } from "https://deno.land/x/using_statement/mod.ts";
import { Camera } from "./Camera.ts";const picturesIterator = using(new Camera(), function* (camera) {
for (let i = 0; i < 10; i++) {
yield camera.takePictureSync();
}
});// camera is not disposed yet...
for (const picture of picturesIterator) {
outputPicture(picture);
}// camera is now disposed
```### Inspiration
- C#'s
[using statement](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement).
- My old gist [here](https://gist.github.com/dsherret/cf5d6bec3d0f791cef00).
- [ECMAScript using statement proposal](https://github.com/tc39/proposal-using-statement).