sphere-group / pegasus

The Pegasus API for Sphere 2.0
BSD 2-Clause "Simplified" License
1 stars 0 forks source link

Dependency Injection #8

Closed Radnen closed 8 years ago

Radnen commented 10 years ago

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 commented 10 years ago

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

Radnen commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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

joskuijpers commented 10 years ago

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.