npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dislocator

v2.0.2

Published

Another service locator

Downloads

25,173

Readme

Dislocator

Build Status Coverage Status

A Service Locator implementation for JavaScript.

Installation

$ npm install dislocator

Usage

A ServiceLocator is a registry for services. It can be passed around in your application and help you make more decoupled applications, as well as making swapping services out when testing code.

// Creating a service locator instance
import ServiceLocator from 'dislocator';

const serviceLocator = new ServiceLocator();

serviceLocator.register('config', { name: "World" });

serviceLocator.register('greeter', (serviceLocator) => {
  const config = serviceLocator.get('config');

  return () => {
    return `Hello ${config.name}!`;
  }
})

// Using the service locator

function sayHi(serviceLocator) {
  const greeter = serviceLocator.greeter;
  console.log(greeter());
}

sayHi(); // logs: Hello World!

class Dislocator

Default export from the CJS and ESM builds as well as in the UMD build when used in CJS or AMD contexts. Accessible on window.Dislocator if loaded in the browser without a module loader.

constructor()

The constructor accepts no options.

const serviceLocator = new Dislocator();

register(name: string, serviceCreator:any) => this

Method for registering a service.

The name must be a string that contains only letters (both upper- and lowercase) and numbers.

The serviceCreator argument can be a literal value (e.g. an object) or a function. Functions passed as serviceCreators will not be invoked until the service is requested; meaning that serviceCreator functions must return the service instance synchroniuosly.

serviceLocator.register('config', { value: "my config value" });
serviceLocator.register('myService', () => {
  return new MyService();
});

serviceCreator functions get passed a reference to the Dislocator instance as the only argument.

serviceLocator.register('myService', (services) => {
  const config = services.get('config');
  return new MyService(config);
});

The register methods can be chained as the dislocator instance is returned.

serviceLocator
  .register('someService', () => {})
  .register('anotherService', () => {});

get(name: string) => any

Returns an instance of the requested service.

serviceLocator.get('myService');

Services can also be retrieved through getters on the Dislocator instance.

serviceLocator.get('myService') === serviceLocator.myService; // => true

If you attempt to retrieve a service that is not registered both methods will result in an Error being thrown.

try {
  serviceLocator.get('noSuchService');
} catch (e) {
  // e: `Error: No registration named "noSuchService"`
}

Dislocator does circular dependency detection when instantiating services.

const serviceLocator = new Dislocator();

serviceLocator
  .register('serviceA', () => {
    const b = serviceLocator.get('serviceB');
    return new ServiceA(b);
  })
  .register('serviceB', () => {
    const a = serviceLocator.get('serviceA');
    return new ServiceB(a);
  });

try {
  serviceLocator.get('serviceA');
} catch (e) {
  // e: `Error: Circular dependency detected (serviceA -> serviceB -> serviceA)`
}

unregister(name: string) => this

Remove a registered service and any instantiated versions of it. Can be chained.

serviceLocator
  .unregister('someService')
  .unregister('anotherService');

isRegistered(name: string) => boolean

Checks if a service is registered.

serviceLocator.isRegistered('someService'); // => false

serviceLocator.register('someService', () => {});

serviceLocator.isRegistered('someService'); // => true

serviceLocator.unregister('someService');

serviceLocator.isRegistered('someService'); // => false

names() => array<string*>

Returns a list of names of the registered services.

serviceLocator
  .register('someService', () => {})
  .register('anotherService', () => {});

serviceLocator.names(); // => ['someService', 'anotherService']

use(serviceProvider: function) => this

Allows you to modularize functions that register services.

const serviceLocator = new Dislocator();

serviceLocator.register('config', myConfigObject);

serviceLocator.use(require('./services/myService'));

// File: services/myService.js

const MyService = require('../MyService');

module.exports = function myServiceProvider(serviceLocator) {
  serviceLocator.register('myService', () => {
    return new MyService();
  });
};

License

MIT