-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (48 loc) · 1.09 KB
/
index.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
function constructorOf(Child, Super) {
if (Child === Super) return true;
if (!Child) {
return false;
}
if (Child.super_ === undefined) {
return false;
}
if (Child.super_ === Super) {
return true;
} else {
return constructorOf(Child.super_, Super);
}
}
function then(jobs, KueJob, jobType, Job) {
jobs.on('job complete', function (id, result) {
KueJob.get(id, function (err, job) {
if (job.type === jobType) {
if (constructorOf(Job, KueJob)) {
new Job(result, job.data, job).save();
} else {
Job(result, job.data, job, function (job) {
job.save();
});
}
}
});
});
}
module.exports = function chainer(jobs, KueJob) {
return function (ParentJob) {
var jobType;
if ('string' === typeof ParentJob) {
jobType = ParentJob;
} else {
if (!constructorOf(ParentJob, KueJob)) {
throw new Error(ParentJob + ' does not inherit from the kue.Job.');
}
jobType = (new ParentJob).type;
}
if (!jobType) {
throw new Error('Not an actionable parent job('+ParentJob+').');
}
return {
then: then.bind(null, jobs, KueJob, jobType)
};
};
};