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

wire-dependency-injection

v2.0.6

Published

An easy to use JavaScript Dependency Injection Library

Downloads

30

Readme

WireDependencyInjection

License NPM version NPM downloads Twitter Follow

Introduction

WireDependencyInjection is a simple and lightweight library that manages dependencies with dependency injection principles.

You have a suggestion or want to report a bug? Create an issue on our GitHub repository.

Any type of contribution is welcomed. ❤ Feel free to contribute to the project ❤

Table of contents

Installation

npm install wire-dependency-injection
yarn add wire-dependency-injection

How does it work?

The objective is to access our dependencies without having to pass them between functions and classes.

To solve this problem, we are using what's called dependency injection.

We will store singletons (unique instance) of our dependencies in a single object called dependency manager and get them back when required.

Example:

| Identifier | Category | Value | | ---------------- | ---------- | --------------------------- | | number-of-images | BEAN | 10 | | download-service | SERVICE | class DownloadService {...} | | rest-controller | CONTROLLER | class RestController {...} |

Storing dependencies

There are two ways to store dependencies.

Declaration

The first one is a direct declaration, you will provide an identifier, the value and a category (optional), and that's it.

dependencyManager.declare('maximum-amount', 25);
dependencyManager.declare('minimum-amount', 7, 'CONFIGURATION');

Instancing

The second one is by instancing, you will provide

  • an identifier.
  • an initializer (a function or class that needs to be called to obtain the final value).
  • a behaviour (an indication of how and when it should be instanced), CAUTIOUS by default.
  • wires (a list of selectors for the required dependencies that will be passed in to the initializer) (optional).
  • a category (optional).

There are a total of 3 behaviours you can use

  • CAUTIOUS: Will wait for all of its dependencies to be present before instancing.
  • EAGER: Will instance as soon as declared. Will fail if not all the dependencies are present.
  • LAZY: Will instance only when requested. Will fail if not all the dependencies are present.

In this example, the SaveService will be instanced as soon as declared since all of its dependencies (none) are ready.

The SaveRestController will only be instanced when requested and will have the SaveService instance as parameter.

class SaveService {
  // ...
}

class SaveRestController {
  constructor(saveService: SaveService) {
    // ...
  }

  // ...
}

dependencyManager.instance('save-service', SaveService);
dependencyManager.instance('save-rest-controller', SaveRestController, {
  category: 'CONTROLLER',
  behaviour: LAZY,
  wires: ['save-service'],
});

Requesting dependencies

There are two possible ways of requesting dependencies.

Wiring

In the first one, we ask for a dependency and instantly get the result back, it can be an error if something went wrong, a single or multiple results depending on what we requested.

To get a single result, provide the identifier or set getFirst to true if you only provide the category.

To get multiple results, only provide the wanted category.

const saveService: SaveService = dependencyManager.wire('save-service');
const controllers: Controller[] = dependencyManager.wire({
  category: 'CONTROLLER',
});
const firstDeclaredController: Controller = dependencyManager.wire({
  category: 'CONTROLLER',
  getFirst: true,
});

dependencyManager.asyncWire('save-service').then(()=>...);
await dependencyManager.asyncWire({
  category: 'CONTROLLER',
}, 500);

AutoWiring

In the second one, we wait for the dependency to be ready before receiving it.

The function needs the same parameters as in the first method and a callback which will receive the dependency as a parameter.

the returned value will be undefined.

dependencyManager.autoWire('save-service', (saveService: SaveService) => {
  /*...*/
});
dependencyManager.autoWire(
  { category: 'CONTROLLER' },
  (controllers: Controller[]) => {
    /*...*/
  }
);
let firstDeclaredController: Controller = dependencyManager.autoWire(
  { category: 'CONTROLLER', getFirst: true },
  (c) => {
    firstDeclaredController = c;
  }
);

class LoadController {
  private loadService = dependencyManager.autoWire(
    'load-service',
    (_) => (this.loadService = _)
  );
  // ...
}

Error management

The error management is separated in two categories.

Thrown errors

In case of a direct action like declaring or wiring single dependencies, the errors that occur can be caught using the classic try/catch.

try {
  dependencyManager.declare('a-bean', 1);
  dependencyManager.declare('a-bean', 2);
} catch (e) {
  console.error();
}

Emitted errors

In case of potentially indirect action like instancing, resolving, autoWiring or wiring multiple dependencies, the errors are emitted with the error event in the extended EventEmitter.

dependencyManager.on('error', (error) => {
  /*...*/
});

dependencyManager.instance('a-service', AService);

The default listener prints the errors in the console. It can be removed using the removeListener function and the DEFAULT_ERROR_HANDLER.

import { DEFAULT_ERROR_HANDLER } from 'wire-dependency-injection';

dependencyManager.removeListener('error', DEFAULT_ERROR_HANDLER);

Technical documentation

Technical documentation can be found in the docs/ folder or on https://leopoldhub.github.io/wire-dependency-injection/.

Core features

  • No-Config: Get started quickly without complex configuration files.
  • Dependency Injection: Wire your dependencies at runtime, based on the Dependency Injection principle.
  • Modularity: Create different types of dependencies to easily manage them afterward.

Contributing

Any contributions are welcome!

Please follow the guidelines in our Contributing Guide to contribute to the project.

Feel free to report any issue or feature request on the issue tracker.

License

Apache-2.0