kessler / darkmagic Goto Github PK
View Code? Open in Web Editor NEWAn experimental opinionated dependency injection module
License: MIT License
An experimental opinionated dependency injection module
License: MIT License
In a situation like this:
index.js
var darkmagic = require('darkmagic')
darkmagic.inject(function(y, callback) {
callback()
})
y.js
module.exports = function($injector, callback) {
console.log('y')
$injector.inject(function (z) {
callback()
})
}
z.js
module.exports = function (y, callback) {
console.log('z')
setImmediate(function() {
setImmediate(callback)
})
}
module.exports = function (callback) {
callback()
callback() // should throw an error
}
A bunch of insights I had so far from experimenting with darkmagic:
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
}
}
> var darkmagic = require('darkmagic')
undefined
> darkmagic.inject(function(mvc) { })
TypeError: Cannot read property 'filename' of undefined
at Injector._initSearchPaths (/Users/kessler/dev/ironsrc/premium/bitfort/nwapp/node_modules/darkmagic/lib/Injector.js:244:41)
at new Injector (/Users/kessler/dev/ironsrc/premium/bitfort/nwapp/node_modules/darkmagic/lib/Injector.js:22:7)
at newInjector (/Users/kessler/dev/ironsrc/premium/bitfort/nwapp/node_modules/darkmagic/index.js:23:17)
at Object.inject (/Users/kessler/dev/ironsrc/premium/bitfort/nwapp/node_modules/darkmagic/index.js:10:17)
at repl:1:12
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
because:
Injector.prototype._initSearchPaths = function () {
this._searchPaths = []
var rootDir = path.dirname(require.main.filename)
.
.
.
}
needs a || process.cwd() or something like that
Hey Kessler. Author of Node Majic here. I just found your project. Ever since writing and publishing Node Majic, I thought I might be a bit crazy, as I have found countless dependency injection frameworks for Node, but none approach it the way I did with Majic.
Until now!
Darkmagic and Majic appear to have very strong similarities (even the name!).
Curious your thoughts on Node Majic?
Perhaps we could join forces? It's disappointing to me to see so many Node dependency libraries out there, that require so much boilerplate. IMO it turns developers off, and they end up just using require. In my experience, require works fine for small (very small) projects. And is a mess once your project gets off the ground, needs real testing and mocking etc.
Anyway, cheers, glad to finally see that I'm not entirely crazy!
The following:
require('darkmagic').inject(function(child_process) {
child_process.fork('smth');
});
causes an error:
/Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/node_modules/darkmagic/lib/Dependency.js:102
throw new Error(util.format('[%s%s] is Missing dependency [%s]', parentDepe
^
Error: [anonymous] is Missing dependency [child_process]
at Dependency.load (/Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/node_modules/darkmagic/lib/Dependency.js:102:10)
at /Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/node_modules/darkmagic/lib/Injector.js:114:29
at Waterfall.run (/Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/node_modules/darkmagic/lib/Waterfall.js:30:20)
at Injector._inject (/Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/node_modules/darkmagic/lib/Injector.js:104:12)
at Injector.inject (/Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/node_modules/darkmagic/lib/Injector.js:58:7)
at Object.inject (/Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/node_modules/darkmagic/index.js:17:11)
at Object.<anonymous> (/Users/sitin/Developer/Innovecs/MobileCore/tools.mobilecore.com/dbsync/broken.js:1:84)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Process finished with exit code 8
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.