Giter Club home page Giter Club logo

deno-disposable's Introduction

disposable

deno land deno doc Test

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 for 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:

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

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:

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:

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. Contributors need to agree that any modifications sent in this repository follow the license.

deno-disposable's People

Contributors

lambdalisue avatar web-flow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.