forked from 0x2620/pandora
implement pause/resume for chunk upload
This commit is contained in:
parent
4bff07f054
commit
7738d13679
1 changed files with 37 additions and 3 deletions
|
@ -30,10 +30,14 @@ pandora.chunkupload = function(options) {
|
||||||
chunkURL,
|
chunkURL,
|
||||||
file = options.file,
|
file = options.file,
|
||||||
maxRetry = -1,
|
maxRetry = -1,
|
||||||
|
nextChunkId,
|
||||||
|
paused = false,
|
||||||
retries = 0,
|
retries = 0,
|
||||||
request,
|
request,
|
||||||
that = Ox.Element();
|
that = Ox.Element();
|
||||||
|
|
||||||
|
options.data = options.data || {};
|
||||||
|
|
||||||
initUpload();
|
initUpload();
|
||||||
|
|
||||||
function done() {
|
function done() {
|
||||||
|
@ -143,7 +147,12 @@ pandora.chunkupload = function(options) {
|
||||||
// reset retry counter
|
// reset retry counter
|
||||||
retries = 0;
|
retries = 0;
|
||||||
// start uploading next chunk
|
// start uploading next chunk
|
||||||
|
if (paused) {
|
||||||
|
nextChunkId = chunkId + 1;
|
||||||
|
that.triggerEvent('paused', {next: nextChunkId});
|
||||||
|
} else {
|
||||||
uploadChunk(chunkId + 1);
|
uploadChunk(chunkId + 1);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// failed to upload, try again in 5 second
|
// failed to upload, try again in 5 second
|
||||||
retries++;
|
retries++;
|
||||||
|
@ -153,7 +162,12 @@ pandora.chunkupload = function(options) {
|
||||||
done();
|
done();
|
||||||
} else {
|
} else {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
if (paused) {
|
||||||
|
nextChunkId = chunkId;
|
||||||
|
that.triggerEvent('paused', {next: nextChunkId});
|
||||||
|
} else {
|
||||||
uploadChunk(chunkId);
|
uploadChunk(chunkId);
|
||||||
|
}
|
||||||
}, 5000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,7 +181,12 @@ pandora.chunkupload = function(options) {
|
||||||
done();
|
done();
|
||||||
} else {
|
} else {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
if (paused) {
|
||||||
|
nextChunkId = chunkId;
|
||||||
|
that.triggerEvent('paused', {next: nextChunkId});
|
||||||
|
} else {
|
||||||
uploadChunk(chunkId);
|
uploadChunk(chunkId);
|
||||||
|
}
|
||||||
}, 3000);
|
}, 3000);
|
||||||
}
|
}
|
||||||
}, false);
|
}, false);
|
||||||
|
@ -200,6 +219,21 @@ pandora.chunkupload = function(options) {
|
||||||
request.abort();
|
request.abort();
|
||||||
request = null;
|
request = null;
|
||||||
}
|
}
|
||||||
|
return that;
|
||||||
|
};
|
||||||
|
that.pause = function() {
|
||||||
|
paused = true;
|
||||||
|
return that;
|
||||||
|
};
|
||||||
|
that.resume = function() {
|
||||||
|
if (paused) {
|
||||||
|
paused = false;
|
||||||
|
if (nextChunkId) {
|
||||||
|
uploadChunk(nextChunkId);
|
||||||
|
nextChunkId = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return that;
|
||||||
};
|
};
|
||||||
|
|
||||||
return that;
|
return that;
|
||||||
|
|
Loading…
Reference in a new issue