UNPKG

@mnightingale/react-native-image-cache-hoc

Version:

React Native Higher Order Component that adds advanced caching functionality to the react native Image component.

139 lines 6.36 kB
/** * * This HOC adds the following functionality to react native <Image> components: * * - File caching. Images will be downloaded to a cache on the local file system. * Cache is maintained until cache size meets a certain threshold at which point the oldest * cached files are purged to make room for fresh files. * * - File persistence. Images will be stored indefinitely on local file system. * Required for images that are related to issues that have been downloaded for offline use. * * More info: https://facebook.github.io/react/docs/higher-order-components.html * */ import React, { ReactNode } from 'react'; import FileSystemFactory, { CacheFileInfo, FileSystem } from './FileSystem'; import { ImageStyle, StyleProp } from 'react-native'; import { BehaviorSubject, Subscription } from 'rxjs'; import { HeaderFn } from './types'; export declare type CacheStrategy = 'immutable' | 'mutable'; export declare type Source = { uri?: string; cache?: CacheStrategy; }; export interface OnLoadEvent { width: number; height: number; } export interface ReactNativeImageCacheHocProps { source?: Source; onLoadFinished?(event: OnLoadEvent): void; style?: StyleProp<ImageStyle>; placeholder?: ReactNode; fileHostWhitelist?: string[]; } interface ReactNativeImageCacheHocState { source?: { uri?: string; }; } export interface ReactNativeImageCacheHocOptions { validProtocols?: string[]; fileHostWhitelist?: string[]; cachePruneTriggerLimit?: number; fileDirName?: string | null; defaultPlaceholder?: ReactNode | null; headers?: HeaderFn; } declare const imageCacheHoc: <P extends object>(Wrapped: React.ComponentType<P>, options?: ReactNativeImageCacheHocOptions) => { new (props: P): { componentId: any; unmounted$: BehaviorSubject<boolean>; options: Required<ReactNativeImageCacheHocOptions>; fileSystem: FileSystem; subscription?: Subscription | undefined; invalidUrl: boolean; _validateImageComponent(): boolean; componentDidMount(): void; /** * * Enables caching logic to work if component source prop is updated (that is, the image url changes without mounting a new component). * See: https://github.com/billmalarky/react-native-image-cache-hoc/pull/15 * * @param prevProps {Object} - Previous props. */ componentDidUpdate(prevProps: ReactNativeImageCacheHocProps): void; componentWillUnmount(): void; onSourceLoaded({ path, md5 }: CacheFileInfo): void; render(): {}; context: any; setState<K extends "source">(state: ReactNativeImageCacheHocState | ((prevState: Readonly<ReactNativeImageCacheHocState>, props: Readonly<P & ReactNativeImageCacheHocProps>) => ReactNativeImageCacheHocState | Pick<ReactNativeImageCacheHocState, K> | null) | Pick<ReactNativeImageCacheHocState, K> | null, callback?: (() => void) | undefined): void; forceUpdate(callback?: (() => void) | undefined): void; readonly props: Readonly<P & ReactNativeImageCacheHocProps> & Readonly<{ children?: React.ReactNode; }>; state: Readonly<ReactNativeImageCacheHocState>; refs: { [key: string]: React.ReactInstance; }; shouldComponentUpdate?(nextProps: Readonly<P & ReactNativeImageCacheHocProps>, nextState: Readonly<ReactNativeImageCacheHocState>, nextContext: any): boolean; componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void; getSnapshotBeforeUpdate?(prevProps: Readonly<P & ReactNativeImageCacheHocProps>, prevState: Readonly<ReactNativeImageCacheHocState>): any; componentWillMount?(): void; UNSAFE_componentWillMount?(): void; componentWillReceiveProps?(nextProps: Readonly<P & ReactNativeImageCacheHocProps>, nextContext: any): void; UNSAFE_componentWillReceiveProps?(nextProps: Readonly<P & ReactNativeImageCacheHocProps>, nextContext: any): void; componentWillUpdate?(nextProps: Readonly<P & ReactNativeImageCacheHocProps>, nextState: Readonly<ReactNativeImageCacheHocState>, nextContext: any): void; UNSAFE_componentWillUpdate?(nextProps: Readonly<P & ReactNativeImageCacheHocProps>, nextState: Readonly<ReactNativeImageCacheHocState>, nextContext: any): void; }; /** * * Manually cache a file. * Can be used to pre-warm caches. * If calling this method repeatedly to cache a long list of files, * be sure to use a queue and limit concurrency so your app performance does not suffer. * * @param url {String} - url of file to download. * @returns {Promise} promise that resolves to an object that contains cached file info. */ cacheFile(url: string): Promise<any>; /** * * Manually move or copy a local file to the cache. * Can be used to pre-warm caches. * If calling this method repeatedly to cache a long list of files, * be sure to use a queue and limit concurrency so your app performance does not suffer. * * @param local {String} - path to the local file. * @param url {String} - url of file to download. * @param move {Boolean} - whether the file should be copied or moved. * @param mtime {Date} - creation timestamp * @param ctime {Date} - modification timestamp (iOS only) * @returns {Promise} promise that resolves to an object that contains cached file info. */ cacheLocalFile(local: string, url: string, move?: boolean, mtime?: Date | undefined, ctime?: Date | undefined): Promise<{ url: string; path: null; } | { url: string; path: string; }>; /** * * Delete all locally stored image files created by react-native-image-cache-hoc. * Calling this method will cause a performance hit on your app until the local files are rebuilt. * * @returns {Promise} promise that resolves to an object that contains the flush results. */ flush(): Promise<boolean>; /** * Export FileSystem for convenience. * * @returns {FileSystem} */ fileSystem(): FileSystem; contextType?: React.Context<any> | undefined; }; export { imageCacheHoc, FileSystem, FileSystemFactory }; //# sourceMappingURL=index.d.ts.map