npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-background-services

v1.3.6

Published

A React Native library for managing background services

Downloads

32

Readme

React Native Background Services

A React Native library for managing background services.

Installation

To install the library, run:

npm install react-native-background-services
Setup
Ensure you have linked the native modules properly. If you're using React Native 0.60 and above, the linking is handled automatically.

For manual linking, follow the instructions here.

Usage
Main Component
The main component provided by the library is BackGroundServices. It helps you manage background services in your React Native application.

Importing the Component
javascript
import { BackGroundServices } from 'react-native-background-services';
Props
Title (string): The title of the background service.
Text (string): The text displayed by the background service.
onEachSecond (number): The interval in seconds for the background service to trigger.
onServiceRunning (function): Callback function that is called each time the background service triggers.
IsServiceRunning (function) (optional): Callback function that receives a boolean indicating if the background service is running.
killService (boolean): Boolean to stop the background service.
Example Usage
javascript
import React, { useState } from 'react';
import { View, Text } from 'react-native';
import { BackGroundServices } from 'react-native-background-services';

const App = () => {
  const [isServiceRunning, setIsServiceRunning] = useState(false);

  const handleServiceRunning = (status) => {
    console.log('Service running:', status);
    setIsServiceRunning(status);
  };

  const handleServiceTriggered = () => {
    console.log('Background service triggered');
  };

  return (
    <View>
      <BackGroundServices
        Title="Background Service"
        Text="Service is running..."
        onEachSecond={1}
        onServiceRunning={handleServiceTriggered}
        killService={false}
        IsServiceRunning={handleServiceRunning}
      />
      <Text>Service Running: {isServiceRunning ? 'Yes' : 'No'}</Text>
    </View>
  );
};

export default App;
Detailed Component Code
javascript
import { View, NativeEventEmitter, NativeModules } from 'react-native';
import React, { useEffect, useState } from 'react';

const { BackgroundServiceCheck } = NativeModules;

interface BackGroundServicesInterFace {
  Title: string;
  Text: string;
  onEachSecond: number;
  onServiceRunning: () => void;
  IsServiceRunning?: (boolean: boolean) => void;
  killService: boolean;
}

const BackGroundServices: React.FC<BackGroundServicesInterFace> = ({ Text, Title, onServiceRunning, onEachSecond, killService, IsServiceRunning }) => {
  const [isServiceRunning, setIsServiceRunning] = useState(false);

  const [serviceContent, setServiceContent] = useState({
    Title: Title,
    Text: Text,
  });

  const runTheFunction = async () => {
    if (IsServiceRunning) {
      const status = await BackgroundServiceCheck.isBackgroundServiceRunning();
      IsServiceRunning(status);
      setIsServiceRunning(status);
    }
    onServiceRunning();
  };

  useEffect(() => {
    runTheFunction();
  }, [isServiceRunning]);

  useEffect(() => {
    if (killService) {
      console.log("Services Stopped");
      BackgroundServiceCheck.stopBackgroundService();
      if (IsServiceRunning) {
        IsServiceRunning(false);
      }
    } else {
      const eventEmitter = new NativeEventEmitter(BackgroundServiceCheck);
      if (BackgroundServiceCheck) {
        console.log("Starting background service");
        BackgroundServiceCheck.startBackgroundService(onEachSecond * 1000, serviceContent);
        eventEmitter.addListener('backgroundServiceTriggered', runTheFunction);
      } else {
        console.log("Native module does not exist");
      }
    }
  }, [killService]);

  return <View />;
};

export default BackGroundServices;
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License
MIT


### Explanation:

1. **Installation Instructions**: Details on how to install the library using npm.
2. **Setup Instructions**: Notes on ensuring proper linking of native modules.
3. **Usage Example**: A sample component demonstrating how to use the `BackGroundServices` component, including handling service running status and triggering events.
4. **Component Code**: The full code of the `BackGroundServices` component for reference.
5. **Contributing**: Information on how to contribute to the project.
6. **License**: Licensing information.

This README should give users a clear understanding of how to install, set up, and use your Rea