Skip to content

BackgroundTimer.clearTimeout does not stop ongoing background task #523

@rakshitbharat

Description

@rakshitbharat

cant stop the running task

import BackgroundTimer from 'react-native-background-timer';
import {DeviceEventEmitter} from 'react-native';

/**
 * Class BackgroundTaskManager is designed to manage background tasks using
 * react-native-background-timer. However, the task started by scheduleTask
 * does not stop when finish is called.
 */
class BackgroundTaskManager {
  static instance = null;
  static iteration = 0;
  static backgroundTaskId = null;

  constructor() {
    if (!BackgroundTaskManager.instance) {
      BackgroundTaskManager.instance = this;
      BackgroundTaskManager.resetInit();
      console.log('BackgroundTaskManager initialized');
    }

    return BackgroundTaskManager.instance;
  }

  /**
   * Resets the task manager and clears any existing background task.
   */
  static resetInit() {
    this.configArgsiOS = null;
    this.configStartFunction = null;
    this.configTimeoutFunction = null;
    this.task = null;
    this.options = null;
    this.iteration++;
    if (this.backgroundTaskId !== null) {
      BackgroundTimer.clearTimeout(this.backgroundTaskId);
      this.backgroundTaskId = null;
    }
    console.log('BackgroundTaskManager reset');
  }

  /**
   * Configures the task manager with provided arguments.
   */
  static async configure(...args) {
    BackgroundTaskManager.resetInit();
    console.log('Configuring with args:', args);
    if (args.length > 1) {
      this.configStartFunction = args[1];
    }
    if (args.length > 2) {
      this.configTimeoutFunction = args[2];
    }
    console.log('Configuration is now unified for both platforms.');
  }

  /**
   * Schedules a new background task to start after a specified delay.
   */
  static async scheduleTask(...args) {
    const { taskId } = args[0];
    console.log('Scheduling task with args:', args);

    this.backgroundTaskId = BackgroundTimer.setTimeout(async () => {
      if (this.configStartFunction) {
        // Example long-running task
        let count = 0;
        while (true) {
          console.log(count++);
          await BackgroundTaskManager.delay(1000);
        }
      }
    }, 1000); // 1 second delay

    console.log(`Background task ${taskId} scheduled`);
  }

  /**
   * Attempts to stop the background task. However, this does not
   * seem to stop the ongoing task.
   */
  static async finish() {
    console.log(`Stopping background task`, {
      bg: BackgroundTaskManager.backgroundTaskId,
    });
    BackgroundTimer.clearTimeout(BackgroundTaskManager.backgroundTaskId);
    console.log(`Background task stopped`);
  }

  static delay = ms => new Promise(resolve => setTimeout(resolve, ms));
}

export default BackgroundTaskManager;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions