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 🙏

© 2026 – Pkg Stats / Ryan Hefner

disposable-mixin

v1.0.1

Published

Mixin that brings 'Disposable' implementation to your types

Readme

disposable-mixin

Explicitly releasing resources

Mixin that brings 'Disposable' implementation to your types.

npm version Build Status Coverage Status

    npm install --save disposable-mixin

Motivation

To write types that may free underlying resources explicitly like database connections, sockets and etc. by cleaning the references and invoking nested disposable objects.

Usage

disposable-mixin brings 2 methods:

  • isDisposed returns value which indicates wther the object is already disposed
  • dispose frees specified underlying resources and marks the object as disposed

The mixin comes as a factory function i.e. in order to get mixin it needs to invoke exported function.


    import composeClass from 'compose-class';
    import DisposableMixin from 'disposable-mixin';

    const Class = composeClass({
        mixins: [
            DisposableMixin()
        ]
    });

It is done intentionally in order to be able to pass the list of resource names (keys or symbols) and finalize callback.


    import Symbol from 'es6-symbol';
    import composeClass from 'compose-class';
    import DisposableMixin from 'disposable-mixin';

    const FIELDS = {
        items: Symbol('items')
    };

    const Class = composeClass({
        mixins: [
            DisposableMixin([FIELDS.items])
        ],

        constructor(items) {
            this[FIELDS.items] = items;
        },

        items() {
            return this[FIELDS.items];
        }
    });

    const instance = new Class(createManyObjects());

    console.log(instance.items()); // [Objects...]
    console.log(instance.isDisposed()); // false

    instance.dispose();

    console.log(instance.items()); // null
    console.log(instance.isDisposed()); // true

Finalize callback allows to handle more complex scenarios like calling 'close' methods on database connections, destroy native objects and etc.

// connection.js
    import Symbol from 'es6-symbol';
    import _ from 'lodash';
    import composeClass from 'compose-class';
    import DisposableMixin from 'disposable-mixin';
    import { MongoClient } from 'mongodb';

    const FIELDS = {
        settings: Symbol('settings'),
        connection: Symbol('connection')
    };

    function finalize(instance) {
        if (instance[FIELDS.connection]) {
            instance.close();
        }
    }

    const ConnectionWrapper = composeClass({
        mixins: [
            DisposableMixin(_.values(FIELDS), finalize)
        ],

        constructor(settings) {
            this[FIELDS.settings] = settings;
        },

        open(cb) {
            if (this[FIELDS.connection]) {
                return process.nextTick(() => cb(null));
            }

            return MongoClient.connect(this[FIELDS.settings], (err, db) => {
                if (err) {
                    return cb(err);
                }

                this[FIELDS.connection] = db;

                return cb(null);
            });
        },

        collection(name) {
            return this[FIELDS.connection].collection(name);
        },

        close() {
            if (this[FIELDS.connection]) {
                this[FIELDS.connection].close();
            }
        }
    });

If a resource implements 'Disposable' interface, its dispose methods will be invoked.

// repository.js

import Symbol from 'es6-symbol';
import _ from 'lodash';
import composeClass from 'compose-class';
import DisposableMixin from 'disposable-mixin';
import { MongoClient } from 'mongodb';

const FIELDS = {
    connection: Symbol('connection')
};

const Repository = composeClass({
    mixins: [
        DisposableMixin(_.values(FIELDS))
    ],

    constructor(connection) {
        this[FIELDS.connection] = connection;
    },

    findAll(cb) {
        const cursor = this[FIELDS.connection].collection('restaurants').find();
        cursor.each(cb);

        return this;
    }
});

If we call dispose method of the repository instance, connection will be disposed automatically. Therefore, we can easily build deeply nested objects without worring about possible memory leaks.