Task Extended

Run a node script in a separate process.

Used by the fuzzy-finder and find in project.

For a real-world example, see the scan-handler and the instantiation of the task.

Examples

In your package code:

[Task](../Task/) = require 'atom'

task = Task.once '/path/to/task-file.coffee', parameter1, parameter2, ->
  console.log 'task has finished'

task.on 'some-event-from-the-task', (data) =>
  console.log data.someString # prints 'yep this is it'

In '/path/to/task-file.coffee':

module.exports = (parameter1, parameter2) ->
  # Indicates that this task will be async.
  # Call the `callback` to finish the task
  callback = @async()

  emit('some-event-from-the-task', {someString: 'yep this is it'})

  callback()

Methods

.once(taskPath, args)

A helper method to easily launch and run a task once.

Argument Description

taskPath

The String path to the CoffeeScript/JavaScript file which exports a single Function to execute.

args

The arguments to pass to the exported function.

Return values

Returns the created Task.

::constructor(taskPath)

Creates a task. You should probably use {.once}

Argument Description

taskPath

The String path to the CoffeeScript/JavaScript file that exports a single Function to execute.

::start(args, callback)

Starts the task.

Throws an error if this task has already been terminated or if sending a message to the child process fails.

Argument Description

args

The arguments to pass to the function exported by this task’s script.

callback

optional

A Function to call when the task completes.

::send(message)

Send message to the task.

Throws an error if this task has already been terminated or if sending a message to the child process fails.

Argument Description

message

The message to send to the task.

::on(eventName, callback)

Call a function when an event is emitted by the child process

Argument Description

eventName

The String name of the event to handle.

callback

The Function to call when the event is emitted.

Return values

Returns a Disposable that can be used to stop listening for the event.

::once(taskPath, args)

A helper method to easily launch and run a task once.

Argument Description

taskPath

The String path to the CoffeeScript/JavaScript file which exports a single Function to execute.

args

The arguments to pass to the exported function.

Return values

Returns the created Task.

::terminate()

Forcefully stop the running task.

No more events are emitted once this method is called.

::cancel()

Cancel the running task and emit an event if it was canceled.

Return values

Returns a Boolean indicating whether the task was terminated.