Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use
React.use
where available (#10758)
* use `React.use` where available * bumps bundlesize to 34.83KB (was 34.82KB) --------- Co-authored-by: Lenz Weber-Tronic <lenz@apollographql.com> Co-authored-by: Alessia Bellisario <alessia@apollographql.com>
- Loading branch information
1 parent
aaec09c
commit 9def742
Showing
3 changed files
with
28 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@apollo/client': patch | ||
--- | ||
|
||
use `React.use` where available |
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,19 +1,26 @@ | ||
import { wrapPromiseWithState } from '../../../utilities'; | ||
import React from 'react'; | ||
|
||
// TODO: Replace the use of this with React's use once its available. For now, | ||
// this mimics its functionality for promises by adding | ||
// properties to the promise instance and reading them synchronously. This is | ||
// named with two underscores to allow this hook to evade typical rules of | ||
type Use = <T>(promise: Promise<T>) => T; | ||
// Prevent webpack from complaining about our feature detection of the | ||
// use property of the React namespace, which is expected not | ||
// to exist when using current stable versions, and that's fine. | ||
const useKey = 'use' as keyof typeof React; | ||
const realHook = React[useKey] as Use | undefined; | ||
|
||
// This is named with two underscores to allow this hook to evade typical rules of | ||
// hooks (i.e. it can be used conditionally) | ||
export function __use<TValue>(promise: Promise<TValue>) { | ||
const statefulPromise = wrapPromiseWithState(promise); | ||
export const __use = | ||
realHook || | ||
function __use<TValue>(promise: Promise<TValue>) { | ||
const statefulPromise = wrapPromiseWithState(promise); | ||
|
||
switch (statefulPromise.status) { | ||
case 'pending': | ||
throw statefulPromise; | ||
case 'rejected': | ||
throw statefulPromise.reason; | ||
case 'fulfilled': | ||
return statefulPromise.value; | ||
} | ||
} | ||
switch (statefulPromise.status) { | ||
case 'pending': | ||
throw statefulPromise; | ||
case 'rejected': | ||
throw statefulPromise.reason; | ||
case 'fulfilled': | ||
return statefulPromise.value; | ||
} | ||
}; |