src/conrad.js
function _addJob(v1, v2) {
    var i,
        l,
        o;

    // Array of jobs:
    if (Array.isArray(v1)) {
      // Keep conrad to start until the last job is added:
      _noStart = true;

      for (i = 0, l = v1.length; i < l; i++)
        _addJob(v1[i].id, __extend(v1[i], v2));

      _noStart = false;
      if (!_isRunning) {
        // Update the _lastFrameTime:
        _lastFrameTime = __dateNow();

        _dispatch('start');
        _loop();
      }
    } else if (typeof v1 === 'object') {
      // One job (object):
      if (typeof v1.id === 'string')
        _addJob(v1.id, v1);

      // Hash of jobs:
      else {
        // Keep conrad to start until the last job is added:
        _noStart = true;

        for (i in v1)
          if (typeof v1[i] === 'function')
            _addJob(i, __extend({
              job: v1[i]
            }, v2));
          else
            _addJob(i, __extend(v1[i], v2));

        _noStart = false;
        if (!_isRunning) {
          // Update the _lastFrameTime:
          _lastFrameTime = __dateNow();

          _dispatch('start');
          _loop();
        }
      }

    // One job (string, *):
    } else if (typeof v1 === 'string') {
      if (_hasJob(v1))
        throw new Error(
          '[conrad.addJob] Job with id "' + v1 + '" already exists.'
        );

      // One job (string, function):
      if (typeof v2 === 'function') {
        o = {
          id: v1,
          done: 0,
          time: 0,
          status: 'waiting',
          currentTime: 0,
          averageTime: 0,
          weightTime: 0,
          job: v2
        };

      // One job (string, object):
      } else if (typeof v2 === 'object') {
        o = __extend(
          {
            id: v1,
            done: 0,
            time: 0,
            status: 'waiting',
            currentTime: 0,
            averageTime: 0,
            weightTime: 0
          },
          v2
        );

      // If none of those cases, throw an error:
      } else
        throw new Error('[conrad.addJob] Wrong arguments.');

      // Effectively add the job:
      _jobs[v1] = o;
      _dispatch('jobAdded', __clone(o));

      // Check if the loop has to be started:
      if (!_isRunning && !_noStart) {
        // Update the _lastFrameTime:
        _lastFrameTime = __dateNow();

        _dispatch('start');
        _loop();
      }

    // If none of those cases, throw an error:
    } else
      throw new Error('[conrad.addJob] Wrong arguments.');

    return this;
  }
function _addJob(v1, v2) {
    var i,
        l,
        o;

    // Array of jobs:
    if (Array.isArray(v1)) {
      // Keep conrad to start until the last job is added:
      _noStart = true;

      for (i = 0, l = v1.length; i < l; i++)
        _addJob(v1[i].id, __extend(v1[i], v2));

      _noStart = false;
      if (!_isRunning) {
        // Update the _lastFrameTime:
        _lastFrameTime = __dateNow();

        _dispatch('start');
        _loop();
      }
    } else if (typeof v1 === 'object') {
      // One job (object):
      if (typeof v1.id === 'string')
        _addJob(v1.id, v1);

      // Hash of jobs:
      else {
        // Keep conrad to start until the last job is added:
        _noStart = true;

        for (i in v1)
          if (typeof v1[i] === 'function')
            _addJob(i, __extend({
              job: v1[i]
            }, v2));
          else
            _addJob(i, __extend(v1[i], v2));

        _noStart = false;
        if (!_isRunning) {
          // Update the _lastFrameTime:
          _lastFrameTime = __dateNow();

          _dispatch('start');
          _loop();
        }
      }

    // One job (string, *):
    } else if (typeof v1 === 'string') {
      if (_hasJob(v1))
        throw new Error(
          '[conrad.addJob] Job with id "' + v1 + '" already exists.'
        );

      // One job (string, function):
      if (typeof v2 === 'function') {
        o = {
          id: v1,
          done: 0,
          time: 0,
          status: 'waiting',
          currentTime: 0,
          averageTime: 0,
          weightTime: 0,
          job: v2
        };

      // One job (string, object):
      } else if (typeof v2 === 'object') {
        o = __extend(
          {
            id: v1,
            done: 0,
            time: 0,
            status: 'waiting',
            currentTime: 0,
            averageTime: 0,
            weightTime: 0
          },
          v2
        );

      // If none of those cases, throw an error:
      } else
        throw new Error('[conrad.addJob] Wrong arguments.');

      // Effectively add the job:
      _jobs[v1] = o;
      _dispatch('jobAdded', __clone(o));

      // Check if the loop has to be started:
      if (!_isRunning && !_noStart) {
        // Update the _lastFrameTime:
        _lastFrameTime = __dateNow();

        _dispatch('start');
        _loop();
      }

    // If none of those cases, throw an error:
    } else
      throw new Error('[conrad.addJob] Wrong arguments.');

    return this;
  }
function _addJob ( v1 , v2 ) { var i , l , o ; // Array of jobs: if ( Array . isArray ( v1 ) ) { // Keep conrad to start until the last job is added: _noStart = true ; for ( i = 0 , l = v1 . length ; i < l ; i ++ ) _addJob ( v1 [ i ] . id , __extend ( v1 [ i ] , v2 ) ) ; _noStart = false ; if ( ! _isRunning ) { // Update the _lastFrameTime: _lastFrameTime = __dateNow ( ) ; _dispatch ( 'start' ) ; _loop ( ) ; } } else if ( typeof v1 === 'object' ) { // One job (object): if ( typeof v1 . id === 'string' ) _addJob ( v1 . id , v1 ) ; // Hash of jobs: else { // Keep conrad to start until the last job is added: _noStart = true ; for ( i in v1 ) if ( typeof v1 [ i ] === 'function' ) _addJob ( i , __extend ( { job : v1 [ i ] } , v2 ) ) ; else _addJob ( i , __extend ( v1 [ i ] , v2 ) ) ; _noStart = false ; if ( ! _isRunning ) { // Update the _lastFrameTime: _lastFrameTime = __dateNow ( ) ; _dispatch ( 'start' ) ; _loop ( ) ; } } // One job (string, *): } else if ( typeof v1 === 'string' ) { if ( _hasJob ( v1 ) ) throw new Error ( '[conrad.addJob] Job with id "' + v1 + '" already exists.' ) ; // One job (string, function): if ( typeof v2 === 'function' ) { o = { id : v1 , done : 0 , time : 0 , status : 'waiting' , currentTime : 0 , averageTime : 0 , weightTime : 0 , job : v2 } ; // One job (string, object): } else if ( typeof v2 === 'object' ) { o = __extend ( { id : v1 , done : 0 , time : 0 , status : 'waiting' , currentTime : 0 , averageTime : 0 , weightTime : 0 } , v2 ) ; // If none of those cases, throw an error: } else throw new Error ( '[conrad.addJob] Wrong arguments.' ) ; // Effectively add the job: _jobs [ v1 ] = o ; _dispatch ( 'jobAdded' , __clone ( o ) ) ; // Check if the loop has to be started: if ( ! _isRunning && ! _noStart ) { // Update the _lastFrameTime: _lastFrameTime = __dateNow ( ) ; _dispatch ( 'start' ) ; _loop ( ) ; } // If none of those cases, throw an error: } else throw new Error ( '[conrad.addJob] Wrong arguments.' ) ; return this ; }
Adds one or more jobs, and starts the loop if no job was running before. A
job is at least a unique string "id" and a function, and there are some
parameters that you can specify for each job to modify the way conrad will
execute it. If a job is added with the "id" of another job that is waiting
or still running, an error will be thrown.

When a job is added, it is referenced in the _jobs object, by its id.
Then, if it has to be executed right now, it will be also referenced in
the _runningJobs object. If it has to wait, then it will be added into the
_waitingJobs object, until it can start.

Keep reading this documentation to see how to call this method.

@return {Object} Returns conrad.

Adding one job:
***************
Basically, a job is defined by its string id and a function (the job). It
is also possible to add some parameters:

> conrad.addJob('myJobId', myJobFunction);
> conrad.addJob('myJobId', {
>   job: myJobFunction,
>   someParameter: someValue
> });
> conrad.addJob({
>   id: 'myJobId',
>   job: myJobFunction,
>   someParameter: someValue
> });

Adding several jobs:
********************
When adding several jobs at the same time, it is possible to specify
parameters for each one individually or for all:

> conrad.addJob([
>   {
>     id: 'myJobId1',
>     job: myJobFunction1,
>     someParameter1: someValue1
>   },
>   {
>     id: 'myJobId2',
>     job: myJobFunction2,
>     someParameter2: someValue2
>   }
> ], {
>   someCommonParameter: someCommonValue
> });
> conrad.addJob({
>   myJobId1: {,
>     job: myJobFunction1,
>     someParameter1: someValue1
>   },
>   myJobId2: {,
>     job: myJobFunction2,
>     someParameter2: someValue2
>   }
> }, {
>   someCommonParameter: someCommonValue
> });
> conrad.addJob({
>   myJobId1: myJobFunction1,
>   myJobId2: myJobFunction2
> }, {
>   someCommonParameter: someCommonValue
> });

Recognized parameters:
**********************
Here is the exhaustive list of every accepted parameters:

{?Function} end      A callback to execute when the job is ended. It is
not executed if the job is killed instead of ended
"naturally".
{?Integer}  count    The number of time the job has to be executed.
{?Number}   weight   If specified, the job will be executed as it was
added "weight" times.
{?String}   after    The id of another job (eventually not added yet).
If specified, this job will start only when the
specified "after" job is ended.
Adds one or more jobs and starts the loop if no job was running before . A job is at least a unique string id and a function and there are some parameters that you can specify for each job to modify the way conrad will execute it . If a job is added with the id of another job that is waiting or still running an error will be thrown .