'use strict';

// Load requirements
const engine = require('../engine');

// Configures and runs the conversion task
module.exports = {

  run: function(job) {
    return new Promise((resolve, reject) => {

      // Check we got a file at all
      if ( ! job ) {
        return reject('No job data provided');
      }

      // Configure the conversion engine command
      let command = engine.config(job.file, job.options, job.meta, (err) => {
        return reject(err);
      });

      // Bind events
      ['start', 'filenames', 'progress', 'error', 'end'].forEach((e) => {
        return command.on(e, () => {
            
          // Format arguments to include details and metadata
          let args = Array.prototype.slice.call(arguments);
          args.push(job, job.meta);
            
          // Call event callback with args
          this['on' + e].apply(this, args);
        });
      });

      // Start processing the job
      return command.run();
    });
  },

  onstart: require('./events/start'),

  onfilenames: require('./events/filenames'),

  onprogress: require('./events/progress'),

  onerror: require('./events/error'),

  onend: require('./events/end')

};

@@ -68,73 +68,296 @@
 ne')
-;%0A%0A// Configures and runs the conversion task%0Amodule.exports = %7B%0A
+,%0A      moment = require('moment');%0A%0A// Export for use%0Amodule.exports = %7B%0A%0A  // Helps avoid errors when engine quits without starting%0A  progressBar: %7Bterminate: function() %7B %7D%7D,%0A%0A  // Track the time since conversion began%0A  started: moment(),%0A%0A  // Configures and runs the conversion task
 %0A  r
@@ -830,13 +830,18 @@
 (e, 
-() =%3E
+function()
  %7B%0A 
@@ -1115,24 +1115,35 @@
 );%0A        %7D
+.bind(this)
 );%0A      %7D);

Fix data bindings within event callbacks on conversion