|
1 | | -# react-native-sync-tasks |
| 1 | +# π SyncTasksManager (JSI) |
2 | 2 |
|
3 | | -api sync tasks |
| 3 | +<p align="center"> |
| 4 | +<img src="./assets/img.png" width="600"/> |
| 5 | +</p> |
4 | 6 |
|
5 | | -## Installation |
| 7 | +**SyncTasksManager** is a native JSI-based library for React Native that allows you to manage and execute background sync tasks (such as periodic API polling) efficiently from JavaScript, while delegating the actual execution to the native layer for better performance. |
6 | 8 |
|
7 | | -```sh |
8 | | -npm install react-native-sync-tasks |
| 9 | +--- |
| 10 | + |
| 11 | +## βοΈ Features |
| 12 | + |
| 13 | +- π Periodic HTTP polling with configurable interval |
| 14 | +- π‘ Callback on data reception or error |
| 15 | +- π§΅ High-performance task execution via JSI |
| 16 | +- π§ Centralized task management (start/stop all tasks) |
| 17 | +- β
Seamless integration with native modules (C++/JSI) |
| 18 | +- β¨ Built-in response deduplication using response body hash β avoids redundant `onData` calls if the response has not changed |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## π¦ Installation |
| 23 | + |
| 24 | +> You need to properly install and link the native JSI module `SyncTasksManager` on both iOS and Android. |
| 25 | +
|
| 26 | +```bash |
| 27 | +# Install your native module here (replace with real package) |
| 28 | +npm install react-native-sync-tasks-manager |
9 | 29 | ``` |
10 | 30 |
|
11 | | -## Usage |
| 31 | +> Donβt forget to run `pod install` for iOS if using CocoaPods. |
| 32 | +
|
| 33 | +--- |
12 | 34 |
|
| 35 | +## π οΈ Usage |
13 | 36 |
|
14 | | -```js |
15 | | -import { multiply } from 'react-native-sync-tasks'; |
| 37 | +```ts |
| 38 | +import { createTask, SyncTasksManager } from 'react-native-sync-tasks-manager'; |
16 | 39 |
|
17 | | -// ... |
| 40 | +const task = createTask<{ response: string }>({ |
| 41 | + config: { |
| 42 | + url: 'https://jsonplaceholder.typicode.com/posts/1', |
| 43 | + interval: 2000, |
| 44 | + headers: { |
| 45 | + 'Content-Type': 'application/json', |
| 46 | + 'Accept': 'application/json', |
| 47 | + }, |
| 48 | + }, |
| 49 | + onData: (data) => { |
| 50 | + console.log('DATA', data); |
| 51 | + }, |
| 52 | + onError: (error) => { |
| 53 | + console.log('ERROR', error); |
| 54 | + }, |
| 55 | +}); |
18 | 56 |
|
19 | | -const result = multiply(3, 7); |
| 57 | +SyncTasksManager.addTask(task); |
| 58 | +SyncTasksManager.startAll(); |
20 | 59 | ``` |
21 | 60 |
|
| 61 | +--- |
| 62 | + |
| 63 | +## π API |
| 64 | + |
| 65 | +### `createTask<T>(props: TCreateTaskProps<T>): Task` |
| 66 | + |
| 67 | +Creates a background task that will periodically fetch data from the specified URL. |
22 | 68 |
|
23 | | -## Contributing |
| 69 | +#### Props: |
24 | 70 |
|
25 | | -See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. |
| 71 | +| Name | Type | Description | |
| 72 | +|-----------|----------------------------------------------------------------------|-----------------------------------------------| |
| 73 | +| `config` | `{ url: string; interval: number; headers?: Record<string, string> }` | HTTP polling configuration | |
| 74 | +| `onData` | `(data: T) => void` | Callback when data is successfully received | |
| 75 | +| `onError` | `(error: { error: string; body?: string; code: number }) => void` | Callback when request fails (optional) | |
| 76 | + |
| 77 | +> 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. |
| 78 | +
|
| 79 | +### `Task` |
| 80 | + |
| 81 | +Represents an individual background sync task. |
| 82 | + |
| 83 | +#### Methods: |
| 84 | + |
| 85 | +- `start(): void` β Manually start the task |
| 86 | +- `stop(): void` β Stop the task |
| 87 | +- `isRunning(): boolean` β Check if the task is currently running |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +### `SyncTasksManager` |
| 92 | + |
| 93 | +A global task manager for controlling multiple tasks at once. |
| 94 | + |
| 95 | +#### Methods: |
| 96 | + |
| 97 | +- `addTask(task: Task): void` β Add a single task |
| 98 | +- `addTasks(tasks: Task[]): void` β Add multiple tasks |
| 99 | +- `startAll(): void` β Start all registered tasks |
| 100 | +- `stopAll(): void` β Stop all running tasks |
| 101 | + |
| 102 | +--- |
26 | 103 |
|
27 | | -## License |
| 104 | +## π License |
28 | 105 |
|
29 | 106 | MIT |
30 | 107 |
|
31 | 108 | --- |
32 | 109 |
|
33 | | -Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob) |
|
0 commit comments