import type React from 'react'; import { findNodeHandle } from 'react-native'; import type { Nullish } from '../types/UtilityTypes'; /** wrapper func for setState that returns a promise */ // eslint-disable-next-line consistent-this export function setStateAsync( that: React.Component, newState: T | ((prevState: T) => T) ){ return new Promise((resolve) => { that.setState(newState, () => { resolve(); }); }); }; /** wrapper for timeout that returns a promise */ export function timeout(ms: Number) { return new Promise(resolve => { const timeoutID = setTimeout(() => { clearTimeout(timeoutID); resolve(); }, ms) }); }; /** Wraps a promise that will reject if not not resolved in milliseconds */ export function promiseWithTimeout(ms: Number, promise: Promise){ // Create a promise that rejects in milliseconds const timeoutPromise = new Promise((_, reject) => { const timeoutID = setTimeout(() => { clearTimeout(timeoutID); reject(`Promise timed out in ${ms} ms.`) }, ms); }); // Returns a race between our timeout and the passed in promise return Promise.race([promise, timeoutPromise]); }; export function isClassComponent(component: unknown) { return !!( typeof component === 'function' && component?.prototype?.isReactComponent ); }; export function lastElement(array: Array): T | undefined { if(!array) return undefined; return array[array.length - 1]; }; export function arrayInsert(array: Array, index: number, newItem: T){ return [ // part of the array before the specified index ...array.slice(0, index), // inserted item newItem, // part of the array after the specified index ...array.slice(index) ]; }; export function addOptional (a: T, b: U | null): T & Partial { return {...a, ...b}; }; export function shallowMergeObjects( a: Nullish, b: Nullish ): T | null { if (a == null && b == null) return null; else if(a != null && b == null) return a; else if(a == null && b != null) return b; else return { ...a, ...b } as T; }; export function getNativeNodeHandle(nativeRef: React.Component){ const nodeHandle = findNodeHandle(nativeRef); if(nodeHandle == null){ throw new Error('Unable to get the node handle for the native ref.'); }; return nodeHandle; };