kessler / darkmagic

An experimental opinionated dependency injection module
MIT License
21 stars 0 forks source link

depending on a dependency that returns a function in an async scenario #6

Closed kessler closed 9 years ago

kessler commented 9 years ago

This is confusing:

Dependency d3 was required by another d1 which also required d2 d2 required d1 as well but d2 is asynchronous. DM would queue d3 as new dependency when resolving d1 but it would also queue d3 as new when resolving d2.

d3 exports a function.

While resolving d2 DM will resolve and cache d3 all of this is done before d1 is resolved. Now the cache has the exported function of d3 but the d1 resolution process still thinks its new so it rerequire it...

This cause an intended circular dependency error.

code:

d1.js

require('darkmagic').inject(function(d2, d3) {

})

d2.js

module.exports = function(d3, callback) {
    callback(null, {})
})

d3.js

module.exports = function() {
    return function () { 
       // this function is reinjected in d1 resolution 
    }
}