https://github.com/lambdalisue/deno-disposable
🦕 Ensure a disposable resource is disposed in Deno
https://github.com/lambdalisue/deno-disposable
deno disposable typescript using
Last synced: about 2 months ago
JSON representation
🦕 Ensure a disposable resource is disposed in Deno
- Host: GitHub
- URL: https://github.com/lambdalisue/deno-disposable
- Owner: lambdalisue
- License: mit
- Created: 2021-06-13T17:16:24.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-26T00:15:39.000Z (about 1 year ago)
- Last Synced: 2025-05-08T03:14:36.370Z (about 2 months ago)
- Topics: deno, disposable, typescript, using
- Language: TypeScript
- Homepage: https://deno.land/x/disposable
- Size: 30.3 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# disposable
[](https://deno.land/x/disposable)
[](https://doc.deno.land/https/deno.land/x/disposable/mod.ts)
[](https://github.com/lambdalisue/deno-disposable/actions/workflows/test.yml)This module provides `Disposable` type with `usingResource()`,
`usingResourceSync()`, `usingAllResources()`, and `usingAllResourcesSync()`
functions to ensure a disposable resource is disposed. It is like C#'s
`IDisposable` with `using` or Python's context with `with`.Note that
[TypeScript 5.2 add supports](https://github.com/microsoft/TypeScript/issues/52955)
for
[Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management)
which will take over the functionality provided by this library.## Usage
Implement `Disposable` on a resource. Write code to release that resource in
`dispose()` method. Then use it with `usingResource` like:```typescript
import {
Disposable,
usingResource,
} from "https://deno.land/x/disposable/mod.ts";class Connection implements Disposable {
dispose() {
// Synchronously release the resource
}
}await usingResource(new Connection(), (conn) => {
// The connection is alive in this block
// Do whatever you want synchronously
});
// The connection is disposed after above block
```You can use async function for `dispose()` or `fn` like
```typescript
import {
Disposable,
usingResource,
} from "https://deno.land/x/disposable/mod.ts";class Connection implements Disposable {
async dispose() {
// Asynchronously release the resource
}
}await usingResource(new Connection(), async (conn) => {
// The connection is alive in this block
// Do whatever you want asynchronously
});
// The connection is disposed after above block
```If you are only using synchronous disposable and prefer synchronous code, use
`usingResourceSync` like:```typescript
import {
Disposable,
usingResourceSync,
} from "https://deno.land/x/disposable/mod.ts";class Connection implements Disposable {
dispose() {
// Synchronously release the resource
}
}usingResourceSync(new Connection(), (conn) => {
// The connection is alive in this block
// Do whatever you want
});
// The connection is disposed after above block
```If you would like to dispose multiple disposables, use `usingAllResources` like:
```typescript
import {
Disposable,
usingAllResources,
} from "https://deno.land/x/disposable/mod.ts";class Connection1 implements Disposable {
async dispose() {
// Asynchronously release the resource
}
}class Connection2 implements Disposable {
dispose() {
// Synchronously release the resource
}
}await usingAllResources(
[new Connection1(), new Connection2()],
async (conn1, conn2) => {
// The connections are alive in this block
// Do whatever you want asynchronously
},
);
// The connections are disposed after above block
```Or use `usingAllResourcesSync` for synchronous disposables.
## License
The code follows MIT license written in [LICENSE](./LICENSE). Contributors need
to agree that any modifications sent in this repository follow the license.