rn-stash

1.0.2 • Public • Published

stash

stash is an angularjs-style dependency injection framework for JavaScript that makes it easy to define modules, singletons and constants. All dependencies are resolved on demand, freeing you from loading your code in any particular order.

This project was ported from "lukevenediger/dingu" to TypeScript as one of my side projects while learning TypeScript. It is a great framework!

Features

  • Bring in dependencies simply by naming them as a function parameter
  • Load your javascript files in any arbitrary order
  • Store singleton instances
  • Store constant values like settings and magic numbers
  • Protects against circular dependencies
  • Can be locked to prevent further changes at runtime.

How It Works

Example 1: ModuleA depends on ModuleB

rnStash.module('ModuleA', function(ModuleB) {
  return {
    doSomething: function() {
      return 'Hi from ModuleA (' + ModuleB.doSomething() + ')';
    }
  };
});
 
rnStash.module('ModuleB', function() {
  return {
    doSomething: function() {
      return 'Hi from ModuleB';
    }
  };
});

Next we'll ask stash for an instance of ModuleA and call the doSomething method.

var a = rnStash.get('ModuleA');
console.log(a.doSomething());

Example 2: Using Singletons

stash lets you define a module that will only initialise once, and always return the same instance back. Any dependencies it needs will be resolved and injected the first time it's created.

rnStash.singleton('OneOnly', function() {
  var counter = 0;
  return {
    nextID: function() {
      counter += 1;
      return counter;
    }
  }
});
 
console.log(rnStash.get('OneOnly').nextID() === 1); // true
console.log(rnStash.get('OneOnly').nextID() === 2); // true

Example 3: Storing Constant Values

Use stash to store settings, magic numbers and other common values and include them in your modules by name.

// Store a string
rnStash.value('apiUrl', 'http://foo.com/api');
 
// or an object - stash isn't fussy
rnStash.value('systemInfo', {
  apiVersion: '1.2',
  token: 'aab23510' 
});
 
// And then have them ready in your module
rnStash.module('apiClient', function(apiUrl, systemInfo) {
  // profit!
});

Download and Install

There are many ways to get stash:

  • Download stash.js
  • Install it via npm - npm install rn-stash

Project Information

Maintainers:

Original Code / Developer:

License:

  • MIT

Package Sidebar

Install

npm i rn-stash

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • niemandr