/*globals define*/
/*jshint browser: true, node:true*/
/**
 * Provides functionality (used by the project-cache) for loading objects.
 *
 * To avoid multiple round-trips to the server the loadObject requests are put in a bucket
 * that is loaded when the bucket is full (gmeConfig.storage.loadBucketSize) or when a
 * timeout is triggered (gmeConfig.storage.loadBucketTimer).
 *
 * @author pmeijer / https://github.com/pmeijer
 */

define(['common/storage/storageclasses/simpleapi'], function (SimpleAPI) {
    'use strict';

    function StorageObjectLoaders(webSocket, logger, gmeConfig) {
        // watcher counters determining when to join/leave a room on the sever
        this.logger = this.logger || logger.fork('storage');
        SimpleAPI.call(this, webSocket, logger, gmeConfig);
        this.webSocket = webSocket;
        this.gmeConfig = gmeConfig;
        // Bucket for loading objects
        this.loadBucket = [];
        this.loadBucketSize = 0;
        this.loadBucketTimer = null;
        this.logger.debug('StorageObjectLoaders ctor');
    }

    StorageObjectLoaders.prototype = Object.create(SimpleAPI.prototype);
    StorageObjectLoaders.prototype.constructor = StorageObjectLoaders;

    // Getters
    StorageObjectLoaders.prototype.loadObject = function (projectName, hash, callback) {
        var self = this;
        this.logger.debug('loadObject', projectName, hash);

        self.loadBucket.push({projectName: projectName, hash: hash, cb: callback});
        self.loadBucketSize += 1;

        function resetBucketAndLoadObjects() {
            var myBucket = self.loadBucket;
            self.loadBucket = [];
            self.loadBucketTimer = null;
            self.loadBucketSize = 0;
            self.loadObjects(projectName, myBucket);
        }

        if (self.loadBucketSize === 1) {
            self.logger.debug('loadBucket was empty starting timer [ms]', self.gmeConfig.storage.loadBucketTimer);
            self.loadBucketTimer = setTimeout(function () {
                self.logger.debug('loadBucketTimer triggered, loadBucket size', self.loadBucketSize);
                resetBucketAndLoadObjects();
            }, self.gmeConfig.storage.loadBucketTimer);
        } else if (self.loadBucketSize === self.gmeConfig.storage.loadBucketSize) {
            self.logger.debug('loadBuckSize reached sending out loadObjects', self.loadBucketSize);
            clearTimeout(self.loadBucketTimer);
            resetBucketAndLoadObjects();
        }
    };

    StorageObjectLoaders.prototype.loadObjects = function (projectName, hashedObjects) {
        var self = this,
            hashes = {},
            data,
            i;
        for (i = 0; i < hashedObjects.length; i++) {
            hashes[hashedObjects[i].hash] = true;
        }
        hashes = Object.keys(hashes);
        data = {
            hashes: hashes,
            projectName: projectName
        };

        this.webSocket.loadObjects(data, function (err, result) {
            if (err) {
                throw new Error(err);
            }
            self.logger.debug('loadObjects returned', result);
            for (i = 0; i < hashedObjects.length; i++) {
                if (typeof result[hashedObjects[i].hash] === 'string') {
                    self.logger.error(result[hashedObjects[i].hash]);
                    hashedObjects[i].cb(result[hashedObjects[i].hash]);
                } else {
                    hashedObjects[i].cb(err, result[hashedObjects[i].hash]);
                }
            }
        });
    };

    return StorageObjectLoaders;
});
@@ -2061,23 +2061,19 @@
 ed, 
-loadB
+b
 ucket
- s
+S
 ize
+:
 ', s
@@ -2203,21 +2203,33 @@
        %7D
- else
+%0A        %0A       
  if (sel
@@ -2346,19 +2346,12 @@
 hed 
-sending out
+will
  loa
@@ -2358,16 +2358,29 @@
 dObjects
+, bucketSize:
 ', self.
@@ -3586,8 +3586,9 @@
 ers;%0A%7D);
+%0A

Fix for loadBucketSize, can now be 1. (cherry picked from commit 48b099b7e2eda2b774c36bdb417e22f685ab36f7 [formerly 45b770a2e21cc53355fc804eff5768f35ba22e2c])