Last active
August 29, 2015 14:01
-
-
Save FireNeslo/872864db4e8856a0a611 to your computer and use it in GitHub Desktop.
A really simple dependency injector based on promises.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AppInjector() { | |
var injector = new Injector(); | |
function deps(func) { | |
return func.length ? func.toString().match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1].replace(/ /g, '').split(',') : []; | |
} | |
this.define = function(name, fn) { | |
return injector.define(name, deps(fn), fn); | |
}; | |
this.require = injector.require; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GenInjector() { | |
AppInjector.call(this); | |
this.async = function async(genFn) { | |
return function() { | |
return new Promise(function(resolve) { | |
var gen = genFn(); | |
(function next(val) { | |
var ret = gen.next(val); | |
if(ret.done) { return resolve(ret.value); } | |
Promise.resolve(ret.value).then(next); | |
}()); | |
}); | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// defines require, define and async globally when used in non strict mode without new | |
GenInjector(); | |
//values | |
define('a', {a:'value'}) | |
//factories | |
define('b', function(a) { | |
a.b = 'factory'; | |
return a; | |
}); | |
//promises | |
define('c', new Promise(function(resolve) { | |
setTimeout(function() { | |
resolve('promise'); | |
}, 2000); | |
})) | |
//generators | |
define('d', async(function* (){ | |
var res = yield require('b'), | |
c = yield require('c'); | |
res.c = c; | |
console.log(res); | |
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (root, factory) { | |
if (typeof define === 'function' && define.amd) { | |
define([], factory); | |
} else if (typeof exports === 'object') { | |
module.exports = factory(); | |
} else { | |
root.Injector = factory(); | |
} | |
}(this, function () { | |
return function Injector(injectors) {'use strict'; | |
if(!this) return new Injector(injectors); | |
function get(name) { | |
if(get[name]) return get[name]; | |
var module = get[name] = Promise.defer(); | |
injectors && injectors.forEach(function(injector) { | |
injector.require(name).then(module.resolve, module.reject); | |
}) | |
return module; | |
} | |
function require(name, locals) { | |
return Array.isArray(name) ? Promise.all(name.map(require)) : get(name).promise; | |
} | |
function define(name, dependencies, fn) { | |
return require(dependencies).then(function(deps) { | |
var defer = get(name); | |
try { | |
defer.resolve(fn.apply ? fn.apply(fn, deps) : fn); | |
} catch(error) { | |
defer.reject(error); | |
} | |
return defer.promise; | |
}); | |
} | |
return this.require = require, this.define = define, this; | |
}; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment