Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection #8

Closed
Radnen opened this issue Apr 16, 2014 · 6 comments
Closed

Dependency Injection #8

Radnen opened this issue Apr 16, 2014 · 6 comments
Labels

Comments

@Radnen
Copy link
Member

Radnen commented Apr 16, 2014

I think sphere stages should have the ability to load in a dependency module without extra boiler-plate code.

var module = new Sphere.Module("my module", ["dependent", function(dependent) {
    return {
        calc: function(n) { return dependent.exec() * n; }
    }
}]);

var stage = new Sphere.Stage("my-stage", ["my module", {
    load: function(module) {
        this.a = module.calc(5);
    },
}]);
@FlyingJester
Copy link
Member

Very Python-esque. I like it, but I am having a difficult time imagining the actual implementation.

@Radnen
Copy link
Member Author

Radnen commented Apr 16, 2014

I'm sure the implementation would be something like:

var modules = [];

function AddModule(name, module) {
    modules[name] = module;
}

Then when you implement creating a stage:

function CreateStage(name, stageparams) {
    var stage = stageparams[stageparams.length-1];
    var dependencies = [];
    for (var i = 0; i < stageparams.length - 1; ++i) {
        dependencies[i] = modules[stageparams[i]];
    }
    stage.load = stage.load.bind.apply(stage, dependencies);
    stage.enter = stage.enter.bind.apply(stage, dependencies);
    stage.leave = stage.leave.bind.apply(stage, dependencies); // etc.
    modules[name] = stage;
    return stage;
}

Of course we can get more elegant than that, but at least it shows you we have to utilize a hash map of modules to module names, and then when a stage is created, resolve the modules from it's dependency list. The API doesn't have to be like the above, I'm just doing it in the angular way, that's all.

Also notice I made a stage a module, because technically they would be no different. I just think that a stage is more of an interface. A stage is a module but it must have load/enter/leave functions whereas a standard module can have really anything.

@joskuijpers
Copy link
Contributor

For some reason, I have NO clue what this is about.

One this i am sure of, we can't name them modules because code encapsulation will use a 'module' system (like angular.js, like node.js, etc).

Maybe you mean something alike... But I am not a real fan of how angular does it...

@Radnen
Copy link
Member Author

Radnen commented Apr 16, 2014

Dependency Injection is used to automatically find and resolve dependencies. This is useful for making sure your code uses the libraries it only should use rather than just have access to all code files. http://en.wikipedia.org/wiki/Dependency_injection

Anyways, I think it's easy enough to add in pure js code, so it's not necessarily an engine-level thing unless we intend to add it to #9 somehow.

@joskuijpers
Copy link
Contributor

I am currently trying to implement #9, after that I can see about adding dependency injections.

@joskuijpers
Copy link
Contributor

AFAIK, Angular uses this module system because it is Async: it loads the dependencies and then itself and then executes that code. I don't think we need async loading of libs.
I don't see how dependency injection in such form will help us in the current module/require() system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants