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

@iehr/expo-polyfills

v4.3.182

Published

A module for polyfilling the minimum necessary web APIs for using the iEHR client on React Native

Downloads

512

Readme

@iehr/expo-polyfills

A module for polyfilling the minimum necessary web APIs for using the iEHR client on React Native

Installation in managed Expo projects

For managed Expo projects, please follow the installation instructions in the API documentation for the latest stable release. If you follow the link and there is no documentation available then this library is not yet usable within managed projects — it is likely to be included in an upcoming Expo SDK release.

Expo SDK Compatibility

It's recommended you use the latest Expo SDK (SDK 51 as of May 2024).

However, this package should be compatible with Expo SDK 49+.

Going forward, each version of this package will advertise the minimum compatible Expo SDK required which is subject to change based on the breaking changes of underlying Expo packages.

Installation in bare React Native projects

For bare React Native projects, you must ensure that you have installed and configured the expo package before continuing.

Add the package to your npm dependencies

npm install @iehr/expo-polyfills

Overview

There are currently two major components to this package:

  1. The polyfills for getting IEHRClient working without errors in React Native. See: [polyfillIEHRWebAPIs]
  2. The ExpoClientStorage class, which enables IEHRClient to persist what is normally stored in LocalStorage on the web client into a secure storage on a mobile device. Under the hood it uses Expo's SecureStore, but abstracts away the complexity of its asynchronous APIs, since the Storage interface is normally synchronous in nature.

Usage

To get full compatibility with the IEHRClient in React Native, call the polyfillIEHRWebAPIs in the app root and pass in an ExpoClientStorage into your IEHRClient. If you want to wait to load components until after the IEHRClient has initialized, you can conditionally render based on the loading property from the useIEHRContext hook.

import { IEHRClient } from '@iehr/core';
import { IEHRProvider, useIEHRContext } from '@iehr/react-hooks';
import { polyfillIEHRWebAPIs, ExpoClientStorage } from '@iehr/expo-polyfills';

polyfillIEHRWebAPIs();

const iehr = new IEHRClient({ storage: new ExpoClientStorage() });

function Home(): JSX.Element {
  const { loading } = useIEHRContext();
  return loading ? <div>Loading...</div> : <div>Loaded!</div>;
}

function App(): JSX.Element {
  return (
    <IEHRProvider iehr={iehr}>
      <Home />
    </IEHRProvider>
  );
}

Usage with Expo Router

When using IEHRClient with Expo Router, you will likely need to disable the polyfill for window.location; Expo Router provides a polyfill that better interoperates with the package than the iEHR-provided one. See: https://expo.github.io/router/docs/lab/runtime-location#native

To disable the iEHR window.location polyfill, simply pass the following config to polyfillIEHRWebAPIs:

polyfillIEHRWebAPIs({ location: false });

Usage with the iEHR useSubscription hook

When using useSubscription in your Expo app, there is one more function you should call in the root of your app: initWebSocketManager. The function just takes the IEHRClient instance you will be using. You can get useSubscription working in your Expo app like so:

import { IEHRClient, useSubscription } from '@iehr/core';
import { IEHRProvider, useIEHRContext } from '@iehr/react-hooks';
import { polyfillIEHRWebAPIs, ExpoClientStorage, initWebSocketManager } from '@iehr/expo-polyfills';

polyfillIEHRWebAPIs();

const iehr = new IEHRClient({ storage: new ExpoClientStorage() });

initWebSocketManager(iehr);

function Counter(): JSX.Element {
  const [count, setCount] = useState(0);

  useSubscription(
    'Communication',
    (_bundle: Bundle) => {
      setCount((s) => s + 1);
    }
  );

  return <div>Count: {count}</div>
}

function Home(): JSX.Element {
  const { loading } = useIEHRContext();
  return loading ? <div>Loading...</div> : <Counter />;
}

function App(): JSX.Element {
  return (
    <IEHRProvider iehr={iehr}>
      <Home />
    </IEHRProvider>
  );
}

Managing backgrounding of app when using useSubscription

Due to stability concerns on both the mobile app and iEHR server, we automatically close the WebSocket connection when the mobile app is backgrounded / goes inactive. However, we will automatically seamlessly reconnect the WebSocket when the app becomes active again. This means that you may miss notifications for a subscription in between disconnecting and reconnecting. We try to make it more ergonomic for managing the "catch-up" process for developers by providing lifecycle "hooks" (not React hooks, but options in the useSubscription hook itself). We have the following lifecycle events that you can use to make sure you don't miss an event for a resource:

  • onWebSocketOpen - When the WebSocket itself makes a successful connection.
  • onWebSocketOpen - When the WebSocket itself closes.
  • onSubscriptionConnect - When a particular subscription has been established and we are sure that we are receiving notification events for it.
  • onSubscriptionDisconnect - When a particular subscription is disconnected and we are no longer getting notification events for it.

Here is how you can use these lifecycle callbacks to notify the user that the connection has been lost and find any messages that have been missed after it reconnects to this particular subscription:

import { IEHRClient } from '@iehr/core';
import { IEHRProvider, useIEHRContext, useIEHR, useSubscription } from '@iehr/react-hooks';
import { polyfillIEHRWebAPIs, ExpoClientStorage, initWebSocketManager } from '@iehr/expo-polyfills';

polyfillIEHRWebAPIs();

const iehr = new IEHRClient({ storage: new ExpoClientStorage() });

initWebSocketManager(iehr);

function Counter(): JSX.Element {
  const iehr = useIEHR();
  const [count, setCount] = useState(0);
  const [reconnecting, setReconnecting] = useState(false);
  const lastMessageTime = useRef<string>(new Date().toISOString());

  useSubscription(
    'Communication',
    (_bundle: Bundle) => {
      setCount((s) => s + 1);
      lastMessageTime.current = new Date().toISOString();
    },
    {
      onWebSocketClose: useCallback(() => {
        if (!reconnecting) {
          setReconnecting(true);
        }
        showNotification({ color: 'red', message: 'Live chat disconnected. Attempting to reconnect...' });
      }, [setReconnecting, reconnecting]),
      onWebSocketOpen: useCallback(() => {
        if (reconnecting) {
          showNotification({ color: 'green', message: 'Live chat reconnected.' });
        }
      }, [reconnecting]),
      onSubscriptionConnect: useCallback(() => {
        if (reconnecting) {
          const searchParams = new URLSearchParams();
          searchParams.append('_sort', '-_lastUpdated');
          // Get messages that are greater than the last received timestamp
          if (lastMessageTime.current) {
            searchParams.append('_lastUpdated', `gt${lastMessageTime.current}`);
          }
          lastMessageTime.current = new Date().toISOString();
          iehr.searchResources('Communication', searchParams, { cache: 'no-cache' })
            .then((communications: Communication[]) => {
              setCount(s => s + communications.length);
            })
            .catch((err) => showNotification({ color: 'red', message: normalizeErrorString(err) }));
          setReconnecting(false);
        }
      }, [reconnecting, setReconnecting, iehr]),
    }
  );

  return <div>Count: {count}</div>
}

function Home(): JSX.Element {
  const { loading } = useIEHRContext();
  return loading ? <div>Loading...</div> : <Counter />;
}

function App(): JSX.Element {
  return (
    <IEHRProvider iehr={iehr}>
      <Home />
    </IEHRProvider>
  );
}