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

Simplify Task command API #29

Open
jeff-h opened this issue Jun 26, 2014 · 3 comments
Open

Simplify Task command API #29

jeff-h opened this issue Jun 26, 2014 · 3 comments

Comments

@jeff-h
Copy link
Member

jeff-h commented Jun 26, 2014

As @perifer pointed out in #28 the Task command could benefit from simplification.

At present the JS API reflects the Objective-C mindset where you create a new NSTask instance, then call methods on it to configure it.

A more JavaScript-y API (using jQuery as a model) would probably be:

  • a one-line call, with multiple arguments
  • ideally condensing most of the config into a settings object
  • I also hope we can naively split the command on space characters to get the arguments array that NSTask wants
  • in my tests at least, setting pipeOutput to true worked fine even if the command returned no results, so is it safe to default this to true, or simply not provide the option to not set it to false?

I propose something like:

var myTask = MacGap.Task.launch('/bin/ls -la /tmp', {
    success: myTaskCompleted,
    error: myTaskFailed,
});

function myTaskCompleted(stdout) {
    console.log(stdout);
};

or else just the one callback:

var myTask = MacGap.Task.launch('/bin/ls -la /tmp', myTaskCompleted);

function myTaskCompleted(resultCode, stdio) {
    console.log(stdio);
};

I'm happy to do the refactoring.

@jeff-h jeff-h mentioned this issue Jun 26, 2014
@rawcreative
Copy link
Member

The Task api is written the way it is for flexibility, the requirements of NSTask itself, and the restrictions of JavaScriptCore. While it might be nice to have a more consolidated one line command, it's not possible or practical. Separating the arguments/calls the way they are, allows for flexibility in not only the commands that can be run, but how arguments are passed to those commands.

Bundling the command call and command arguments together won't work because NSTask requires the arguments to be passed as an NSArray. So that means before the arguments are passed to NSTask, they'd have to be parsed and split up into an NSArray. This would be fine if every command accepts the same type, format, and position of arguments, but they don't. So there's no way to reliably parse the arguments for every possible task.

Grouping callbacks together in a single object, or in a config object won't work either because of the limitations of JavaScriptCore. JS Objects containing functions don't get converted properly and the function is lost upon conversion.

@jeff-h
Copy link
Member Author

jeff-h commented Jun 27, 2014

So would there be a problem with something like this?

var myTask = MacGap.Task.launch('/bin/ls', ['-la', '/tmp'], myTaskCompletedCallback);

@perifer
Copy link
Contributor

perifer commented Jun 27, 2014

That is actually nearly identical to http://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
child_process.execFile(String file, [Array args], [Object options], [Function callback])

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

3 participants