@esfx/disposable Package

    Provides a low-level API for defining explicit resource management that third-party libraries can use to interoperate.

    Installation

    npm i @esfx/disposable
    

    Usage

    • TypeScript
    • JavaScript (CommonJS)
    import { Disposable } from "@esfx/disposable";
    import * as fs from "fs";
    
    class MyFileResouce {
        private _handle?: number;
    
        constructor() {
            this._handle = fs.openSync("path/to/file", "r");
        }
    
        close() {
            if (this._handle !== undefined) {
                fs.closeSync(this._handle);
                this._handle = undefined;
            }
        }
        
        // provide low-level 'dispose' primitive for interop
        [Disposable.dispose]() {
            this.close();
        }
    }
    
    const { Disposable } = require("@esfx/disposable");
    const fs = require("fs");
    
    class MyFileResouce {
        constructor() {
            this._handle = fs.openSync("path/to/file", "r");
        }
    
        close() {
            if (this._handle !== undefined) {
                fs.closeSync(this._handle);
                this._handle = undefined;
            }
        }
        
        // provide low-level 'dispose' primitive for interop
        [Disposable.dispose]() {
            this.close();
        }
    }
    

    Classes

    AsyncDisposableStack

    A container for asynchronously disposable resources. When the stack is disposed, its containing resources are disposed in the reverse of the order in which they were added.

    DisposableStack

    A container for disposable resources. When the stack is disposed, its containing resources are disposed in the reverse of the order in which they were added.

    Interfaces

    AsyncDisposable

    Indicates an object that has resources that can be explicitly disposed asynchronously.

    AsyncDisposableScope

    Disposable

    Indicates an object that has resources that can be explicitly disposed.

    DisposableScope

    Used to aproximate using via for..of. See scope().

    NOTE: This is not spec-compliant and will not be standardized.

    Type Aliases

    AsyncDisposableLike

    Warning

    Deprecated

    Use instead.
    Declaration
    export declare type AsyncDisposableLike = AsyncDisposable | Disposable | (() => void | PromiseLike<void>);

    DisposableLike

    Warning

    Deprecated

    Use instead.
    Declaration
    export declare type DisposableLike = Disposable | (() => void);

    Namespaces

    AsyncDisposable

    Disposable

    Indicates an object that has resources that can be explicitly disposed.

    • Improve this Doc
    Generated by DocFX