Skip to content

DevTools: Add support for use(Promise) #28277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ module.exports = {
__IS_EDGE__: 'readonly',
},
},
{
files: ['packages/react-devtools-shell/**/*.js'],
rules: {
// DevTools shell is an internal app not published to NPM
'react-internal/prod-error-codes': 'off',
},
},
],

env: {
Expand Down
28 changes: 24 additions & 4 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ReactContext,
ReactProviderType,
StartTransitionOptions,
Thenable,
Usable,
} from 'shared/ReactTypes';
import type {
Expand Down Expand Up @@ -122,14 +123,33 @@ function readContext<T>(context: ReactContext<T>): T {
return context._currentValue;
}

function useThenable<T>(thenable: Thenable<T>): T {
switch (thenable.status) {
case 'fulfilled':
return thenable.value;
case 'rejected':
throw thenable.reason;
case 'pending':
default:
throw thenable;
}
}

function use<T>(usable: Usable<T>): T {
if (usable !== null && typeof usable === 'object') {
// $FlowFixMe[method-unbinding]
if (typeof usable.then === 'function') {
// TODO: What should this do if it receives an unresolved promise?
throw new Error(
'Support for `use(Promise)` not yet implemented in react-debug-tools.',
);
// This is a thenable.
const thenable: Thenable<T> = (usable: any);
const value = useThenable(thenable);

hookLog.push({
primitive: 'Use',
stackError: new Error(),
value,
});

return value;
} else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
const context: ReactContext<T> = (usable: any);
const value = readContext(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import {
forwardRef,
Fragment,
memo,
Suspense,
useCallback,
useContext,
useDebugValue,
useEffect,
useOptimistic,
useState,
use,
useReducer,
} from 'react';
import {useFormState} from 'react-dom';

Expand Down Expand Up @@ -132,6 +134,55 @@ function Forms() {
);
}

let promise = Promise.resolve('initial');
function FunctionWithUse() {
// $FlowFixMe[underconstrained-implicit-instantiation]
const [, rerender] = useReducer((n: number) => n + 1, 0);
const value = use(promise);

return (
<Fragment>
{value}
<form
onSubmit={event => {
event.preventDefault();
const delay = +event.target.elements.delay.value;
const shouldReject = event.target.elements.shouldReject.checked;
const settledValue = event.target.elements.settledValue.value;
promise = new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldReject) {
reject(new Error(settledValue));
} else {
resolve(settledValue);
}
}, delay);
});
rerender();
}}>
<label>
delay
<input
name="delay"
type="text"
inputMode="numeric"
defaultValue={1000}
/>
</label>
<label>
settled value
<input name="settledValue" type="text" />
</label>
<label>
reject
<input name="shouldReject" type="checkbox" defaultChecked={false} />
</label>
<button type="submit">Suspend</button>
</form>
</Fragment>
);
}

export default function CustomHooks(): React.Node {
return (
<Fragment>
Expand All @@ -140,6 +191,9 @@ export default function CustomHooks(): React.Node {
<ForwardRefWithHooks />
<HocWithHooks />
<Forms />
<Suspense fallback="loading">
<FunctionWithUse />
</Suspense>
</Fragment>
);
}
Expand Down