<commit_before>/**
* @namespace
*/
var ORBIS = {

  /**
  * @author <NAME> <http://www.lcluber.com/contact>
  * @file Asynchronous assets loader library.
  * @version 0.2.0
  * @copyright (c) 2011 <NAME>

  * @license
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  *
  */

  revision: '0.3.0',

  assets: {}, //data from the assets file
  assetsPath      : '',
  requests        : {}, //requests list
  requestsLength  : 0,
  sentRequests    : 0,
  onProgress      : function(){},
  onComplete      : function(){},
  progress        : {},
  pending         : 0,

  tick             : 0,
  maxPending       : 0,
  defaultMaxPending: 6,
  defaultTick      : 100,

  logs             : {},

  /**
  * Create a new loader.
  * @since 0.2.0
  * @method
  * @param {function} onProgress The name of your onProgress callback function.
  * @param {function} onComplete The name of your onComplete callback function.
  * @param {integer} [tick = 100] The timeout between two requests.
  * @param {integer} [maxPending = 6] The maximum pending requests. Once reached new requests will not start before a previous one completes. Six being the maximum on most browsers.
  * @returns {Loader}  The new Loader
  */
  create : function(onProgress, onAnimate, onComplete, tick, maxPending) {
    var _this = Object.create(this);
    _this.logs     = ORBIS.Logger.create();
    _this.logs.init();

    //_this.checkCallback(onProgress);
    //_this.checkCallback(onComplete);
    if ( ORBIS.Utils.isFunction(onProgress) )
      _this.onProgress = onProgress;
    else
      _this.logs.add('onProgress Parameter is not a function');

    if ( ORBIS.Utils.isFunction(onComplete) )
      _this.onComplete = onComplete;
    else
      _this.logs.add('onComplete Parameter is not a function');

    _this.progress = ORBIS.Progress.create(onAnimate);

    _this.setTick(tick);
    _this.setMaxPending(maxPending);
    return _this;
  },

  // checkCallback : function(callback){
  //   console.log(callback.name);
  //   if ( ORBIS.Utils.isFunction(callback) )
  //     this[callback.name] = callback;
  //   else
  //     this.logs.add(callback.name + ' parameter is not a function');
  // },

  /**
  * Start the loader.
  * @since 0.2.0
  * @method
  * @param {string} assetsFilePath The path of your assets config file.
  * @param {string} assetsPath The path of the folder containing all the assets.
  */
  launch : function( assetsFilePath, assetsPath ){
    this.progress.init();

    this.assetsPath = ORBIS.Utils.removeTrailingSlash(assetsPath);
    //check if assets file exists
    // if(){
    //
    // }
    //check if assetsFile already loaded
    if( this.assets.fsm && this.assets.fsm.getStatus() === 'success' ){
      this.logs.add( assetsFilePath + ' already loaded.');
      this.onAssetsFileLoaded('success', this.assets.response, this);
    }else{
      this.logs.add('create request for ' + assetsFilePath);
      this.assets = ORBIS.Request.create( assetsFilePath, 'file', '', this.onAssetsFileLoaded, this );
      if(this.assets)
        this.assets.send();
      else
        this.logs.add('!! the config file must be of type "file"');
    }
  },

  onAssetsFileLoaded : function(status, response, _this){
    if (status == 'success'){
      _this.logs.add('parsing asset file');
      _this.requestsLength = 0;
      _this.sentRequests = 0;
      for (var property in response.json){
        if ( response.json.hasOwnProperty(property) ){
          var type   = response.json[property];
          var files  = type.files;
          var folder = type.folder ? type.folder + '/' : '';
          for (var i = 0 ; i < files.length ; i++){
            var asset = files[i];
            var file = asset.hasOwnProperty('file') ? asset.file : asset;
            var path = _this.assetsPath + '/' + folder + file;
            //errors
            if(_this.getAsset(file)){
              _this.logs.add( path + ' already loaded.');
              continue;
            }
            var request = ORBIS.Request.create( path, '', asset, _this.onFileLoaded, _this );
            if (typeof request.file === 'string'){
              _this.logs.add(request.file);
              continue;
            }
            _this.logs.add('create request for ' + path);
            _this.requests[file] = request;
            _this.requestsLength++;
          }
        }
      }
      _this.logs.add(_this.requestsLength + ' requests to perform');
      _this.sendRequest();
    }else
      _this.logs.add('!! error during config file AJAX request');
  },

  sendRequest : function(){
    if( this.pending < this.maxPending){
      var nextProperty = Object.keys(this.requests)[this.sentRequests];
      if (this.getAsset(nextProperty) || this.requests[nextProperty].send() == 'error')
        this.updateProgress( 0, this.getAsset(nextProperty).response );
      this.pending++;
      this.sentRequests++;

    }

    if (this.sentRequests < this.requestsLength){
      var _this = this;
      setTimeout( function(){
        _this.loadAnotherFile(_this);
      }, this.tick );
    }
  },

  loadAnotherFile : function(_this){ //start loading
    _this.sendRequest();
  },

  //callback
  onFileLoaded : function(success, response, _this){ //called when a file is loaded
    _this.updateProgress(success, response);
  },

  updateProgress : function(success, response){ //called when a file is cached
    var progress = this.progress.update(success, this.requestsLength);<commit_msg>done and seems to work fine. Needs further testings<commit_after>/**
* @namespace
*/
var ORBIS = {

  /**
  * @author <NAME> <http://www.lcluber.com/contact>
  * @file Asynchronous assets loader library.
  * @version 0.2.0
  * @copyright (c) 2011 <NAME>

  * @license
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in all
  * copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  *
  */

  revision: '0.3.0',

  assets: {}, //data from the assets file
  assetsPath      : '',
  requests        : {}, //requests list
  requestsLength  : 0,
  sentRequests    : 0,
  onProgress      : function(){},
  onComplete      : function(){},
  progress        : {},
  pending         : 0,

  tick             : 0,
  maxPending       : 0,
  defaultMaxPending: 6,
  defaultTick      : 100,

  logs             : {},

  /**
  * Create a new loader.
  * @since 0.2.0
  * @method
  * @param {function} onProgress The name of your onProgress callback function.
  * @param {function} onComplete The name of your onComplete callback function.
  * @param {integer} [tick = 100] The timeout between two requests.
  * @param {integer} [maxPending = 6] The maximum pending requests. Once reached new requests will not start before a previous one completes. Six being the maximum on most browsers.
  * @returns {Loader}  The new Loader
  */
  create : function(onProgress, onAnimate, onComplete, tick, maxPending) {
    var _this = Object.create(this);
    _this.logs     = ORBIS.Logger.create();
    _this.logs.init();

    //_this.checkCallback(onProgress);
    //_this.checkCallback(onComplete);
    if ( ORBIS.Utils.isFunction(onProgress) )
      _this.onProgress = onProgress;
    else
      _this.logs.add('onProgress Parameter is not a function');

    if ( ORBIS.Utils.isFunction(onComplete) )
      _this.onComplete = onComplete;
    else
      _this.logs.add('onComplete Parameter is not a function');

    _this.progress = ORBIS.Progress.create(onAnimate);

    _this.setTick(tick);
    _this.setMaxPending(maxPending);
    return _this;
  },

  // checkCallback : function(callback){
  //   console.log(callback.name);
  //   if ( ORBIS.Utils.isFunction(callback) )
  //     this[callback.name] = callback;
  //   else
  //     this.logs.add(callback.name + ' parameter is not a function');
  // },

  /**
  * Start the loader.
  * @since 0.2.0
  * @method
  * @param {string} assetsFilePath The path of your assets config file.
  * @param {string} assetsPath The path of the folder containing all the assets.
  */
  launch : function( assetsFilePath, assetsPath ){
    this.progress.init();

    this.assetsPath = ORBIS.Utils.removeTrailingSlash(assetsPath);
    //check if assets file exists
    // if(){
    //
    // }
    //check if assetsFile already loaded
    if( this.assets.fsm && this.assets.fsm.getStatus() === 'success' ){
      this.logs.add( assetsFilePath + ' already loaded.');
      this.onAssetsFileLoaded('success', this.assets.response, this);
    }else{
      this.logs.add('create request for ' + assetsFilePath);
      this.assets = ORBIS.Request.create( assetsFilePath, 'file', '', this.onAssetsFileLoaded, this );
      if(this.assets)
        this.assets.send();
      else
        this.logs.add('!! the config file must be of type "file"');
    }
  },

  onAssetsFileLoaded : function(status, response, _this){
    if (status == 'success'){
      _this.logs.add('parsing asset file');
      _this.requestsLength = 0;
      _this.sentRequests = 0;
      for (var property in response.json){
        if ( response.json.hasOwnProperty(property) ){
          var type   = response.json[property];
          var files  = type.files;
          var folder = type.folder ? type.folder + '/' : '';
          for (var i = 0 ; i < files.length ; i++){
            var asset = files[i];
            var file = asset.hasOwnProperty('file') ? asset.file : asset;
            var path = _this.assetsPath + '/' + folder + file;
            //errors
            if(_this.getAsset(file)){
              _this.logs.add( path + ' already loaded.');
              continue;
            }
            var request = ORBIS.Request.create( path, '', asset, _this.onFileLoaded, _this );
            if (typeof request.file === 'string'){
              _this.logs.add(request.file);
              continue;
            }
            _this.logs.add('create request for ' + path);
            _this.requests[file] = request;
            _this.requestsLength++;
          }
        }
      }
      _this.logs.add(_this.requestsLength + ' requests to perform');
      _this.sendRequest();
    }else
      _this.logs.add('!! error during config file AJAX request');
  },

  sendRequest : function(){
    if( this.pending < this.maxPending){
      var nextProperty = Object.keys(this.requests)[this.sentRequests];
      if (this.getAsset(nextProperty) || this.requests[nextProperty].send() == 'error')
        this.updateProgress( 0, this.getAsset(nextProperty).response );
      this.pending++;
      this.sentRequests++;

    }

    if (this.sentRequests < this.requestsLength){
      // var _this = this;
      // setTimeout( function(){
      //   _this.loadAnotherFile(_this);
      // }, this.tick );
      setTimeout( this.loadAnotherFile.bind(this), this.tick );
    }
  },

  loadAnotherFile : function(){ //start loading
    this.sendRequest();
  },

  //callback
  onFileLoaded : function(success, response, _this){ //called when a file is loaded
    _this.updateProgress(success, response);
  },

  updateProgress : function(success, response){ //called when a file is cached
    var progress = this.progress.update(success, this.requestsLength);
{'fraction_non_alphanumeric': 0.11896420411271896, 'fraction_numerical': 0.005026656511805026, 'mean_word_length': 2.6424410540915395, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '172338', 'n_tokens_mistral': 3859, 'n_tokens_neox': 3762, 'n_words': 1142}