Created
July 20, 2016 10:39
-
-
Save ssured/2b9e26ef797ee7db17ac13bcd2d2a78f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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