From a515d21a6880b7d73723579c9dbe67b0114f6883 Mon Sep 17 00:00:00 2001 From: Gustavo Bonfim Date: Mon, 15 Jul 2024 09:02:41 -0300 Subject: [PATCH 1/2] feat: verify app state before schedule queue to support background state --- src/Queue.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Queue.ts b/src/Queue.ts index 5d3ea41..437a2ca 100644 --- a/src/Queue.ts +++ b/src/Queue.ts @@ -1,4 +1,4 @@ -import { NativeModules } from 'react-native'; +import { NativeModules, AppState } from 'react-native'; import { FALSE, Job, RawJob } from './models/Job'; import { JobStore } from './models/JobStore'; @@ -223,7 +223,11 @@ export class Queue { await Promise.all(resetTasks); } private scheduleQueue() { - this.timeoutId = setTimeout(this.runQueue, this.updateInterval); + if (AppState.currentState === 'active') { + this.timeoutId = setTimeout(this.runQueue, this.updateInterval); + } else { + this.runQueue(); + } } private runQueue = async () => { if (!this.isActive) { From 887b46eff8311ee01ef09553fe13c7f73d7a0c04 Mon Sep 17 00:00:00 2001 From: Gustavo Bonfim Date: Mon, 15 Jul 2024 09:14:10 -0300 Subject: [PATCH 2/2] chore: check app state before executing worker with timeout --- src/Worker.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Worker.ts b/src/Worker.ts index 241daa9..b0a9e9e 100644 --- a/src/Worker.ts +++ b/src/Worker.ts @@ -1,3 +1,4 @@ +import { AppState } from 'react-native'; import { Job, RawJob } from './models/Job'; export const CANCEL = 'rn_job_queue_cancel'; @@ -78,7 +79,7 @@ export class Worker

{ const job = { ...rawJob, ...{ payload } }; this.executionCount++; this.onStart(job); - if (timeout > 0) { + if (timeout > 0 && AppState.currentState === 'active') { return this.executeWithTimeout(job, timeout); } else { return this.executer(payload, job.id);