{"id":44665500,"url":"https://github.com/heap-code/singleton","last_synced_at":"2026-02-15T00:21:27.371Z","repository":{"id":175275101,"uuid":"653598788","full_name":"heap-code/singleton","owner":"heap-code","description":"A simple singleton implementation in Typescript","archived":false,"fork":false,"pushed_at":"2026-02-12T15:42:29.000Z","size":6736,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-02-13T00:05:54.714Z","etag":null,"topics":["lazy","singleton"],"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/heap-code.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-06-14T11:06:48.000Z","updated_at":"2026-02-09T12:38:31.000Z","dependencies_parsed_at":"2026-01-01T01:00:22.039Z","dependency_job_id":null,"html_url":"https://github.com/heap-code/singleton","commit_stats":null,"previous_names":["heap-code/singleton"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/heap-code/singleton","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heap-code%2Fsingleton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heap-code%2Fsingleton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heap-code%2Fsingleton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heap-code%2Fsingleton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heap-code","download_url":"https://codeload.github.com/heap-code/singleton/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heap-code%2Fsingleton/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29461775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T22:42:09.113Z","status":"ssl_error","status_checked_at":"2026-02-14T22:42:05.053Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["lazy","singleton"],"created_at":"2026-02-15T00:21:23.089Z","updated_at":"2026-02-15T00:21:27.356Z","avatar_url":"https://github.com/heap-code.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Singleton\n\n[![CI](https://github.com/heap-code/singleton/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/heap-code/singleton/actions/workflows/ci.yml)\n[![npm version](https://img.shields.io/npm/v/@heap-code/singleton)](https://www.npmjs.com/package/@heap-code/singleton)\n![Code coverage](.badges/code/coverage.svg)\n![Comment coverage](.badges/comment/coverage.svg)\n\nA simple singleton implementation with lazy initialization.\n\n## Installation\n\nSimply run:\n\n```bash\nnpm install @heap-code/singleton\n```\n\n### CDN\n\nThanks to [_jsdelivr_](https://www.jsdelivr.com/),\nthis package can easily be used in browsers like this:\n\n```html\n\u003cscript\n src=\"https://cdn.jsdelivr.net/npm/@heap-code/singleton/dist/bundles/singleton.umd.js\"\n type=\"application/javascript\"\n\u003e\u003c/script\u003e\n```\n\n\u003e **Note:**  \n\u003e It is recommended to use a minified and versioned bundle.\n\u003e\n\u003e For example:\n\u003e\n\u003e ```html\n\u003e \u003cscript\n\u003e  src=\"https://cdn.jsdelivr.net/npm/@heap-code/singleton@1.1.1/dist/bundles/singleton.umd.min.js\"\n\u003e  type=\"application/javascript\"\n\u003e \u003e\u003c/script\u003e\n\u003e ```\n\nMore at this [_jsdelivr_ package page](https://www.jsdelivr.com/package/npm/@heap-code/singleton).\n\n## Usage\n\nWrap a value that should only be calculated once and only when needed:\n\n```typescript\n// Typescript\nimport { Singleton } from \"@heap-code/singleton\";\n\nconst mySingleton = new Singleton(() =\u003e Math.random());\nconsole.log(mySingleton.get() === mySingleton.get());  // true\n```\n\n### Promise\n\nIt also works with `Promise` as they are only fulfilled once:\n\n```typescript\nimport { Singleton } from \"@heap-code/singleton\";\n\nfunction doAsyncStuff() {\n  return new Promise(resolve =\u003e {\n    console.log(\"It doesn't look like it, but I'm actually doing a lot of things.\");\n    setTimeout(() =\u003e resolve(new Date().getMilliseconds()), 100);\n  });\n}\n\nasync function bootstrap() {\n  const a1 = await doAsyncStuff();\n  const a2 = await doAsyncStuff();\n  console.log(a1 === a2); // false\n\n  const singleton = new Singleton(() =\u003e doAsyncStuff());\n  const b1 = await singleton.get();\n  const b2 = await singleton.get();\n  console.log(b1 === b2); // true\n}\n\nbootstrap();\n```\n\n\u003e **Note**:  \n\u003e Rather use this library for its lazy initialization rather than its \"singletoness\":\n\u003e\n\u003e **Example**:\n\u003e\n\u003e ```typescript\n\u003e import { Singleton } from \"@heap-code/singleton\";\n\u003e \n\u003e class MyAddition {\n\u003e   // Simply calculated, often used\n\u003e   public readonly added: number;\n\u003e \n\u003e   private readonly singleton: Singleton\u003cnumber\u003e;\n\u003e \n\u003e   public constructor(private readonly a: number, private readonly b: number) {\n\u003e     this.added = a + b;\n\u003e     this.singleton = new Singleton(() =\u003e Math.pow(a * b, Math.sin(a) * Math.cos(b)));\n\u003e   }\n\u003e \n\u003e   public get divided() {\n\u003e     // Used sometimes\n\u003e     return this.a / this.b;\n\u003e   }\n\u003e \n\u003e   public get complicated() {\n\u003e     // Used only sometimes and performance-intensive.\n\u003e     // Calculated only once needed\n\u003e     return this.singleton.get();\n\u003e   }\n\u003e }\n\u003e ```\n\n## Releases\n\nSee information about breaking changes and release notes [here](https://github.com/heap-code/singleton/blob/HEAD/CHANGELOG.md).\n\n## Author's note\n\nThis is not the most useful package, as it can most of the time be simply replaced by a variable.\nAnd come consider it to be an anti-pattern ([Singleton pattern criticism](https://en.wikipedia.org/wiki/Singleton_pattern#Criticism)).\n\nThis package was more a test for automating changelogs generation, _GitHub_ and _npm_ publishing process.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheap-code%2Fsingleton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheap-code%2Fsingleton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheap-code%2Fsingleton/lists"}