Skip to content

Commit d30a910

Browse files
committed
style: remove newlines
1 parent aac8ddc commit d30a910

File tree

4 files changed

+6
-71
lines changed

4 files changed

+6
-71
lines changed

Models/Queue.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
* Queue Job Realm Schema defined in ../config/Database
66
*
77
*/
8-
98
import Database from '../config/Database';
109
import uuid from 'react-native-uuid';
1110
import Worker from './Worker';
1211
import promiseReflect from 'promise-reflect';
1312

14-
1513
export class Queue {
16-
1714
/**
1815
*
1916
* Set initial class properties.
@@ -82,7 +79,6 @@ export class Queue {
8279
* @param startQueue - {boolean} - Whether or not to immediately begin prcessing queue. If false queue.start() must be manually called.
8380
*/
8481
createJob(name, payload = {}, options = {}, startQueue = true) {
85-
8682
if (!name) {
8783
throw new Error('Job name must be supplied.');
8884
}
@@ -109,10 +105,9 @@ export class Queue {
109105
});
110106

111107
// Start queue on job creation if it isn't running by default.
112-
if (startQueue && this.status == 'inactive') {
108+
if (startQueue && this.status === 'inactive') {
113109
this.start();
114110
}
115-
116111
}
117112

118113
/**
@@ -139,9 +134,8 @@ export class Queue {
139134
* @return {boolean|undefined} - False if queue is already started. Otherwise nothing is returned when queue finishes processing.
140135
*/
141136
async start(lifespan = 0) {
142-
143137
// If queue is already running, don't fire up concurrent loop.
144-
if (this.status == 'active') {
138+
if (this.status === 'active') {
145139
return false;
146140
}
147141

@@ -160,8 +154,7 @@ export class Queue {
160154
concurrentJobs = await this.getConcurrentJobs();
161155
}
162156

163-
while (this.status == 'active' && concurrentJobs.length) {
164-
157+
while (this.status === 'active' && concurrentJobs.length) {
165158
// Loop over jobs and process them concurrently.
166159
const processingJobs = concurrentJobs.map( job => {
167160
return this.processJob(job);
@@ -179,11 +172,9 @@ export class Queue {
179172
} else {
180173
concurrentJobs = await this.getConcurrentJobs();
181174
}
182-
183175
}
184176

185177
this.status = 'inactive';
186-
187178
}
188179

189180
/**
@@ -206,22 +197,16 @@ export class Queue {
206197
* @return {promise} - Promise that resolves to a collection of all the jobs in the queue.
207198
*/
208199
async getJobs(sync = false) {
209-
210200
if (sync) {
211-
212201
let jobs = null;
213202
this.realm.write(() => {
214-
215203
jobs = Array.from(this.realm.objects('Job'));
216-
217204
});
218205

219206
return jobs;
220-
221207
} else {
222208
return Array.from(await this.realm.objects('Job'));
223209
}
224-
225210
}
226211

227212
/**
@@ -239,11 +224,9 @@ export class Queue {
239224
* @return {promise} - Promise resolves to an array of job(s) to be processed next by the queue.
240225
*/
241226
async getConcurrentJobs(queueLifespanRemaining = 0) {
242-
243227
let concurrentJobs = [];
244228

245229
this.realm.write(() => {
246-
247230
// Get next job from queue.
248231
let nextJob = null;
249232

@@ -294,9 +277,7 @@ export class Queue {
294277
.sorted([['priority', true], ['created', false]]));
295278

296279
concurrentJobs = reselectedJobs.slice(0, concurrency);
297-
298280
}
299-
300281
});
301282

302283
return concurrentJobs;
@@ -319,7 +300,6 @@ export class Queue {
319300
* @param job {object} - Job realm model object
320301
*/
321302
async processJob(job) {
322-
323303
// Data must be cloned off the realm job object for several lifecycle callbacks to work correctly.
324304
// This is because realm job is deleted before some callbacks are called if job processed successfully.
325305
// More info: https://github.com/billmalarky/react-native-queue/issues/2#issuecomment-361418965
@@ -331,27 +311,21 @@ export class Queue {
331311
this.worker.executeJobLifecycleCallback('onStart', jobName, jobId, jobPayload);
332312

333313
try {
334-
335314
await this.worker.executeJob(job);
336315

337316
// On successful job completion, remove job
338317
this.realm.write(() => {
339-
340318
this.realm.delete(job);
341-
342319
});
343320

344321
// Job has processed successfully, fire onSuccess and onComplete job lifecycle callbacks.
345322
this.worker.executeJobLifecycleCallback('onSuccess', jobName, jobId, jobPayload);
346323
this.worker.executeJobLifecycleCallback('onComplete', jobName, jobId, jobPayload);
347-
348324
} catch (error) {
349-
350325
// Handle job failure logic, including retries.
351326
let jobData = JSON.parse(job.data);
352327

353328
this.realm.write(() => {
354-
355329
// Increment failed attempts number
356330
if (!jobData.failedAttempts) {
357331
jobData.failedAttempts = 1;
@@ -375,7 +349,6 @@ export class Queue {
375349
if (jobData.failedAttempts >= jobData.attempts) {
376350
job.failed = new Date();
377351
}
378-
379352
});
380353

381354
// Execute job onFailure lifecycle callback.
@@ -386,9 +359,7 @@ export class Queue {
386359
this.worker.executeJobLifecycleCallback('onFailed', jobName, jobId, jobPayload);
387360
this.worker.executeJobLifecycleCallback('onComplete', jobName, jobId, jobPayload);
388361
}
389-
390362
}
391-
392363
}
393364

394365
/**
@@ -401,31 +372,21 @@ export class Queue {
401372
* @param jobName {string} - Name associated with job (and related job worker).
402373
*/
403374
flushQueue(jobName = null) {
404-
405375
if (jobName) {
406-
407376
this.realm.write(() => {
408-
409377
let jobs = Array.from(this.realm.objects('Job')
410378
.filtered('name == "' + jobName + '"'));
411379

412380
if (jobs.length) {
413381
this.realm.delete(jobs);
414382
}
415-
416383
});
417-
418384
} else {
419385
this.realm.write(() => {
420-
421386
this.realm.deleteAll();
422-
423387
});
424388
}
425-
426389
}
427-
428-
429390
}
430391

431392
/**
@@ -435,10 +396,8 @@ export class Queue {
435396
* @return {Queue} - A queue instance.
436397
*/
437398
export default async function queueFactory() {
438-
439399
const queue = new Queue();
440400
await queue.init();
441401

442402
return queue;
443-
444403
}

Models/Worker.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
* Worker Model
44
*
55
*/
6-
76
export default class Worker {
8-
97
/**
108
*
119
* Singleton map of all worker functions assigned to queue.
@@ -33,7 +31,6 @@ export default class Worker {
3331
* @param options {object} - Worker options. See README.md for worker options info.
3432
*/
3533
addWorker(jobName, worker, options = {}) {
36-
3734
// Validate input.
3835
if (!jobName || !worker) {
3936
throw new Error('Job name and associated worker function must be supplied.');
@@ -73,14 +70,12 @@ export default class Worker {
7370
* @return {number}
7471
*/
7572
getConcurrency(jobName) {
76-
7773
// If no worker assigned to job name, throw error.
7874
if (!Worker.workers[jobName]) {
7975
throw new Error('Job ' + jobName + ' does not have a worker assigned to it.');
8076
}
8177

8278
return Worker.workers[jobName].options.concurrency;
83-
8479
}
8580

8681
/**
@@ -93,7 +88,6 @@ export default class Worker {
9388
* @param job {object} - Job realm model object
9489
*/
9590
async executeJob(job) {
96-
9791
// If no worker assigned to job name, throw error.
9892
if (!Worker.workers[job.name]) {
9993
throw new Error('Job ' + job.name + ' does not have a worker assigned to it.');
@@ -107,21 +101,16 @@ export default class Worker {
107101
const jobPayload = JSON.parse(job.payload);
108102

109103
if (jobTimeout > 0) {
110-
111104
let timeoutPromise = new Promise((resolve, reject) => {
112-
113105
setTimeout(() => {
114106
reject(new Error('TIMEOUT: Job id: ' + jobId + ' timed out in ' + jobTimeout + 'ms.'));
115107
}, jobTimeout);
116-
117108
});
118109

119110
await Promise.race([timeoutPromise, Worker.workers[jobName](jobId, jobPayload)]);
120-
121111
} else {
122112
await Worker.workers[jobName](jobId, jobPayload);
123113
}
124-
125114
}
126115

127116
/**
@@ -134,7 +123,6 @@ export default class Worker {
134123
* @param jobPayload {object} - Data payload associated with job.
135124
*/
136125
async executeJobLifecycleCallback(callbackName, jobName, jobId, jobPayload) {
137-
138126
// Validate callback name
139127
const validCallbacks = ['onStart', 'onSuccess', 'onFailure', 'onFailed', 'onComplete'];
140128
if (!validCallbacks.includes(callbackName)) {
@@ -144,15 +132,11 @@ export default class Worker {
144132
// Fire job lifecycle callback if set.
145133
// Uses a try catch statement to gracefully degrade errors in production.
146134
if (Worker.workers[jobName].options[callbackName]) {
147-
148135
try {
149136
await Worker.workers[jobName].options[callbackName](jobId, jobPayload);
150137
} catch (error) {
151138
console.error(error); // eslint-disable-line no-console
152139
}
153-
154140
}
155-
156141
}
157-
158-
}
142+
}

config/Database.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* Realm database bootstrap
33
*/
4-
54
import { Config } from './config';
65
import Realm from 'realm';
76

@@ -38,11 +37,9 @@ class JobSchema {
3837
}
3938

4039
export default class Database {
41-
4240
static realmInstance = null; // Use a singleton connection to realm for performance.
4341

4442
static async getRealmInstance(options = {}) {
45-
4643
// Connect to realm if database singleton instance has not already been created.
4744
if (Database.realmInstance === null) {
4845

@@ -52,13 +49,9 @@ export default class Database {
5249
schema: [JobSchema]
5350

5451
// Look up shouldCompactOnLaunch to auto-vacuum https://github.com/realm/realm-js/pull/1209/files
55-
5652
});
57-
5853
}
5954

6055
return Database.realmInstance;
61-
6256
}
63-
64-
}
57+
}

config/config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* Config constants
44
*
55
*/
6-
76
export const Config = {
87
REALM_PATH: 'reactNativeQueue.realm', // Name of realm database.
98
REALM_SCHEMA_VERSION: 0 // Must be incremented if data model updates.
10-
};
9+
};

0 commit comments

Comments
 (0)