Giter Site home page Giter Site logo

madappgang / mag-service-registry Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 596 KB

Globally available central service registry

Home Page: https://www.npmjs.com/package/@madappgang/registry

License: MIT License

JavaScript 100.00%
service-locator services registry locator

mag-service-registry's Introduction

Registry

Build Status Coverage Status

What is it?

This package implements a concept of a globally available registry that enables access to your logic throughout the application. It is quite similar to service locator software development pattern.

What might you need it for?

You want to initialize your logic right after the application starts. The place to do it is the applications entry point. The issue is that you would then have to export services from the entry point module, which is not a good thing to do. Entry point code should only be run once. If you're exporting logic in your entry point module, you're probably better off moving that functionality in to a re-usable module.

With this package you can create a registry and export it at one place, but delegate the registering to another. This is the key idea of the package.

How do you use it?

First off you need to install it. It is available on npm.

$ npm install --save @madappgang/registry

Creating a registry

// serviceRegistry.js

import createRegistry from '@madappgang/registry';

const { register, exposeRegistered } = createRegistry();

// here you delegate the registering so it can be performed in another file
export { register };

export default exposeRegistered();

Default import of this file is an object that contains all registered stuff. At first it is an empty object since nothing has been registered yet, but it's going to update each time you register something.

The idea is that you register stuff somewhere else, but then access it from here.

Registering logic

All you need to do now is to init your logic and get it registered globally.

// entry point or whatever place you want to configure your services at
import { register } from './serviceRegistry';
...

const registerServices = async () => {
  const httpService = await configureHttpService();
  const storageService = await configureStorageService();

  return register({
    http: httpService,
    storage: storageService,
  });
};

const services = await registerServices();

...

Accessing logic

Then you simply access you services in another file

// anotherFile.js

// this is imported from the place where the registry was created
import services from './serviceRegistry';

console.log(services.http); // httpService
console.log(services.storage); // storageService

As you can see you did the registering at the entry point, but access the results from servieRegistry.js

As simple as that.

API

Registry

Registry is created with packages createRegistry method.

Property Type Description
register function This is used to register units. It accepts an object, keys of which are aliases, and values are units. You will then be able to access units by those aliases. Returns all that was ever registered.
exposeRegistered function This returns the object that holds everything that has ever been registered to this instance of registry. Returns all that was ever registered.

License

This project is licensed under the MIT License - see the LICENSE file for details.

mag-service-registry's People

Contributors

dprovodnikov avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.