{"id":16773840,"url":"https://github.com/ziflex/disposable","last_synced_at":"2025-10-18T05:32:57.901Z","repository":{"id":35070866,"uuid":"203213058","full_name":"ziflex/disposable","owner":"ziflex","description":"Disposable abstract class","archived":false,"fork":false,"pushed_at":"2025-06-06T16:48:23.000Z","size":942,"stargazers_count":2,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-06T17:36:36.060Z","etag":null,"topics":["class","disposable","javascript-library","resource-management","state-management","typescript","typescript-library"],"latest_commit_sha":null,"homepage":"https://ziflex.github.io/disposable/","language":"TypeScript","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":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-19T16:50:29.000Z","updated_at":"2025-06-06T16:47:05.000Z","dependencies_parsed_at":"2025-02-17T23:32:49.905Z","dependency_job_id":"5fc77409-d0c5-41a4-a457-95fbd578422b","html_url":"https://github.com/ziflex/disposable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/ziflex/disposable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziflex","download_url":"https://codeload.github.com/ziflex/disposable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziflex%2Fdisposable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261479083,"owners_count":23164622,"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":["class","disposable","javascript-library","resource-management","state-management","typescript","typescript-library"],"created_at":"2024-10-13T06:47:10.412Z","updated_at":"2025-09-25T22:22:54.002Z","avatar_url":"https://github.com/ziflex.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# disposable\n\n\u003e Disposable abstract class\n\n[![npm version](https://badge.fury.io/js/disposable-class.svg)](https://www.npmjs.com/package/disposable-class)\n[![Actions Status](https://github.com/ziflex/disposable/workflows/Node%20CI/badge.svg)](https://github.com/ziflex/disposable/workflows/Node%20CI/badge.svg)\n\n## Installation\n\n```bash\nnpm i disposable-class\n```\n\n## Usage\n\n[API](https://ziflex.github.io/disposable/)\n\n### As a decorator\n\n```typescript\nimport { disposable } from 'disposable-class';\n\ninterface DbConnection {\n    close(): void;\n}\n\n@disposable\nclass Repository {\n    private _conn: DbConnection;\n\n    constructor(conn: DbConnection) {\n        this._conn = conn;\n    }\n\n    public dispose(): void {\n        // with decorator you do not have to call super.dispose();\n        // the decorator does it automatically\n\n        this._conn.close();\n    }\n}\n```\n\n### As a class\n\n```typescript\nimport { Disposable } from 'disposable-class';\n\ninterface DbConnection {\n    close(): void;\n}\n\nclass Repository extends Disposable {\n    private _conn: DbConnection;\n\n    constructor(conn: DbConnection) {\n        super();\n\n        this._conn = conn;\n    }\n\n    public dispose(): void {\n        if (!this.isDisposed()) {\n            // important to call in order to mark the class as disposed\n            super.dispose();\n\n            this._conn.close();\n        }\n    }\n}\n```\n\n### Freeing resources\n\n#### Plain values\n\n```typescript\nimport { disposable, free } from 'disposable-class';\n\n@disposable\nclass Repository {\n    @free()\n    private _data: []; // the property will be dereferenced\n\n    constructor() {\n        this._conn = [];\n    }\n}\n```\n\n#### Disposables\n\n```typescript\nimport { disposable, free } from 'disposable-class';\n\n@disposable\nclass ResourceA {\n    @free()\n    private _data: []; // the property will be dereferenced\n\n    constructor() {\n        this._conn = [];\n    }\n}\n\n@disposable\nclass ResourceB {\n    @free()\n    private _data: ResourceA; // 'dispose' will be invoked and the property will be dereferenced\n\n    constructor() {\n        this._conn = new ResourceA();\n    }\n}\n```\n\n#### Custom objects\n\n```typescript\nimport { disposable, free } from 'disposable-class';\n\ninterface DbConnection {\n    close(): void;\n}\n\n@disposable\nclass Repository {\n    // \"free\" decorator allows you to create an alias for a property\n    // by adding information what method must be called to free underlying resource.\n    // Optionally, 'check' param can be passed in order to do a check whether the resource needs to be freed.\n    // For example, \"@free({ call: 'close', check: 'isClosed' })\".\n    @free({ call: 'close' })\n    private _conn: { close: Function };\n\n    constructor(conn: DbConnection) {\n        this._conn = conn;\n    }\n}\n```\n\n#### Custom objects 2\n\n```typescript\nimport { disposable, free } from 'disposable-class';\n\ninterface DbConnection {\n    close(): void;\n    isClosed(): boolean;\n}\n\n@disposable\nclass Repository {\n    // You can go wild\n    @free({\n        call: (conn: DbConnection) =\u003e conn.close(),\n        check: (conn: DbConnection) =\u003e conn.isClosed(),\n    })\n    private _conn: DbConnection;\n\n    constructor(conn: DbConnection) {\n        this._conn = conn;\n    }\n}\n```\n\n### Methods protection\n\n```typescript\nimport { disposable, free, protect } from 'disposable-class';\n\n@disposable\nclass List\u003cT\u003e {\n    @free()\n    private _items: T[];\n\n    constructor() {\n        this._items = [];\n    }\n\n    // Onсe the instance gets disposed\n    // All further function calls will be ignored and DisposedError will be thrown\n    @protect()\n    public add(item: T): void {\n        // It's very important to protect methods that use properties that marked as freeable\n        // In order to avoid runtime error, because the properties will be derefferenced\n        this._items.push(item);\n    }\n}\n```\n\n#### Async methods\n\n```typescript\nimport { disposable, protect } from 'disposable-class';\n\n@disposable\nclass Repository {\n    @free()\n    private _conn: DbConnection;\n\n    constructor(conn: DbConnection) {\n        this._conn = conn;\n    }\n\n    // Once the instance gets disposed\n    // All further function calls will return rejected Promise\n    @protect({ async: true })\n    public async findOne(query: any): void {\n        return this._conn.findOne(query);\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fdisposable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziflex%2Fdisposable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziflex%2Fdisposable/lists"}