{"id":22747196,"url":"https://github.com/one-com/dislocator","last_synced_at":"2025-10-23T17:26:32.328Z","repository":{"id":57212913,"uuid":"36006967","full_name":"One-com/dislocator","owner":"One-com","description":"Another service locator","archived":false,"fork":false,"pushed_at":"2020-04-17T11:12:40.000Z","size":80,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-28T00:44:50.743Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/One-com.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":"2015-05-21T10:38:11.000Z","updated_at":"2020-04-24T12:09:52.000Z","dependencies_parsed_at":"2022-08-24T21:41:47.912Z","dependency_job_id":null,"html_url":"https://github.com/One-com/dislocator","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Fdislocator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Fdislocator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Fdislocator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/One-com%2Fdislocator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/One-com","download_url":"https://codeload.github.com/One-com/dislocator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248662375,"owners_count":21141564,"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-12-11T03:13:39.534Z","updated_at":"2025-10-23T17:26:27.288Z","avatar_url":"https://github.com/One-com.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dislocator\n[![Build Status](https://travis-ci.org/One-com/dislocator.svg?branch=master)](https://travis-ci.org/One-com/dislocator)\n[![Coverage Status](https://coveralls.io/repos/github/One-com/dislocator/badge.svg?branch=master)](https://coveralls.io/github/One-com/dislocator?branch=master)\n\nA Service Locator implementation for JavaScript.\n\n# Installation\n\n```\n$ npm install dislocator\n```\n\n# Usage\n\nA ServiceLocator is a registry for services. It can be passed around in your\napplication and help you make more decoupled applications, as well as making\nswapping services out when testing code.\n\n```js\n// Creating a service locator instance\nimport ServiceLocator from 'dislocator';\n\nconst serviceLocator = new ServiceLocator();\n\nserviceLocator.register('config', { name: \"World\" });\n\nserviceLocator.register('greeter', (serviceLocator) =\u003e {\n  const config = serviceLocator.get('config');\n\n  return () =\u003e {\n    return `Hello ${config.name}!`;\n  }\n})\n\n// Using the service locator\n\nfunction sayHi(serviceLocator) {\n  const greeter = serviceLocator.greeter;\n  console.log(greeter());\n}\n\nsayHi(); // logs: Hello World!\n```\n\n# class Dislocator\n\nDefault export from the CJS and ESM builds as well as in the UMD build when used\nin CJS or AMD contexts. Accessible on `window.Dislocator` if loaded in the\nbrowser without a module loader.\n\n## constructor()\n\nThe constructor accepts no options.\n\n```js\nconst serviceLocator = new Dislocator();\n```\n\n## register(name: string, serviceCreator:any) =\u003e this\n\nMethod for registering a service.\n\nThe `name` must be a string that contains only letters (both upper- and\nlowercase) and numbers.\n\nThe `serviceCreator` argument can be a literal value (e.g. an object) or a\nfunction. Functions passed as `serviceCreator`s will not be invoked until the\nservice is requested; meaning that `serviceCreator` functions must return the\nservice instance synchroniuosly.\n\n```js\nserviceLocator.register('config', { value: \"my config value\" });\nserviceLocator.register('myService', () =\u003e {\n  return new MyService();\n});\n```\n\n`serviceCreator` functions get passed a reference to the Dislocator instance as\nthe only argument.\n\n```js\nserviceLocator.register('myService', (services) =\u003e {\n  const config = services.get('config');\n  return new MyService(config);\n});\n```\n\nThe `register` methods can be chained as the dislocator instance is returned.\n\n```js\nserviceLocator\n  .register('someService', () =\u003e {})\n  .register('anotherService', () =\u003e {});\n```\n\n## get(name: string) =\u003e any\n\nReturns an instance of the requested service.\n\n```js\nserviceLocator.get('myService');\n```\n\nServices can also be retrieved through getters on the Dislocator instance.\n\n```js\nserviceLocator.get('myService') === serviceLocator.myService; // =\u003e true\n```\n\nIf you attempt to retrieve a service that is not registered both methods will\nresult in an Error being thrown.\n\n```js\ntry {\n  serviceLocator.get('noSuchService');\n} catch (e) {\n  // e: `Error: No registration named \"noSuchService\"`\n}\n```\n\nDislocator does circular dependency detection when instantiating services.\n\n```js\nconst serviceLocator = new Dislocator();\n\nserviceLocator\n  .register('serviceA', () =\u003e {\n    const b = serviceLocator.get('serviceB');\n    return new ServiceA(b);\n  })\n  .register('serviceB', () =\u003e {\n    const a = serviceLocator.get('serviceA');\n    return new ServiceB(a);\n  });\n\ntry {\n  serviceLocator.get('serviceA');\n} catch (e) {\n  // e: `Error: Circular dependency detected (serviceA -\u003e serviceB -\u003e serviceA)`\n}\n```\n\n## unregister(name: string) =\u003e this\n\nRemove a registered service and any instantiated versions of it. Can be chained.\n\n```js\nserviceLocator\n  .unregister('someService')\n  .unregister('anotherService');\n```\n\n## isRegistered(name: string) =\u003e boolean\n\nChecks if a service is registered.\n\n```js\nserviceLocator.isRegistered('someService'); // =\u003e false\n\nserviceLocator.register('someService', () =\u003e {});\n\nserviceLocator.isRegistered('someService'); // =\u003e true\n\nserviceLocator.unregister('someService');\n\nserviceLocator.isRegistered('someService'); // =\u003e false\n```\n\n## names() =\u003e array\u003cstring*\u003e\n\nReturns a list of names of the registered services.\n\n```js\nserviceLocator\n  .register('someService', () =\u003e {})\n  .register('anotherService', () =\u003e {});\n\nserviceLocator.names(); // =\u003e ['someService', 'anotherService']\n```\n\n\n## use(serviceProvider: function) =\u003e this\n\nAllows you to modularize functions that register services.\n\n```js\nconst serviceLocator = new Dislocator();\n\nserviceLocator.register('config', myConfigObject);\n\nserviceLocator.use(require('./services/myService'));\n\n// File: services/myService.js\n\nconst MyService = require('../MyService');\n\nmodule.exports = function myServiceProvider(serviceLocator) {\n  serviceLocator.register('myService', () =\u003e {\n    return new MyService();\n  });\n};\n```\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fone-com%2Fdislocator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fone-com%2Fdislocator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fone-com%2Fdislocator/lists"}