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

mainline

v0.1.1

Published

Yet another javascript DI framework.

Downloads

1

Readme

mainline

Yet another javascript DI framework

Install

$ npm install --save mainline

Compatible with Node v6.2 and up.

Usage

import Mainline, {inject, injectable} from 'mainline';

const SOME_VALUE = 'path/to/something';

Mainline.registerVariable(SOME_VALUE, 'SOME_VALUE');

@injectable()
class DataStore {
  constructor() {
    ...
  }
}

@inject(['DataStore', 'SOME_VALUE'])
class DataService {
  constructor(datastore, value) {
    this.datastore = datastore,
    this.value = value;
  }
}

const data = new DataService(); // Mainline automatically provides parameters

Documentation

Mainline

#register

Registers the target and makes it globally available.

Mainline.register(someFunction, options);

Arguments

  • target (Object|Class|Function|Primitive) target to make available throughout the app.
  • options (Object)
    • alias optional (String) Name by which to resolve it by. If none is provided the function or class name is used.
    • type optional (injectTypes) You can specify if it is a CLASS (Default) | SINGLETON | FUNC | VALUE.

Returns

  • target (Object|Class|Function|Primitive) The target.

#registerFunc

Registers a target function and makes it globally available.

Mainline.registerFunc(target, alias);

Arguments

  • target (Function) Function to make available throughout the app.
  • alias optional (String) Alias for the function. If not provided the function name is used.

Returns

  • target (Object|Class|Function|Primitive) The target.

#registerVariable

Registers a target variable and makes it globally available.

Mainline.registerVariable(target, alias);

Arguments

  • target (Object|Primitive) Instance to make available throughout the app.
  • alias required (String) Alias for the variable.

Returns

  • target (Object|Primitive) The target.

#registerSingleton

Registers a target class/constructor and makes it globally available.

Mainline.registerSingleton(target, alias);

Arguments

  • target (Class|constructor) class to make available throughout the app. After it is instantiated the first time the same instance is reused.
  • alias optional (String) Alias for the class. If not provided the class/constructor name is used.

Returns

  • target (Object) The target.

#resolve

Method returns a resolver with which to generate the required objects or values.

Mainline.resolve(targetName1, targetName2, ...)

Arguments

  • targetName (String) Names of objects to resolve.

Returns

  • Mainline (Object) This is an object that can be used to retrieve or create dependencies by their name or alias.

#get

Instance method that will give access to those objects specified in resolve method.

const resolver = Mainline.resolve('Item1', 'Item2');
const item1 = resolver.get('Item1').item;

Arguments

  • name (String) Name or alias of dependency.

Returns

  • Resolver (Object) An instance of a resolver which wraps the dependency.

Resolver

The resolver is a wrapper for a dependency which will allow you to create instances of the dependency (if it is a class) and override any function/constructor parameters.

#withParams

Used to provide parameters to bind to a function or pass to a constructor. Parameters must be provided in the order the target expects them.

const func = resolver.get('func1').withParams(param1, param2, ...).item;

Arguments

  • param (Any) Any type needed by the target.

#item

Property used to get an instance of or reference to the desired target.

const obj1 = resolver.get('Class1').item;
const obj2 = resolver.get('Class2').withParams(true, 42).item;

Returns

  • A reference to the required dependency.

@injectable

A decorator which can be used to register any class as an injectable dependency.

@injectable()
class Thing1 {
  ...
}

@injectable('Thing2', injectTypes.SINGLETON)
class Thingy{
  ...
}

Arguments

  • alias optional (String) An alias to the dependency used to inject it later. If one is not provided the class name is used by default.
  • injectType optional (Symbol) Used to specify what kind of dependency it is. CLASS (default) | SINGLETON. SINGLETON will guarantee the same instance will be returned throughout the application.

@inject

A decorator to specify that registered dependencies should be injected into the constructor. Parameters will be automatically passed whenever the class is instantiated.

@inject(['Thing1', 'Thing2'])
class Story {
  constructor(thing1, thing2) {
    ...
  }
}

const story1 = new Story(); // No need to provide parameters.
const story2 = new Story(undefined , 'OtherThing') // You can override injected parameters.
// Undefined params will be replaced with an injected instance.

injectTypes

A collection of Symbols used specify different kinds of dependencies. CLASS | SINGLETON | FUNC | VALUE

Examples

Coming Soon