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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sactive-di

v1.3.1

Published

Dependency injector for Node.js.

Downloads

6

Readme

sactive-di

Dependency injector for Node.js.

Build status Coverage NPM version NPM Download License

NPM

Installation

npm install sactive-di

Usage

sactive-di只有已经绑定的实例才可以注入,参数前缀必须是$$,否则会注入null

class Class1 {
  test() {
    return 'test';
  }
}

class Class2 {
  constructor($$class1) {
    this.$$class1 = $$class1;
  }
  test() {
    return 'test';
  }
}


async function asyncFunc() {
  return 'test';
}

async function asyncFunc2($$Class1) {
  return $$Class1.test();
}

class Class3 {
  constructor($$class1, $$class2, $$async, $$async2) {
    this.$$class1 = $$class1;
    this.$$class2 = $$class2;
    this.$$async = $$async;
  }
}

const Di = reuqire('sactive-di');
const di = new Di();
di.bindClass('class1', class1);
di.bindClass('class1', Class2);
di.bindClass('Class3', Class3);
di.bindFunction('async', asyncFunc);
di.bindFunction('async2', asyncFunc2);
let class3 = app.getInstance('$$class3');
class3.$$class2.test() // => 'test'
class3.$$class2.$$class1.test() // => 'test'
class3.$$class1.test() // => 'test'
class3.$$async.then(function(res) {
    console.log(res) // => 'test'
});
class3.$$async2.then(function(res) {
    console.log(res) // => 'test'
});

上面的例子,Class3 注入了四个依赖 $$class1$$class2$$async$$async2Class2 注入了依赖 $$class1

API

bindClass

绑定类,类在调用该方法之后,可以使用getInstance获取到实例。

bindClass(name, class, options);
  • name:String,给绑定的类命名,使用getInstance获取实例时会用到。
  • class:Class,要绑定的类。
  • options:Object,选项:
    • singleton: Boolean,是否是单例,默认是true

注意,绑定的name会自动加上$$符号,获取时要加上$$

class Logger {
  test() {
    return 'test';
  }
}

const di = new Di();
di.bindClass('logger', Logger);
let logger = di.getInstance('$$logger') //注意加上`$$`
logger.test() // => 'test'

bindFunction

绑定方法,可以使用getInstance获取到实例,用于依赖注入。 这里获取到的实例并不是function本身,而是它的执行结果,如果只是绑定function,不需要执行它,使用bindInstance

bindFunction(name, func, options);
  • name:String,给绑定的方法命名,使用getInstance获取实例时会用到。
  • func:Function,要绑定的方法。
  • options:Object,选项:
    • singleton: Boolean,是否是单例,默认是true

注意,绑定的name会自动加上$$符号,获取时要加上$$

async function asyncFunc() {
  return 'test';
}

function func() {
  return 'test';
}

const arrowFunc = () => {
  return 'test';
};

const di = new Di();
di.bindFunction('func', func);
di.bindFunction('async', asyncFunc);
di.bindFunction('arrow', arrowFunc);

di.getInstance('$$func') // => 'test'
di.getInstance('$$async') // => 'test'
di.getInstance('$$arrow') // => 'test'

bindInstance

绑定实例,可以使用getInstance获取实例,用于依赖注入。

bindInstance(name, instance, options);
  • name:String,给绑定的实例命名,使用getInstance获取实例时会用到。
  • instance:any,要绑定的实例,实例可以是任意类型,stringobjectfunctionclass等。
  • options:Object,选项:
    • singleton: Boolean,是否是单例,默认是true

注意,绑定的name会自动加上$$符号,获取时要加上$$

function func() {
  return 'test';
}

class Logger {
  test() {
    return 'test';
  }
}

let student = {
  name: 'xiaoming'
};

let name = 'xiaoqiang';

const di = new Di();
di.bindInstance('func', func);
di.bindInstance('logger', Logger);
di.bindInstance('student', student);
di.bindInstance('name', name);

di.getInstance('$$student'); // => {name: 'xiaoming'}
di.getInstance('$$name'); // => 'xiaoming'
di.getInstance('$$func')(); // => 'test'
let logger = new di.getInstance('$$Logger')();
logger.test(); // => 'test'

getInstance

获取实例,用于依赖注入。 注意,绑定的name会自动加上$$符号,获取时要加上$$

getInstance(name);
  • name:String,实例名,加上$$

getInstances

获取多个实例,用于依赖注入。 注意,绑定的name会自动加上$$符号,获取时要加上$$

getInstances(names);
  • names:Array,一组实例名,加上$$,例如:di.getInstances(['$$logger', '$$test'])

deleteInstance

删除已经绑定的实例。 注意,绑定的name会自动加上$$符号,获取时要加上$$

deleteInstance(name);
  • name:String,实例名,加上$$

deleteInstances

删除已经绑定的多个实例。 注意,绑定的name会自动加上$$符号,获取时要加上$$

deleteInstances(names);
  • names:Array,一组实例名,加上$$,例如:di.deleteInstances(['$$logger', '$$test'])

Tests

Install the dependencies, then run npm test:

npm install
npm test

#coverage
npm run test:cov

TODO

  • Engilsh Documentation