Skip to content

Instantly share code, notes, and snippets.

@ssured
Created July 20, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssured/2b9e26ef797ee7db17ac13bcd2d2a78f to your computer and use it in GitHub Desktop.
Save ssured/2b9e26ef797ee7db17ac13bcd2d2a78f to your computer and use it in GitHub Desktop.
var getAttachmentQueue = {};
var getAttachmentActive = {};
var getAttachmentCount = 0;
var getAttachmentConcurrency = 10;
function processQueue() {
console.log('processQueue', getAttachmentCount, Object.keys(getAttachmentActive).length, Object.keys(getAttachmentQueue).length);
while (Object.keys(getAttachmentActive).length < getAttachmentConcurrency && Object.keys(getAttachmentQueue).length > 0) {
var key = Object.keys(getAttachmentQueue)[0];
var args = getAttachmentActive[key] = getAttachmentQueue[key];
delete getAttachmentQueue[key];
var opts = args[0], url = args[1], callback = args[2];
ajax$$(opts, {
method: 'GET',
url: url,
binary: true
}, function() {
delete getAttachmentActive[key];
processQueue();
callback.apply(this, arguments);
});
}
}
// Get the attachment
api.getAttachment =
adapterFun$$('getAttachment', function (docId, attachmentId, opts,
callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
var params = opts.rev ? ('?rev=' + opts.rev) : '';
var url = genDBUrl(host, encodeDocId(docId)) + '/' +
encodeAttachmentId(attachmentId) + params;
getAttachmentQueue["key"+(getAttachmentCount++)] = [opts, url, callback];
processQueue();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment