Skip to content

Commit

Permalink
Patch for GitHub issue 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
billmalarky committed Jan 31, 2018
1 parent 32cc9b5 commit 21059a3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Models/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,27 @@ export default class Worker {
throw new Error('Job ' + job.name + ' does not have a worker assigned to it.');
}

// Timeout Logic
if (job.timeout > 0) {
// Data must be cloned off the realm job object for the timeout logic promise race.
// More info: https://github.com/billmalarky/react-native-queue/issues/2#issuecomment-361418965
const jobId = job.id;
const jobName = job.name;
const jobTimeout = job.timeout;
const jobPayload = JSON.parse(job.payload);

if (jobTimeout > 0) {

let timeoutPromise = new Promise((resolve, reject) => {

setTimeout(() => {
reject(new Error('TIMEOUT: Job id: ' + job.id + ' timed out in ' + job.timeout + 'ms.'));
}, job.timeout);
reject(new Error('TIMEOUT: Job id: ' + jobId + ' timed out in ' + jobTimeout + 'ms.'));
}, jobTimeout);

});

await Promise.race([timeoutPromise, Worker.workers[job.name](job.id, JSON.parse(job.payload))]);
await Promise.race([timeoutPromise, Worker.workers[jobName](jobId, jobPayload)]);

} else {
await Worker.workers[job.name](job.id, JSON.parse(job.payload));
await Worker.workers[jobName](jobId, jobPayload);
}

}
Expand Down

0 comments on commit 21059a3

Please sign in to comment.