Skip to content

Commit a7eaade

Browse files
author
ehennum
committed
sort documents within batch to prevent deadlock #535
1 parent c15a1f0 commit a7eaade

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/documents.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ function getDocumentUris(documents) {
4141
return documents.reduce(addDocumentUri, []);
4242
}
4343

44+
/** @ignore */
45+
function compareDocuments(firstDoc, secondDoc) {
46+
const hasFirstDoc = (firstDoc !== null);
47+
const hasSecondDoc = (secondDoc !== null);
48+
if (!hasFirstDoc && !hasSecondDoc) return 0;
49+
if (!hasFirstDoc && hasSecondDoc) return -1;
50+
if (hasFirstDoc && !hasSecondDoc) return 1;
51+
const firstUri = firstDoc.uri;
52+
const secondUri = secondDoc.uri;
53+
const hasFirstUri = ((typeof firstUri === 'string' || firstUri instanceof String) && firstUri.length > 0);
54+
const hasSecondUri = ((typeof secondUri === 'string' || secondUri instanceof String) && secondUri.length > 0);
55+
if (!hasFirstUri && !hasSecondUri) return 0;
56+
if (!hasFirstUri && hasSecondUri) return -1;
57+
if (hasFirstUri && !hasSecondUri) return 1;
58+
if (firstUri < secondUri) return -1;
59+
if (firstUri > secondUri) return 1;
60+
return 0;
61+
}
62+
4463
/** @ignore */
4564
function uriErrorTransform(message) {
4665
/*jshint validthis:true */
@@ -1206,6 +1225,10 @@ function writeContent(contentOnly, document, requestParams, categories, requestT
12061225
/** @ignore */
12071226
function writeDocumentList(contentOnly, documents, requestParams, categories) {
12081227
/*jshint validthis:true */
1228+
if (documents.length > 1) {
1229+
documents = documents.sort(compareDocuments);
1230+
}
1231+
12091232
var requestPartList = [];
12101233
for (var i=0; i < documents.length; i++) {
12111234
addDocumentParts(requestPartList, documents[i], false);

0 commit comments

Comments
 (0)