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-networking-patch

v1.2.2

Published

Improves the performance of the React Native network module and adds a timeout feature.

Downloads

775

Readme

NPM Version

What is this for?

React Native uses 6 event listeners to process networking events. For each request, these 6 listeners are registered and removed whenever processing. This is not the most effective approach. This patch makes RN's networking module work more efficiently.

# Performance Improvement

It improves performance using only one event for each request. Because bridge communication is reduced, it's more efficient and improves overall app performance.

The following bridge communications occur for each fetch request.

JS->N : Networking.addListener(["didSendNetworkData"])
JS->N : Networking.addListener(["didReceiveNetworkResponse"])
JS->N : Networking.addListener(["didReceiveNetworkData"])
JS->N : Networking.addListener(["didReceiveNetworkIncrementalData"])
JS->N : Networking.addListener(["didReceiveNetworkDataProgress"])
JS->N : Networking.addListener(["didCompleteNetworkResponse"])
JS->N : Networking.sendRequest([{"method":"GET","url":"...."....])
N->JS : <callback for Networking.sendRequest>([2])
N->JS : RCTDeviceEventEmitter.emit(["didReceiveNetworkResponse", ....])
N->JS : RCTDeviceEventEmitter.emit(["didReceiveNetworkData", ...])
N->JS : RCTDeviceEventEmitter.emit(["didCompleteNetworkResponse", ...])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])
JS->N : Networking.removeListeners([1])

The patched version looks like this.

JS->N : Networking.addListener(["events"])
JS->N : Networking.sendRequest([{"method":"GET","url":"...."....])
N->JS : <callback for Networking.sendRequest>([2])
N->JS : RCTDeviceEventEmitter.emit(["events", ....])
N->JS : RCTDeviceEventEmitter.emit(["events", ...])
N->JS : RCTDeviceEventEmitter.emit(["events", ...])
JS->N : Networking.removeListeners([1])

Other optimizations are also included.

# Global Timeout

fetch has no built-in timeout option. As you know, there are many workarounds such as using XMLHttpRequest API, AbortController and setTimeout + Promise. I want a simple and easy way. Now, you can set a global timeout without these workarounds.

// RN >= 0.62
import { Networking } from 'react-native';

// RN < 0.62
// import Networking from 'react-native/Libraries/Network/RCTNetworking';

// Setting default global timeout. You only need to set it once.
Networking.setTimeout(3000);

// After 3 seconds, a timeout exception is thrown.
async function getItem() {
  let item = null;

  try {
    const response = await fetch(....);
    item = await response.json();
  } catch (e) {
    console.error(e);
  }

  return item;
}

// `axios` works with a 10 second timeout, not 3 seconds.
async function getLongItem() {
  const instance = axios.create({
    baseURL: '...',
    timeout: 10000,
  });
  ....
}

Usage

Requirement

It works with v0.63.2 or higher of RN. If not, please upgrade to the latest version. Of course, it works on Expo.

Install

If you're using RN v0.66 or higher Once installed, react-native is automatically patched.

yarn add react-native-networking-patch --dev

If you're using RN v0.63.x ~ v0.65.1, You must use the specific version below.

  • RN v0.65.1
yarn add [email protected] --dev
  • RN v0.65.0, RN v0.64.3
yarn add [email protected] --dev
  • RN v0.64.2
yarn add [email protected] --dev
  • RN v0.64.0
yarn add [email protected] --dev
  • RN v0.63.x
yarn add [email protected] --dev

prepare should be added to prevent this patch from being restored whenever packages are changed.

// package.json
{
  ...
  "scripts": {
    ...,
    "prepare": "yarn rn-networking-patch"
  }
}

If you were already using prepare, you can add the patch script later.

"prepare": "yarn jetify; yarn rn-networking-patch"

Execute manually

You can execute the patch manually with the command below.

yarn rn-networking-patch

Uninstall

Just delete the command you added to prepare and remove react-native-networking-patch package.