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

returning variables from prerequisites? #199

Closed
evantahler opened this issue Jul 7, 2013 · 2 comments
Closed

returning variables from prerequisites? #199

evantahler opened this issue Jul 7, 2013 · 2 comments

Comments

@evantahler
Copy link

I would like to create one jake task to pre-load my environment, and then use that as a prerequisite for other tasks (a very rails-y pattern). However, since none of the jake files share a namespace, I would like to return an 'environment' variable from my environment perquisite to my main task:

An prototypical environment loader:

task("envrionment", {async: true}, function() {
  app = require("myApp.js");
  app.initialize(function(err, api){
    if(err == null){ complete(err, api); }
  });
});

I tried 2 methods of "catching" the returned api variable, but to no avail:

desc("otherTask: prereq");
task("otherTaskPrereq", ["envrionment"], {async: true}, function(err, api){
  console.log(api)
});

desc("otherTask: invoke");
task("otherTaskInvoke", {async: true}, function(){
  var env = jake.Task["envrionment"];
  env.addListener('complete', function(err, api){
    console.log(api)
  });
  env.invoke();
});

Is such a thing possible?

@mde
Copy link
Contributor

mde commented Jul 8, 2013

There hasn't really been a nice way to handle this. I've just modified Task to include a "value" property which will be the value passed to complete for async tasks, and the return value of the action-function itself for sync tasks. This value will be passed when emitting the "complete" event. (We have a separate "error" event for tasks, so the (err, data) convention doesn't make sense here.)

After a task is completed, this value will be available in the ".value" property on the task. Calling reenable on the task will clear this value.

I've just pushed these changes to NPM in v0.5.17.

Thanks for the input!

@mde mde closed this as completed Jul 8, 2013
@evantahler
Copy link
Author

Awesome! Thanks for the fast turn-around, @mde. I'll be working to bake jake into https://github.com/evantahler/actionhero as a first-class citizen this week.

For those of you who got here from google, that means that you can now do this (from my example above):

desc("otherTask: prereq");
task("otherTaskPrereq", ["envrionment"], {async: true}, function(){
  api = jake.Task["envrionment"].value;
  console.log(api)
});

desc("otherTask: invoke");
task("otherTaskInvoke", {async: true}, function(){
  var env = jake.Task["envrionment"];
  env.addListener('complete', function(){
    var api = env.value;
    console.log(api)
  });
  env.invoke();
});

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

No branches or pull requests

2 participants