• Jump To … +
    annotation.js async.js bus.js context.js core.js di.js html.js loader.js object.js url.js
  • async.js

  • ¶
    /*global Arenite:true*/
  • ¶

    Asynchronous tools

    Arenite.Async = function (arenite) {
      var _sequencialLatch = function (values, callback, finalCallback) {
        var index = 0;
        var length = values.length;
    
        var _next = function () {
          if (index < length) {
            callback(values[index], index++);
          } else {
            finalCallback();
          }
        };
    
        return {
          next: _next
        };
      };
    
      var _latch = function (times, callback, name) {
        var executions = 0;
        var id = name || new Date().getTime();
    
        if (arenite.config.debug) {
          window.console.groupCollapsed('Latch: Starting latch "' + id + '" for', times, 'times');
          window.console.trace();
          window.console.groupEnd();
        }
    
        return {
          countDown: function () {
            executions++;
            if (arenite.config.debug) {
              window.console.log('Latch: Counting down latch "' + id + '" ,', times - executions, 'remaining');
            }
            if (executions === times) {
              if (arenite.config.debug) {
                window.console.log('Latch: Triggering latch "' + id + '"');
              }
              callback(executions);
            }
          },
          countUp: function () {
            executions--;
            if (arenite.config.debug) {
              window.console.log('Latch: Counting up latch "' + id + '" ,', times - executions, 'remaining');
            }
            if (executions === times) {
              if (arenite.config.debug) {
                window.console.log('Latch: Triggering latch "' + id + '"');
              }
              callback(executions);
            }
          }
        };
      };
    
      return {
        async: {
  • ¶

    Sequencial latch.

    The sequencial latch will synchronously execute the handler with the provided values and execute a callback when all operations are complete.

    
    seqLatch(values, handler, callback)
    
    Where values is an array of values to be passed as parameters to the handler function. The handler function must call the next function of the returned object when it finishes the execution. The callback is the function that is executed once all the values have been handled.

          seqLatch: _sequencialLatch,
  • ¶

    Latch.

    The latch will execute asynchronous tasks and invoke a callback when all the declared times have been executed

    
    latch(times, callback [, name])
    
    Where times is the initially expected times the latch should wait for to trigger the callback. The callback is the function invoked once times reaches 0. The optional name is if you wish to have a meaningful name in the console logs. This function returns an object with two functions:

    
    {
      countDown:…
      countUp:…
    }
    
    countDown will decrease the counter and CountUp will increase the counter that is initialized with the times argument. Once the counter hits 0 the callback is invoked.

          latch: _latch
        }
      };
    };