-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from OmarWKH/message-websocket-fix
Make CeleryWebSocketProgressBar a subclass of CeleryProgressBar
- Loading branch information
Showing
3 changed files
with
96 additions
and
110 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,32 @@ | ||
var CeleryWebSocketProgressBar = (function () { | ||
function onSuccessDefault(progressBarElement, progressBarMessageElement, result) { | ||
CeleryProgressBar.onSuccessDefault(progressBarElement, progressBarMessageElement); | ||
} | ||
|
||
function onResultDefault(resultElement, result) { | ||
CeleryProgressBar.onResultDefault(resultElement, result); | ||
} | ||
|
||
function onErrorDefault(progressBarElement, progressBarMessageElement, excMessage, data) { | ||
CeleryProgressBar.onErrorDefault(progressBarElement, progressBarMessageElement, excMessage, data); | ||
} | ||
class CeleryWebSocketProgressBar extends CeleryProgressBar { | ||
|
||
function onProgressDefault(progressBarElement, progressBarMessageElement, progress) { | ||
CeleryProgressBar.onProgressDefault(progressBarElement, progressBarMessageElement, progress); | ||
constructor(progressUrl, options) { | ||
super(progressUrl, options); | ||
} | ||
|
||
function initProgress (progressUrl, options) { | ||
options = options || {}; | ||
var progressBarId = options.progressBarId || 'progress-bar'; | ||
var progressBarMessage = options.progressBarMessageId || 'progress-bar-message'; | ||
var progressBarElement = options.progressBarElement || document.getElementById(progressBarId); | ||
var progressBarMessageElement = options.progressBarMessageElement || document.getElementById(progressBarMessage); | ||
var onProgress = options.onProgress || onProgressDefault; | ||
var onSuccess = options.onSuccess || onSuccessDefault; | ||
var onError = options.onError || onErrorDefault; | ||
var onTaskError = options.onTaskError || onError; | ||
var resultElementId = options.resultElementId || 'celery-result'; | ||
var resultElement = options.resultElement || document.getElementById(resultElementId); | ||
var onResult = options.onResult || onResultDefault; | ||
|
||
async connect() { | ||
var ProgressSocket = new WebSocket((location.protocol === 'https:' ? 'wss' : 'ws') + '://' + | ||
window.location.host + progressUrl); | ||
window.location.host + this.progressUrl); | ||
|
||
ProgressSocket.onopen = function (event) { | ||
ProgressSocket.send(JSON.stringify({'type': 'check_task_completion'})); | ||
}; | ||
|
||
const bar = this; | ||
ProgressSocket.onmessage = function (event) { | ||
var data = JSON.parse(event.data); | ||
|
||
if (data.progress) { | ||
onProgress(progressBarElement, progressBarMessageElement, data.progress); | ||
let data; | ||
try { | ||
data = JSON.parse(event.data); | ||
} catch (parsingError) { | ||
bar.onDataError(bar.progressBarElement, bar.progressBarMessageElement, "Parsing Error") | ||
throw parsingError; | ||
} | ||
if (data.complete) { | ||
if (data.success) { | ||
onSuccess(progressBarElement, progressBarMessageElement, data.result); | ||
} else { | ||
onTaskError(progressBarElement, progressBarMessageElement, data.result); | ||
} | ||
if (data.hasOwnProperty('result')) { | ||
onResult(resultElement, data.result); | ||
} | ||
|
||
const complete = bar.onData(data); | ||
|
||
if (complete === true || complete === undefined) { | ||
ProgressSocket.close(); | ||
} | ||
} | ||
}; | ||
} | ||
return { | ||
onSuccessDefault: onSuccessDefault, | ||
onResultDefault: onResultDefault, | ||
onErrorDefault: onErrorDefault, | ||
onProgressDefault: onProgressDefault, | ||
initProgressBar: initProgress, | ||
}; | ||
})(); | ||
} |