ColumbiaJS / js-course

Columbia Computer Science Course in Advanced Javascript as Taught By Lev Brie
23 stars 4 forks source link

Circular Dependencies #38

Closed linanqiu closed 9 years ago

linanqiu commented 9 years ago

The test case

      it('loads each module only once', function () {
        var moduleA = DI.module('moduleA', ['moduleB']),
          moduleB = DI.module('moduleB', ['moduleA']);
      });

doesn't actually check for circular deps. Should we expect one that checks for that?

levbrie commented 9 years ago

@linanqiu There isn't actually a test checking for circular dependencies - the reason that I mention it is that, depending on the way you implement your dependency lookups, it's possible to loop forever looking for functions on a module's registered dependencies. As long as you're not looping forever, you should be good to go.

Best, Lev

linanqiu commented 9 years ago

Thanks prof! So we should allow the following to go through

DI.module('moduleA', ['moduleB']);
DI.module('moduleB', ['moduleA']);

And then prevent looping forever in the injection?

levbrie commented 9 years ago

@linanqiu Exactly. Essentially, each module is going to have a set of modules that are available to it. You can think of these as the complete recursive list of their dependencies if you like, or as the set of "loaded modules" that they can access. However you handle it, your object needs to be able to loop through all of its dependencies without getting caught in a loop (the same way that when you do a depth-first or breadth-first search on a tree, for example, you've got to know where you've been so that you don't keep going over the same nodes again and again). The simplest way to handle this is probably in the injection, since you can't handle this right at module creation (since the full set of modules that might be required in by a module isn't set until all modules have been declared).

linanqiu commented 9 years ago

Thanks @levbrie !