Skip to content

πŸš€ JSI-based task scheduler for React Native β€” periodically polls APIs, hashes responses to prevent redundant updates, and manages all tasks natively.

License

Notifications You must be signed in to change notification settings

pioner92/react-native-sync-tasks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ SyncTasksManager (JSI)

SyncTasksManager is a native JSI-based library for React Native that allows you to manage and execute native-threaded sync tasks (such as periodic API polling) efficiently from JavaScript, while delegating the actual execution to the native layer for better performance.

⚠️ Note: This is not a background task manager β€” tasks do not continue running when the app is in the background or killed. The polling happens while the app is active, on a native thread, outside the JS thread.


βš™οΈ Features

  • πŸ” Periodic HTTP polling with configurable interval
  • πŸ“‘ Callback on data reception or error
  • 🧡 High-performance execution via native thread using JSI
  • 🧠 Centralized task management (start/stop all tasks)
  • βœ… Seamless integration with native modules (C++/JSI)
  • ✨ Built-in response deduplication using response body hash β€” avoids redundant onData calls if the response has not changed

πŸ“¦ Installation

npm install react-native-sync-tasks

Don’t forget to run pod install for iOS if using CocoaPods.


πŸ› οΈ Usage

import { createTask, SyncTasksManager } from 'react-native-sync-tasks';

type TData = {
  userId: number;
  id: number;
  title: string;
  body: string;
};

const task = createTask<TData>({
  config: {
    url: 'https://jsonplaceholder.typicode.com/posts/1',
    // 2000ms / default 1000ms
    interval: 2000,
    // headers optional
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    },
  },
  // { body: TData, status_code: number }
  onData: (data) => {
    console.log('DATA', data);
  },
  // { error: string, status_code: number }
  onError: (error) => {
    console.log('ERROR', error);
  },
});

SyncTasksManager.addTask(task);
SyncTasksManager.startAll();
...
// stop all tasks
SyncTasksManager.stopAll();
// or stop only 1 task
task.stop();

πŸ” API

createTask<T>(props: CreateTaskParams<T>): Task

Creates a task that will periodically fetch data from the specified URL, executed on a native thread.

Params:

Name Type Description
config { url: string; interval: number; headers?: Record<string, string> } HTTP polling configuration
onData (data: { body: T, status_code: number }) => void Callback when data is successfully received
onError (error: { error: string, status_code: number }) => void Callback when request fails (optional)

Under the hood, the task stores a hash of the last response body. If the newly fetched response is identical (hash matches), the onData callback will not be triggered.

Task

Represents an individual polling task running on a native thread.

Methods:

  • start(): void β€” Manually start the task
  • stop(): void β€” Stop the task
  • isRunning(): boolean β€” Check if the task is currently running

SyncTasksManager

A global manager to control multiple polling tasks.

Methods:

  • addTask(task: Task): void β€” Add a single task
  • addTasks(tasks: Task[]): void β€” Add multiple tasks
  • startAll(): void β€” Start all registered tasks
  • stopAll(): void β€” Stop all running tasks

πŸ“„ License

MIT


πŸŽ‰ Enjoy using react-native-sync-tasks! Offload polling to native threads and keep your JS thread free.

About

πŸš€ JSI-based task scheduler for React Native β€” periodically polls APIs, hashes responses to prevent redundant updates, and manages all tasks natively.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published