{"id":20324921,"url":"https://github.com/xeeo/nc-singleton","last_synced_at":"2025-07-10T19:34:40.497Z","repository":{"id":57309167,"uuid":"42733640","full_name":"xeeo/nc-singleton","owner":"xeeo","description":"a simple singleton module to be used in your contructors","archived":false,"fork":false,"pushed_at":"2016-04-09T04:52:46.000Z","size":4,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-04T10:28:12.072Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xeeo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-18T16:25:55.000Z","updated_at":"2024-08-14T15:16:18.000Z","dependencies_parsed_at":"2022-08-26T13:51:39.786Z","dependency_job_id":null,"html_url":"https://github.com/xeeo/nc-singleton","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xeeo/nc-singleton","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xeeo%2Fnc-singleton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xeeo%2Fnc-singleton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xeeo%2Fnc-singleton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xeeo%2Fnc-singleton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xeeo","download_url":"https://codeload.github.com/xeeo/nc-singleton/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xeeo%2Fnc-singleton/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264640004,"owners_count":23642323,"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-11-14T19:38:11.194Z","updated_at":"2025-07-10T19:34:40.474Z","avatar_url":"https://github.com/xeeo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nc-singleton\n\nA simple singleton module to be used inside your constructor.\n\n## Usage\n\nLet's assume you are working on a module. You use prototype and you want that all your new statements return the same instance.\n\n```javascript\n'use strict';\n\nvar singleton = require('nc-singleton');\n\nvar Cache = function Cache() {\n    return singleton.call(this, Cache);\n};\n\nCache.prototype.setTime = function() {\n    this.time = new Date();\n};\n\nCache.prototype.getTime = function() {\n    return this.time;\n};\n\n\nvar cache = new Cache()\ncache.setTime();\nconsole.log(cache.getTime());\n\nsetTimeout(function(){\n  var cache = new Cache();\n  console.log(cache.getTime());\n}, 4000);\n\n```\n\nIt also works with ES6\n\n```javascript\n'use strict';\n\nvar singleton = require('nc-singleton');\n\nlet instance = null;\n\nclass Cache {\n      constructor() {\n        return singleton.call(this, Cache);\n      }\n\n      setTime() {\n          this.time = new Date();\n      }\n\n      getTime() {\n          return this.time;\n      }\n}\n\nlet cache = new Cache()\ncache.setTime();\nconsole.log(cache.getTime());\n\nsetTimeout(function(){\n  let cache = new Cache();\n  console.log(cache.getTime());\n}, 4000);\n\n```\n\nFor testing just npm test :)\n\nThat's it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxeeo%2Fnc-singleton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxeeo%2Fnc-singleton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxeeo%2Fnc-singleton/lists"}