-
Notifications
You must be signed in to change notification settings - Fork 1
/
celery.js
64 lines (57 loc) · 1.67 KB
/
celery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
;(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define('celeryjs', [], function() {
return factory();
});
} else {
// Browser globals
root.CeleryJS = factory();
}
}(this, function() {
var Celery = function(options) {
options = $.extend({}, options, Celery.defaultOptions);
var deferred = $.Deferred();
var intervalId = null;
var ajaxOptions = $.extend({}, options.ajax);
ajaxOptions.url = options.url;
function handleStatus(data) {
if (data === null) {
return;
}
var task = data.task;
if (task === null) {
return;
}
var handler = deferred.notify;
if (task.status === 'FAILURE') {
clearInterval(intervalId);
handler = deferred.reject;
}
if (task.status === 'SUCCESS') {
clearInterval(intervalId);
handler = deferred.resolve;
}
handler(task);
}
function checkStatus() {
var xhr = $.ajax(ajaxOptions);
xhr.fail(function(data) {
clearInterval(intervalId);
deferred.reject(data);
});
xhr.done(handleStatus);
}
setTimeout(checkStatus, 0);
intervalId = setInterval(checkStatus, options.checkInterval);
return deferred;
};
Celery.defaultOptions = {
checkInterval: 3000,
ajax: {
cache: false,
dataType: 'json'
}
};
return Celery;
}));