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

cv3-inject

v0.0.7

Published

Dependency injection code.

Downloads

5

Readme

avatar

cv3-inject

Simple dependency injection for ES6

cv3-inject Version Badge Travis (.org) Downloads Badge Size badge Apache-2.0 Licence Badge

Install

$ npm install cv3-inject

Usage

Define default dependencies with Inject:

import { Inject } from 'cv3-inject/Inject';

import { Star   } from './Star';
import { Lights } from './Lights';

export class XmasTree extends Inject(class{}, {
	lights: Lights, star:Star
}) {
	blink()
	{
		this.star.blink();
		this.lights.blink();
	}
}

Static Dependencies

Keys starting with a capital letter will not be instantiated, and instead, a reference to the class will be passed in.

(Please note this example uses object literal shorthand syntax)

import { Inject } from './Inject';

import { Drink  } from './Drink';
import { Snack  } from './Snack';

export class VendingMachine extends Inject(class{}, {Drink, Snack}) {
	getDrink()
	{
		return new this.Drink;
	}

	getSnack()
	{
		return new this.Snack;
	}
}

Overriding Dependencies

You can inject dependencies in-place, before instantiation. This essentially creates a new subclass on the fly. Please be mindful of the parentheses placement.

const redTree = new (Inject(XmasTree, {star: RedStar} ));

The dynamically created class will still register as an instance of the base class:

console.assert(redTree instanceof XmasTree);

Hierarchical Dependencies

const sameTree = new (Inject(XmasTree, {
	star:     RedStar
	, lights: Inject(Lights, {star: undefined})
}));

Explcitily setting an injection to undefined will cause the injector to check the parent injector for the same key, and if not, its parent, and so on.

Top level injections will throw an error if an injection is undefined.

In this example, every new instance of the injected XmasTree will hold a reference to rhe same RedStar at instance.star and instance.lights.star.

Note Child injections can only access parent injections that preceed it in the precedence order in the base definition. In the above example, .star could not be modified to hold a reference to .lights, even if a subclass injection contained a different ordering. Please keep this in mind while designing dependence hierarchies.

Cascading Dependencies

import { Inject as I } from 'cv3-inject/Inject';
// ... more imports

export class VendingMachineTest extends I(Test
	, {vendingMachine:  VendingMachine}

	, ({vendingMachine: VendingMachine}) => ({
		sodaMachine:      I(VendingMachine, {Drink:Soda})
		, waterMachine:   I(VendingMachine, {Drink:BottleOfWater})
		, chipMachine:    I(VendingMachine, {Snack:BagOfChips})
		, pretzelMachine: I(VendingMachine, {Snack:BagOfPretzels})
	})

	, ({pretzelMachine: PretzelMachine}) => ({
		pretzelAndSodaMachine: I(PretzelMachine, {Drink:Soda})
	})

){...}

Note that the Inject() function has been aliased to I().

In this example, the normal dependency list specifies a dependency that will resolve to an instance of VendingMachine, available on the .vendingMachine property of the instance. .sodaMachine, .waterMachine and so on will then use VendingMachine as their base class.

This allows us to later inject different VendingMachine subclasses (which may have different behaviors) into the VendingMachineTest.

Multiple dependency cascades can be can be defined to accommodate any level of complextiy. Each one will contribute to, and read from the same pool of dependencies.

Please note the double bubble notation on the arrow function: ({ })=>({ }). This allows us to 1: Destructure our static dependency list into variables, and 2: directly return an object via shorthand, i.e. without an explict return.

License

cv3-inject © Sean Morris 2019

All code in this package is relased under the Apache 2.0 licence.