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-timer-native

v2.0.1

Published

A better way to manage timers on react native (taking into account difference between device an debugger time on remote js debug mode)

Downloads

7

Readme

React Native Timer Native

Fork of the package react-native-timer adds support of native android time difference between the device and the remote debugger when in JS Remote Debug mode

THE DEVICE TIME MUST BE IN THE FUTURE COMPARED TO THE REMOTE DEBUGGER TIME

Install

npm install --save react-native-timer-native

Android

  • Add the following line to the bottom of your project's settings.gradle file.

    project(':react-native-timer-native').projectDir = new File(settingsDir, '../node_modules/react-native-timer-native/android')

  • add an include line in your project's settings.gradle to include the :react-native-timer-native project.

    include ':react-native-timer-native'

  • Open your app's build.gradle file and add the following line to the dependencies block.

    compile project(":react-native-timer-native")

  • In your app's MainActivity.java file, add new TimerReactPackage() to the return statement of the getPackages() function.

...
    new MainReactPackage(),
    new TimerReactPackage()
...
  • Then in the same file add the import statement : import com.odinvt.reactnativetimernative.TimerReactPackage;

A better way to manage timers in react-native with ES6 components, using WeakMap.

  1. Often you need to do things like show a message for a few seconds, and then hide it, or run an operation again and again at a specific interval. These things will usually happen inside a React Component, and will start after a component has mounted. So, you really cannot just do a setTimeout(fn, 2000) for non trivial things. You need to do a this.timer = setTimeout(fn, 2000), and then clearTimeout(this.timer) in componentWillUnmount.

  2. When a component unmounts, these timers have to be cleared and, so that you are not left with zombie timers doing things when you did not expect them to be there.

  3. React, right now, offers a solution using the react-native-timer-mixin for this. However, mixins are not part of ES6-7 standard, and probably will never be as they get in the way of good software design. And this brings us to the package in question, react-native-timer-native.

  4. With react-native-timer-native, you can set different timers, like timeout, interval etc in the context of a react component, and unmount all of them when the component unmounts, at context level.

Generic API:

(Automatically makes up for the difference of time when in remote js debug mode)

const timer = require('react-native-timer-native');

// timers maintained in the Map timer.timeouts
timer.setTimeout(name, fn, interval);
timer.clearTimeout(name);
timer.timeoutExists(name);

// timers maintained in the Map timer.intervals
timer.setInterval(name, fn, interval);
timer.clearInterval(name);
timers.intervalExists(name);

// timers maintained in the Map timer.immediates
timer.setImmediate(name, fn);
timer.clearImmediate(name);
timers.immediateExists(name);

// timers maintained in the Map timer.animationFrames
timer.requestAnimationFrame(name, fn);
timer.cancelAnimationFrame(name);
timers.animationFrameExists(name);

Mostly, using timers is a pain inside react-native components, so we present to you Contextual Timers. API:


timer.setTimeout(context, name, fn, interval);
timer.clearTimeout(context, name);
timer.clearTimeout(context) // clears all timeouts for a context
timer.timeoutExists(context, name);

timer.setInterval(context, name, fn, interval);
timer.clearInterval(context, name);
timer.clearInterval(context); // clears all intervals for a context
timer.intervalExists(context, name);

timer.setImmediate(context, name, fn);
timer.clearImmediate(context, name);
timer.clearImmediate(context); // clears all immediates for a context
timer.immediateExists(context, name);

timer.requestAnimationFrame(context, name, fn);
timer.cancelAnimationFrame(context, name);
timer.cancelAnimationFrame(context); // cancels all animation frames for a context
timer.animationFrameExists(context, name);

Example Below:

const timer = require('react-native-timer-native');

class Foo extends React.Component {
  state = {
    showMsg: false
  };

  componentWillUnmount() {
    timer.clearTimeout(this);
  }

  showMsg() {
    this.setState({showMsg: true}, () => timer.setTimeout(
      this, 'hideMsg', () => this.setState({showMsg: false}), 2000
    ));
  }

  render() {
    return {
      <View style={{flex: 1}}>
        <TouchableOpacity onPress={() => requestAnimationFrame(() => this.showMsg())}>
          <Text>Press Me</Text>
        </TouchableOpacity>

        {this.state.showMsg ? (
          <Text>Hello!!</Text>
        ) : (
          null
        )}
      </View>
    }
  }
}

PS: Kinda not a best practice, but const t = require('react-native-timer-native') can cut down some typing. Also, this lib can be used in browsers too, but will focus on them when I am working with them.