{"id":16368691,"url":"https://github.com/aigoncharov/singleton","last_synced_at":"2025-03-23T02:33:39.417Z","repository":{"id":57124248,"uuid":"178543748","full_name":"aigoncharov/singleton","owner":"aigoncharov","description":"Singleton decorator. No constructor monkeypatching. Zero dependencies. Built with TypeScript.","archived":false,"fork":false,"pushed_at":"2019-03-30T11:33:54.000Z","size":64,"stargazers_count":27,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-12T02:53:41.762Z","etag":null,"topics":["decorator","singleton","typescript"],"latest_commit_sha":null,"homepage":"","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/aigoncharov.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}},"created_at":"2019-03-30T10:24:14.000Z","updated_at":"2024-05-24T17:45:58.000Z","dependencies_parsed_at":"2022-08-31T05:02:56.171Z","dependency_job_id":null,"html_url":"https://github.com/aigoncharov/singleton","commit_stats":null,"previous_names":["keenondrums/singleton"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Fsingleton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Fsingleton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Fsingleton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aigoncharov%2Fsingleton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aigoncharov","download_url":"https://codeload.github.com/aigoncharov/singleton/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221842952,"owners_count":16890230,"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":["decorator","singleton","typescript"],"created_at":"2024-10-11T02:53:32.454Z","updated_at":"2024-10-28T14:47:22.268Z","avatar_url":"https://github.com/aigoncharov.png","language":"TypeScript","readme":"# singleton [![Build Status](https://travis-ci.org/keenondrums/singleton.svg?branch=master)](https://travis-ci.org/keenondrums/singleton) [![Coverage Status](https://coveralls.io/repos/github/keenondrums/singleton/badge.svg?branch=master)](https://coveralls.io/github/keenondrums/singleton?branch=master) [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Singleton.%20No%20constructor%20monkeypatching.%20Zero%20dependencies.%20Built%20with%20TypeScript.\u0026url=https://github.com/keenondrums/singleton\u0026hashtags=javascript,typescript,singleton,decorator)\n\nSingleton decorator. No constructor monkeypatching. Zero dependencies. Built with TypeScript.\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Installation](#installation)\n- [Quick start](#quick-start)\n- [Usage without decorators](#usage-without-decorators)\n- [Inheritance](#inheritance)\n- [In depth](#in-depth)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Installation\n\n1.  Run\n    ```sh\n    npm i @keenondrums/singleton\n    ```\n1.  **(Optional)** Enable decorators\n\n    1. If you use TypeScript set in you tsconfig.json\n\n       ```json\n       {\n         \"compilerOptions\": {\n           \"experimentalDecorators\": true\n         }\n       }\n       ```\n\n    1. If you use JavaScript configure your babel to support decorators and class properties\n\n## Quick start\n\n```ts\nimport { singleton } from '@keenondrums/singleton'\n\n@singleton\nclass Test {}\n\nnew Test() === new Test() // returns `true`\n```\n\n## Usage without decorators\n\n```ts\nimport { singleton } from '@keenondrums/singleton'\n\nclass Test {}\nconst TestSingleton = singleton(Test)\n\nnew TestSingleton() === new TestSingleton() // returns `true`\n```\n\n## Inheritance\n\nAny child of your singleton will not be a singleton.\n\n```ts\nimport { singleton } from '@keenondrums/singleton'\n\n@singleton\nclass Parent {}\n\nclass Child extends Parent {}\n\nnew Child() === new Child() // returns `false`\n\n// If you want to make `Child` a singleton as well, apply `singleton` decorator directly to it\n@singleton\nclass ChildSingleton extends Parent {}\n\nnew ChildSingleton() === new ChildSingleton() // returns `true`\n```\n\n## In depth\n\n`singleton` decorator wraps your class with a [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) and a [construct trap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/construct) to override class' creation logic.\n\nYour singleton instance is always available as a static property of a class by key `SINGLETON_KEY`.\n\n```ts\nimport { singleton, SINGLETON_KEY } from '@keenondrums/singleton'\n\n@singleton\nclass Test {}\n\nconst instance = new Test()\nTest[SINGLETON_KEY] === instance // returns `true`\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faigoncharov%2Fsingleton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faigoncharov%2Fsingleton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faigoncharov%2Fsingleton/lists"}