Git Product home page Git Product logo

darkmagic's People

Contributors

kessler avatar sitin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

darkmagic's Issues

circular dependency goes undetected in certain scenarios

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)
    })
}

Insights

A bunch of insights I had so far from experimenting with darkmagic:

  1. Async modules are fun, they make life easy
  2. Being able to selectively replace dependencies makes writing tests easier too
  3. Having one injector per process might be a problem in the long run
  4. "Riding" on the real module system cache has pros and cons (need to expand on that)
  5. Dependency inference needs stronger syntax than just parsing the parameters of a function. Should I give it to something like requirejs' string dependency array?
  6. dependency declaration at the client site needs to be decoupled from dependency declaration at the provider site, more specifically, if site A needs dep named B, this dep might be loaded without actually using the name B, but once loaded it will declare itself as B

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

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 
    }
}

problem when running from node repl

> 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

Node Majic

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!

Modules with underscore symbol doesn't loaded

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.