0.0.18 • Published 7 years ago

flip-flop v0.0.18

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Flip Flop

Just Another IOC Library

This Package is Deprecated, please use @flipflop/core and @flipflop/node packages

Thanks to everyone that tried out flip flop. This package wasn't browser friendly so I've decided to break out the dependency injection code from the node-centric stuff for better modularity and clarity. Tentatively there will be a @flipflop/core module and a @flipflop/node module. @flipflop/browser and @flipflop/server modules are on the roadmap as well but may not see the light of day for a while.

flip-flop is just another ioc library

* in progress

Install

npm install --save flip-flop

Usage

Tell flipflop where your dependencies are

let flipflop = require('flip-flop');

flipflop('./dist/*.js')
  .load(function(){
    console.log('everything loaded!');
  });

flipflop loads your files and takes care of supplying your (a)sync dependencies when they're ready.

Just export a function in your js files

module.exports = function(flipflop){
  app('my.module')
    .modules('my.otherModule')
    .collections('collection1', 'collection2')
    .when(function(myOtherModule, collection1, collection2){
      // do something with your dependencies
    })
}

return an A+ compliant thenable (a promise) if your module has to do some async stuff

module.exports = function(app){
  app.module('d')
    .when(function(){
      return new Promise(function(resolve, reject){
        setTimeout(function(){
          resolve('D');
        }, 3000);
      });
    });
  });
}

Why?

let email = require('../../../../../../../lib/modules/components/settings/email.js');

That's why.

You can probably come up with a glob to match all of your project files in a few seconds so just tell flip-flop what you want and stop worrying about where your code is.

Features

Asyncronous Dependencies

As long as your module constructor returns an A+ compliant thenable then any modules that depend on it won't load until it resolves.

log.txt
This is useful log output
a.js
module.exports = function(app){
  app('a')
    .when(function(){
      return new Promise(function(resolve, reject){
        fs.readFile('log.txt', 'utf8', function(err, file){
          if(err){
            return reject(err);
          }
          return resolve(file);
        });
      });
    });
};
b.js
module.exports = function(a){
  app('b')
    .modules('a')
    .when(function(a){
      console.log(a); // This is useful log output
    });
};

Returning anything but a promise lets flip-flop know that your module is ready right away.

Collections

With flip-flop you register and depend on collections. This is useful when you want to create multiple modules that share something in common

server.js
module.exports = function(app){
  app('restify')
    .collections('controllers')
    .when(function(controllers){
      return new Promise(function(resolve, reject){
        var restify = require('restify');
        var server  = restify.createServer();
        server.get('/c1', controllers[0].value);
        server.get('/c2', controllers[1].value);
        server.listen(8080, function(){
          resolve(server);
        });
      });
    });
};
controller1.js
module.exports = function(app){
  app('hello-controller')
    .when(function(){
      return function(req, res, next){
        res.send('hello');
      }
    })
};
controller2.js
module.exports = function(app){
  app('goodbye-controller')
    .when(function(){
      return function(req, res, next){
        res.send('goodbye');
      }
    })
};

API

flipflop(String) | FlipFlop

Pass a file glob to flipflop which matches all of the files you wish to load. A FlipFlop instance will be returned with methods for loading your files and configuring your settings

const flipflop = require('flipflop');
const fl = flipflop('**/*.js');

flipflop(Array) | FlipFlop

Pass an array of file globs to flipflop which matches all of the files you wish to load. A FlipFlop instance will be returned with methods for loading your files and configuring your settings

const flipflop = require('flipflop');
const fl = flipflop([
  'dist/**/*.js',
  'vendor/some-file/some-file.js'
]);

flipflop uses glob-all to parse your globs

FlipFlop.options(Object) | FlipFlop

TODO

  • Better Reject() logic
  • Write Tests
  • Make it Global
  • Support for a config file instead of options
  • Travis
  • Coveralls See See
0.0.18

7 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago