-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Elements component readability
- Initialize context on mount with given stripe value - Make `usePromiseResolver` more generic purpose - Get rid of props destructuring
- Loading branch information
Showing
4 changed files
with
87 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,51 @@ | ||
import React from 'react'; | ||
import {isPromise} from '../utils/guards'; | ||
|
||
export const usePromiseResolver = <T>(mayBePromise: T | PromiseLike<T>): T | null => { | ||
const [resolved, setResolved] = React.useState(() => { | ||
return isPromise(mayBePromise) ? null : mayBePromise | ||
}) | ||
type PromisePending = [undefined, undefined, 'pending']; | ||
type PromiseResolved<T> = [T, undefined, 'resolved']; | ||
type PromiseRejected = [undefined, any, 'rejected']; | ||
type PromiseState<T> = PromisePending | PromiseResolved<T> | PromiseRejected; | ||
|
||
const createPending = (): PromisePending => [undefined, undefined, 'pending']; | ||
|
||
const createResolved = <T>(value: T): PromiseResolved<T> => [ | ||
value, | ||
undefined, | ||
'resolved', | ||
]; | ||
|
||
const createRejected = (reason: any): PromiseRejected => [ | ||
undefined, | ||
reason, | ||
'rejected', | ||
]; | ||
|
||
export const usePromiseResolver = <T>( | ||
mayBePromise: T | PromiseLike<T> | ||
): PromiseState<T> => { | ||
const [state, setState] = React.useState<PromiseState<T>>(() => | ||
isPromise(mayBePromise) ? createPending() : createResolved(mayBePromise) | ||
); | ||
|
||
React.useEffect(() => { | ||
if (!isPromise(mayBePromise)) return setResolved(mayBePromise) | ||
if (!isPromise(mayBePromise)) return setState(createResolved(mayBePromise)); | ||
|
||
let isMounted = true | ||
let isMounted = true; | ||
|
||
setResolved(null) | ||
mayBePromise.then(resolvedValue => { | ||
if (isMounted) setResolved(resolvedValue) | ||
}, () => undefined) | ||
setState(createPending()); | ||
mayBePromise | ||
.then( | ||
(resolved) => createResolved(resolved), | ||
(error) => createRejected(error) | ||
) | ||
.then((nextState) => { | ||
if (isMounted) setState(nextState); | ||
}); | ||
|
||
return () => { | ||
isMounted = false | ||
} | ||
|
||
}, [mayBePromise]) | ||
isMounted = false; | ||
}; | ||
}, [mayBePromise]); | ||
|
||
return resolved | ||
return state; | ||
}; |