Skip to content

Commit

Permalink
refactor(core/presentation): migrate useData and useLatestPromise to …
Browse files Browse the repository at this point in the history
…use PromiseLike, not IPromise (#8279)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
christopherthielen and mergify[bot] committed May 15, 2020
1 parent ede5b90 commit d755966
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IPromise } from 'angular';
import { isNil } from 'lodash';
import { useRef } from 'react';

Expand All @@ -19,7 +18,7 @@ import { useLatestPromise, IUseLatestPromiseResult } from './useLatestPromise.ho
* @param deps array of dependencies, which (when changed) cause the callback to be invoked again
* @returns an object with the result and current status of the promise
*/
export function useData<T>(callback: () => IPromise<T>, defaultResult: T, deps: any[]): IUseLatestPromiseResult<T> {
export function useData<T>(callback: () => PromiseLike<T>, defaultResult: T, deps: any[]): IUseLatestPromiseResult<T> {
const anyDepsMissing = deps.some(dep => isNil(dep));
const result = useLatestPromise<T>(anyDepsMissing ? () => null : callback, deps);
const hasResolvedAtLeastOnceRef = useRef(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IPromise } from 'angular';
import { DependencyList, useEffect, useRef, useState } from 'react';
import { useIsMountedRef } from './useIsMountedRef.hook';

Expand Down Expand Up @@ -38,15 +37,15 @@ const initialPromiseState: IPromiseState<any> = {
* fetch.status === 'REJECTED' ? <span>Error: {fetch.error}</span> :
* fetch.status === 'PENDING' ? <span>Loading...</span> : null);
*
* @param callback a callback that returns an IPromise
* @param callback a callback that returns a PromiseLike
* @param deps array of dependencies, which (when changed) cause the callback to be invoked again
* @returns an object with the result and current status of the promise
*/
export function useLatestPromise<T>(callback: () => IPromise<T>, deps: DependencyList): IUseLatestPromiseResult<T> {
export function useLatestPromise<T>(callback: () => PromiseLike<T>, deps: DependencyList): IUseLatestPromiseResult<T> {
const isMountedRef = useIsMountedRef();
// Capture the isMountedRef.current value before effects run
const isInitialRender = !isMountedRef.current;
const requestInFlight = useRef<IPromise<T>>();
const requestInFlight = useRef<PromiseLike<T>>();
// A counter that is used to trigger the promise handling useEffect
const [requestIdTrigger, setRequestIdTrigger] = useState(0);
const [promiseState, setPromiseState] = useState<IPromiseState<T>>(initialPromiseState);
Expand Down

0 comments on commit d755966

Please sign in to comment.