Skip to content

Commit e73fc7c

Browse files
committed
Fixed running concurrent jobs after add/cancel changes
1 parent 722de36 commit e73fc7c

File tree

6 files changed

+68
-31
lines changed

6 files changed

+68
-31
lines changed

dist/load-queue.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/load-queue.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.html

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,66 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Load queue example</title>
6-
<script src="../dist/load-queue.min.js" type="text/javascript"></script>
6+
<script src="../dist/load-queue.js" type="text/javascript"></script>
77
</head>
8+
<style>
9+
.log {
10+
width: 50%;
11+
padding: 20px;
12+
box-sizing: border-box;
13+
float: left;
14+
height: 400px;
15+
overflow: scroll;
16+
}
17+
</style>
818
<body>
919

10-
<h1>Load queue example</h1>
11-
<div id="log"></div>
20+
<div class="log">
21+
<h1>Load queue example</h1>
22+
<div id="log"></div>
23+
</div>
24+
<div class="log">
25+
<h1>Load queue example - concurrent</h1>
26+
<div id="log2"></div>
27+
</div>
28+
<div style="clear: both;"></div>
1229
<script>
13-
var log = document.getElementById('log')
14-
15-
function write (text) {
16-
log.innerHTML += text + '<br/>'
17-
}
18-
// The custom loader task that will accept entry object (QueryEntry)
19-
var loaderTask = function (entry, success, failure) {
20-
write('Loading ' + entry.url)
21-
setTimeout(function () {
22-
if (entry.url === 'url1') {
23-
failure(new Error('Failed!'))
24-
} else {
25-
success('my custom var', 'custom var 2')
26-
}
27-
}, 1000)
30+
function getRandomArbitrary(min, max) {
31+
return Math.random() * (max - min) + min;
2832
}
2933

30-
// Build the queue
31-
var queue = new LoadQueue.default(loaderTask)
32-
for (var i = 0; i < 20; i++) {
33-
queue.add('url' + i, function (url, customVar, customVar1) {
34-
write('Loaded ' + url + ', vars: ' + customVar + ', ' + customVar1)
35-
}, function (url, error) {
36-
write('Error ' + url + ' - ' + error.message)
37-
})
34+
function start (logDiv, jobs) {
35+
var log = document.getElementById(logDiv)
36+
37+
function write (text) {
38+
log.innerHTML = text + '<br/>' + log.innerHTML
39+
}
40+
41+
// The custom loader task that will accept entry object (QueryEntry)
42+
var loaderTask = function (entry, success, failure) {
43+
write('Loading ' + entry.url)
44+
setTimeout(function () {
45+
if (entry.url === 'url1') {
46+
failure(new Error('Failed!'))
47+
} else {
48+
success('my custom var', 'custom var 2')
49+
}
50+
}, getRandomArbitrary(1, 3) * 1000)
51+
}
52+
53+
// Build the queue
54+
var queue = new LoadQueue.default(loaderTask, jobs)
55+
for (var i = 0; i < 50; i++) {
56+
queue.add('url' + i, function (url, customVar, customVar1) {
57+
write('Loaded ' + url + ', vars: ' + customVar + ', ' + customVar1)
58+
}, function (url, error) {
59+
write('Error ' + url + ' - ' + error.message)
60+
})
61+
}
3862
}
63+
64+
start('log', 1)
65+
start('log2', 3)
3966
</script>
4067
</body>
4168
</html>

lib/QueueCore.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ var QueueCore = function () {
158158

159159
// Start the request
160160
this.startTimeout = setTimeout(function () {
161-
_this.forceStart();
161+
var queueEmpty = false;
162+
// Start enough items for concurrent jobs
163+
while (queueEmpty === false && _this.loading.length() < _this.concurrentJobs) {
164+
console.log('looping');
165+
queueEmpty = _this.forceStart() === false;
166+
}
162167
}, this.startTimeoutTime); // 50ms is enough to handle scroll start -> cancel transitions
163168
}
164169

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "load-queue",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Designed to allow running task in a queue with parallel support. Can be used for image or file loading.",
55
"main": "lib/index.js",
66
"repository": "https://github.com/pionl/load-queue",

src/QueueCore.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ export default class QueueCore {
129129

130130
// Start the request
131131
this.startTimeout = setTimeout(() => {
132-
this.forceStart()
132+
let queueEmpty = false
133+
// Start enough items for concurrent jobs
134+
while (queueEmpty === false && this.loading.length() < this.concurrentJobs) {
135+
// If force start returns false, the queue is empty - stop
136+
queueEmpty = this.forceStart() === false
137+
}
133138
}, this.startTimeoutTime) // 50ms is enough to handle scroll start -> cancel transitions
134139
}
135140

0 commit comments

Comments
 (0)