Giter Club home page Giter Club logo

simple-ioc's Issues

inject error handling problem when callback provided

When inject with callback is invoked there is problem with error handling ex. I have

var ioc = require('../index')
    .setSettings(
    {
        log: {
            output: 'consoleReadable'
        }
    })
    .getContainer()
    .registerInjectable({
        module1: function () {
            return 'module1'
        },
        module2: function () {
            return 'module2'
        }
    });

then i have 2 cases

1 scenario: inject error without callback

ioc.inject(function(module1,module3){
    return module1+'/'+module3
})

output is like this:
FATAL (resolver) Cannot resolve dependencies for anonymous function: {"message":"Component unresolvable","context":{"name":"module3","errors":[{"problemType":"notRegistered"}]}}
cool works as expected

2 scenario: injecterror with callback

ioc.inject(function (module1, module3) {
    return module1 + '/' + module3
}, function cb(err, instance) {
    console.error(err)
})

output from this case is like this:

C:\_projects\simple-ioc\lib\containerHelpers\resolver.js:22
                                return resolvedNonReservedDependencies[ dependency.name ];
                                                                      ^
TypeError: Cannot read property 'module1' of undefined
    at C:\_projects\simple-ioc\lib\containerHelpers\resolver.js:22:43
    at Array.map (native)
    at Object.pub.resolveInjectableComponent (C:\_projects\simple-ioc\lib\containerHelpers\resolver.js:20:83)
    at C:\_projects\simple-ioc\lib\containerHelpers\resolver.js:62:9
    at Immediate.<anonymous> (C:\_projects\simple-ioc\lib\genericHelpers\errRerouter.js:5:5)
    at Immediate.immediate._onImmediate (timers.js:409:16)
    at processImmediate [as _immediateCallback] (timers.js:358:17)

it's no good because framework reports some problem with module 1 that is perfectly correct and no mention about module3

i think that in resolver.js code there is no need to check if callback exists it is good enough to check error and log it fatal if something goes wrong

pub.inject = function( store, fn, parentName, callback ) {
        var component = injectableComponent.get( fn );
        resolveDependencies( store, 'anonymousFunction', component.dependencies.nonReserved, function( err, resolvedNonReservedDependencies ) {
            if( err && !callback )
                log.fatal( 'Cannot resolve dependencies for anonymous function', err );
            else
                pub.resolveInjectableComponent( parentName, component, resolvedNonReservedDependencies, function( err, instance ) {
                    if( callback )
                        callback( err, instance );
                } );
        } );
    };

change like below fix problem

(...)
    if( err)
                log.fatal( 'Cannot resolve dependencies for anonymous function', err );
            else
                pub.resolveInjectableComponent( parentName, component,
(...)

please consider this fix and btw Can You publish newest version to npm registry? I see only 3.0.16

Nested modules with same name, AKA namespacing

Hello, I'm trying to convert myself to using simple-ioc, from jaredhanson/electrolyte, a functionality that seems simple but quite powerful is natural namespacing via strings.

Example follows:

module.exports = exports = function (listBlogposts) {
  // clever controller logic
  return {};
};
exports['@require'] = ['api/list-blogposts'];

In case I also have a controller for the same operation, but returns a page (instead of an API), i can do this:

module.exports = exports = function (listBlogposts) {
  // clever controller logic
  return {};
};
exports['@require'] = ['controller/list-blogposts'];

// or even

module.exports = exports = function (apiList, ctrlList) {
  // clever controller logic
  return {};
};
exports['@require'] = ['api/list-blogposts', 'controller/list-blogposts'];

So I can inject different modules with the same name, or same modules with different names, a lot of flexibility there.

Seems with simple-ioc all modules should have different names, like apiListBlogposts and controllerListBlogposts in the above example.

This leads to verbosity that in a medium-sized project is not always helpful.

Am I missing something?

How do I know if some of the dependencies are impossible to resolve?

As a simple-ioc user who writes a bunch of system/integration tests, I want the container to tell me if something is wrong with the configuration aloud. Here's a piece of code I have:

var ioc = require('simple-ioc');

describe('simple-ioc', function() {
  it('should fail when there is no dependency', function(done) {
    ioc
      .getContainer()
      .inject(function(a) { // right, there's no "a"
        done();
      });
  });
});

When this test gets started, Mocha just silently shuts down. Neither my "inject" callback is called, nor any tests after this one get executed. I'm on Node v0.11.14, simple-ioc version is 3.0.13.

Minification

Hey guys,

i just noticed that there might be an issue with minifing your code.
The problem is that the parameters passed to the functions must be named as the service, because if the file gets minified the parameters get shortnames.

So something like this:

var container = require('simple-ioc').getContainer();
container.autoRegisterPath( './services' );
container.inject(function(myService){
    myService.print("Hello World");
});

would turn to this:

container.inject(function(a){
    a.print("Hello World");
});

AngluarJS has a solution where you name the services first you want to inject. Just like this:

TestApp.controller("TestController", ["myService", function (myService) {
    myService.print("Hello World");
}]);

would turn to this:

TestApp.controller("TestController", ["myService", function (a) {
    a.print("Hello World");
}]);

Would be nice to have something similar since minification is used in many projects.

Thanks

Trigger error when dependencies cannot be resolved

Hi thanks for this wonderful library. I was using older version for quite long time. Recently updated to latest (still 2 years old ;-).

Anyway my problem is that I would like to detect when injectable declares non existing dependency. Now it seems container just dies.

How to reproduce:

var container = require( 'simple-ioc' )
    .getContainer()
    .registerIocLog('log')
    .registerInjectable( {
        transientModule: function( parentName ) {
            return function() {
                console.log( parentName );
            };
        },
        singletonModule1: function( transientModule, nonExisting ) {
            return transientModule;
        },
        singletonModule2: function( transientModule ) {
            return transientModule;
        }
    } )
    .inject( function( singletonModule1, singletonModule2 ) {
        singletonModule1(); // Will output "singletonModule1"
        singletonModule2(); // Will output "singletonModule2"
    } );

inject is never called because of nonExisting dependency. However without any warning message,it is hard to detect which module has wrong dependencies.

More usable callback in inject

Hi , can You take a look at inject function in resolver.js

pub.inject = function( store, fn, parentName, callback ) {
        var component = injectableComponent.get( fn );
        resolveDependencies( store, 'anonymousFunction', component.dependencies.nonReserved, function( err, resolvedNonReservedDependencies ) {
            if( err )
                log.fatal( 'Cannot resolve dependencies for anonymous function', err );
            else
                pub.resolveInjectableComponent( parentName, component, resolvedNonReservedDependencies, function( err, instance ) {
                    if( callback )
                        callback();
                } );
        } );
    };

In my opinion callback at the end should be invoked with parameters err and instance
like in snippet below.

pub.inject = function( store, fn, parentName, callback ) {
        var component = injectableComponent.get( fn );
        resolveDependencies( store, 'anonymousFunction', component.dependencies.nonReserved, function( err, resolvedNonReservedDependencies ) {
            if( err )
                log.fatal( 'Cannot resolve dependencies for anonymous function', err );
            else
                pub.resolveInjectableComponent( parentName, component, resolvedNonReservedDependencies, function( err, instance ) {
                    if( callback )
                        callback(err,instance);
                } );
        } );
    };

Now it has very limited use but this simple change gives opportunity to inject function, return value and for example wrap it with promise like below:

var container = require( 'simple-ioc' );
/*
... container initialization
*/
var newInject=function(fn){
            return new Promise(function(resolve,reject){
                container.inject(fn,function(err,instance){
                    if(err){
                        reject(err);
                    }else{
                        resolve(instance);
                    }
                })
            })
        }
}

this approach works for me , but maybe I've overlooked some concept

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.