npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

disposable-class

v2.1.0

Published

Disposable abstract class

Readme

disposable

Disposable abstract class

npm version Actions Status

Installation

npm i disposable-class

Usage

API

As a decorator

import { disposable } from 'disposable-class';

interface DbConnection {
    close(): void;
}

@disposable
class Repository {
    private _conn: DbConnection;

    constructor(conn: DbConnection) {
        this._conn = conn;
    }

    public dispose(): void {
        // with decorator you do not have to call super.dispose();
        // the decorator does it automatically

        this._conn.close();
    }
}

As a class

import { Disposable } from 'disposable-class';

interface DbConnection {
    close(): void;
}

class Repository extends Disposable {
    private _conn: DbConnection;

    constructor(conn: DbConnection) {
        super();

        this._conn = conn;
    }

    public dispose(): void {
        if (!this.isDisposed()) {
            // important to call in order to mark the class as disposed
            super.dispose();

            this._conn.close();
        }
    }
}

Freeing resources

Plain values

import { disposable, free } from 'disposable-class';

@disposable
class Repository {
    @free()
    private _data: []; // the property will be dereferenced

    constructor() {
        this._conn = [];
    }
}

Disposables

import { disposable, free } from 'disposable-class';

@disposable
class ResourceA {
    @free()
    private _data: []; // the property will be dereferenced

    constructor() {
        this._conn = [];
    }
}

@disposable
class ResourceB {
    @free()
    private _data: ResourceA; // 'dispose' will be invoked and the property will be dereferenced

    constructor() {
        this._conn = new ResourceA();
    }
}

Custom objects

import { disposable, free } from 'disposable-class';

interface DbConnection {
    close(): void;
}

@disposable
class Repository {
    // "free" decorator allows you to create an alias for a property
    // by adding information what method must be called to free underlying resource.
    // Optionally, 'check' param can be passed in order to do a check whether the resource needs to be freed.
    // For example, "@free({ call: 'close', check: 'isClosed' })".
    @free({ call: 'close' })
    private _conn: { close: Function };

    constructor(conn: DbConnection) {
        this._conn = conn;
    }
}

Custom objects 2

import { disposable, free } from 'disposable-class';

interface DbConnection {
    close(): void;
    isClosed(): boolean;
}

@disposable
class Repository {
    // You can go wild
    @free({
        call: (conn: DbConnection) => conn.close(),
        check: (conn: DbConnection) => conn.isClosed(),
    })
    private _conn: DbConnection;

    constructor(conn: DbConnection) {
        this._conn = conn;
    }
}

Methods protection

import { disposable, free, protect } from 'disposable-class';

@disposable
class List<T> {
    @free()
    private _items: T[];

    constructor() {
        this._items = [];
    }

    // Onсe the instance gets disposed
    // All further function calls will be ignored and DisposedError will be thrown
    @protect()
    public add(item: T): void {
        // It's very important to protect methods that use properties that marked as freeable
        // In order to avoid runtime error, because the properties will be derefferenced
        this._items.push(item);
    }
}

Async methods

import { disposable, protect } from 'disposable-class';

@disposable
class Repository {
    @free()
    private _conn: DbConnection;

    constructor(conn: DbConnection) {
        this._conn = conn;
    }

    // Once the instance gets disposed
    // All further function calls will return rejected Promise
    @protect({ async: true })
    public async findOne(query: any): void {
        return this._conn.findOne(query);
    }
}