{"id":16773837,"url":"https://github.com/ziflex/disposable-mixin","last_synced_at":"2025-08-31T09:37:23.322Z","repository":{"id":57212973,"uuid":"72363270","full_name":"ziflex/disposable-mixin","owner":"ziflex","description":"Brings implementation of IDisposable to your types","archived":false,"fork":false,"pushed_at":"2016-11-07T05:36:35.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-23T04:14:47.232Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ziflex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-30T17:11:15.000Z","updated_at":"2016-10-31T19:38:11.000Z","dependencies_parsed_at":"2022-08-24T21:41:49.905Z","dependency_job_id":null,"html_url":"https://github.com/ziflex/disposable-mixin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable-mixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable-mixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable-mixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable-mixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziflex","download_url":"https://codeload.github.com/ziflex/disposable-mixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243903167,"owners_count":20366434,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T06:47:10.156Z","updated_at":"2025-03-16T17:21:32.224Z","avatar_url":"https://github.com/ziflex.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# disposable-mixin\n\n\u003e Explicitly releasing resources\n\nMixin that brings 'Disposable' implementation to your types.\n\n[![npm version](https://badge.fury.io/js/disposable-mixin.svg)](https://www.npmjs.com/package/disposable-mixin)\n[![Build Status](https://secure.travis-ci.org/ziflex/disposable-mixin.svg?branch=master)](http://travis-ci.org/ziflex/disposable-mixin)\n[![Coverage Status](https://coveralls.io/repos/github/ziflex/disposable-mixin/badge.svg?branch=master)](https://coveralls.io/github/ziflex/disposable-mixin)\n\n````sh\n    npm install --save disposable-mixin\n````\n\n## Motivation\n\nTo write types that may free underlying resources explicitly like database connections, sockets and etc. by cleaning the references and invoking nested disposable objects.\n\n## Usage\n\n``disposable-mixin`` brings 2 methods:\n- ``isDisposed`` returns value which indicates wther the object is already disposed\n- ``dispose`` frees specified underlying resources and marks the object as disposed\n\nThe mixin comes as a factory function i.e. in order to get mixin it needs to invoke exported function.\n\n````javascript\n\n    import composeClass from 'compose-class';\n    import DisposableMixin from 'disposable-mixin';\n\n    const Class = composeClass({\n        mixins: [\n            DisposableMixin()\n        ]\n    });\n\n````\n\nIt is done intentionally in order to be able to pass the list of resource names (keys or symbols) and finalize callback.\n\n````javascript\n\n    import Symbol from 'es6-symbol';\n    import composeClass from 'compose-class';\n    import DisposableMixin from 'disposable-mixin';\n\n    const FIELDS = {\n        items: Symbol('items')\n    };\n\n    const Class = composeClass({\n        mixins: [\n            DisposableMixin([FIELDS.items])\n        ],\n\n        constructor(items) {\n            this[FIELDS.items] = items;\n        },\n\n        items() {\n            return this[FIELDS.items];\n        }\n    });\n\n    const instance = new Class(createManyObjects());\n\n    console.log(instance.items()); // [Objects...]\n    console.log(instance.isDisposed()); // false\n\n    instance.dispose();\n\n    console.log(instance.items()); // null\n    console.log(instance.isDisposed()); // true\n\n````\n\nFinalize callback allows to handle more complex scenarios like calling 'close' methods on database connections, destroy native objects and etc.\n\n````javascript\n// connection.js\n    import Symbol from 'es6-symbol';\n    import _ from 'lodash';\n    import composeClass from 'compose-class';\n    import DisposableMixin from 'disposable-mixin';\n    import { MongoClient } from 'mongodb';\n\n    const FIELDS = {\n        settings: Symbol('settings'),\n        connection: Symbol('connection')\n    };\n\n    function finalize(instance) {\n        if (instance[FIELDS.connection]) {\n            instance.close();\n        }\n    }\n\n    const ConnectionWrapper = composeClass({\n        mixins: [\n            DisposableMixin(_.values(FIELDS), finalize)\n        ],\n\n        constructor(settings) {\n            this[FIELDS.settings] = settings;\n        },\n\n        open(cb) {\n            if (this[FIELDS.connection]) {\n                return process.nextTick(() =\u003e cb(null));\n            }\n\n            return MongoClient.connect(this[FIELDS.settings], (err, db) =\u003e {\n                if (err) {\n                    return cb(err);\n                }\n\n                this[FIELDS.connection] = db;\n\n                return cb(null);\n            });\n        },\n\n        collection(name) {\n            return this[FIELDS.connection].collection(name);\n        },\n\n        close() {\n            if (this[FIELDS.connection]) {\n                this[FIELDS.connection].close();\n            }\n        }\n    });\n\n````\n\nIf a resource implements 'Disposable' interface, its ``dispose`` methods will be invoked.\n\n````javascript\n// repository.js\n\nimport Symbol from 'es6-symbol';\nimport _ from 'lodash';\nimport composeClass from 'compose-class';\nimport DisposableMixin from 'disposable-mixin';\nimport { MongoClient } from 'mongodb';\n\nconst FIELDS = {\n    connection: Symbol('connection')\n};\n\nconst Repository = composeClass({\n    mixins: [\n        DisposableMixin(_.values(FIELDS))\n    ],\n\n    constructor(connection) {\n        this[FIELDS.connection] = connection;\n    },\n\n    findAll(cb) {\n        const cursor = this[FIELDS.connection].collection('restaurants').find();\n        cursor.each(cb);\n\n        return this;\n    }\n});\n\n````\n\nIf 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fdisposable-mixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziflex%2Fdisposable-mixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fdisposable-mixin/lists"}