@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:
- The polyfills for getting
IEHRClient
working without errors inReact Native
. See: [polyfillIEHRWebAPIs
] - The
ExpoClientStorage
class, which enablesIEHRClient
to persist what is normally stored inLocalStorage
on the web client into a secure storage on a mobile device. Under the hood it uses Expo'sSecureStore
, but abstracts away the complexity of its asynchronous APIs, since theStorage
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>
);
}