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

Unregistered functions #32

Open
levbrie opened this issue Feb 19, 2015 · 8 comments
Open

Unregistered functions #32

levbrie opened this issue Feb 19, 2015 · 8 comments
Labels

Comments

@levbrie
Copy link
Contributor

levbrie commented Feb 19, 2015

@zgleicher asked: For the test, "shouldn't treat the parameters in nested functions as dependencies" in DI.spec.js simple, parameters 'app' and 'login' have not been registered. 'login' does not even exist. Should it be Auth and User instead?

My response:

@zgleicher Yes, it probably should be Auth and User but your tests should pass regardless. I believe at some point I renamed app and and long to Auth and User, but ended up neglecting this test because it passes anyway, since this particular test is testing how many arguments your inject method has parsed and making sure it doesn't parse nested functions, rather than actually making use of any of those nested injected functions as it does in other tests. Good catch!

@levbrie levbrie added the hw3 label Feb 19, 2015
@zgleicher
Copy link

Thanks!

@emmalovato
Copy link

I'm having a problem with this one though--because those two functions haven't been registered as dependencies, and another test is "shouldn't resolve unregistered dependencies", my inject function ignores them and only parses 1 argument as a result. is there a way to work around this?

@levbrie
Copy link
Contributor Author

levbrie commented Feb 23, 2015

@emmalovato The key here is that you want to ignore the functions being passed in when you call inject that are not actually functions by not trying to replace them with registered functions. However, the function that you return from inject should not be affected by this. If that function returns the arguments that were originally passed in, you don't want to override that. You just want to make sure that any registered functions that were passed in can actually be called from inside the function. Make sense?

@emmalovato
Copy link

maybe--are you saying we should keep the array the right length but just add an undefined element if inject is called with an unregistered parameter? I'm not really sure what you mean by "If that function returns the arguments that were originally passed in, you don't want to override that"

@jeanmariano
Copy link

the test failed for me because it is doing what (I think) you want to do, to ignore the unregistered functions, so it actually returns 1 because only Auth is defined in the parameters. but the test file expects inject() to equal to 3. is that a typo or am I missing something?

@levbrie
Copy link
Contributor Author

levbrie commented Feb 23, 2015

@emmalovato @jeanmariano Sorry, I should have been clearer about this at the beginning. This is admittedly confusing.

The way to handle this is, as @emmalovato suggests, to add an undefined element if inject is called with an unregistered parameter. If you add an inject function that used Array's map() method for example, you might have something like this for your inject function:

function inject (func) {
  var params = parseFunctionParams(func);

  return function () {
    var dependenciesToPassIn = params.map(function(param) {
      // logic to find out if this param is a registered function
      // either in this module or in one of the modules that this
      // module depends on
      // if so, return the registered function, something like:
      // if (funcIsRegistered(param)) {
      //   return getRegisteredFunc(param);
      // }
    });
    // we now have an array of dependenciesToPassIn, in order, where
    // any unregistered dependencies will be passed in as undefined
    return func.apply(null, dependenciesToPassIn);
  };
}

The above is just a suggestion for the inject function. You probably want to first try to look up the registered function on the current module and return it if you find it, and then, if that fails try to look it up on modules that the current module requires. Either way, if you map the parameters to registered functions, the default for parameters where you don't find a function will be undefined, so you'll end up with the arguments that are passed including undefined in each place where the dependency to inject doesn't exist, something like:

[ undefined, undefined, [Function: Auth] ]

I hope this helps. If you're still confused please reply to this issue again.

@emmalovato
Copy link

Great, this definitely clears the issue up for me at least. The only thing I was doing differently was neglecting to add the undefined parameter, so my array of dependencies to pass in in that example just looked like
[ [Function: Auth] ]
and was failing that test because its length was too small, rather than too large like the test intended to check. But now I understand. Thanks!

@levbrie
Copy link
Contributor Author

levbrie commented Feb 24, 2015

@emmalovato Awesome, glad to hear it!

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

4 participants