Skip to content
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

[MVP] Toggle suspense #36

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
33 changes: 33 additions & 0 deletions shells/dev/app/SuspenseTree/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @flow

import React, { Suspense } from 'react';

function SuspenseTree() {
return (
<>
<h1>Suspense</h1>
<Suspense fallback={<h2>Loading outer</h2>}>
<Parent />
</Suspense>
</>
);
}

function Parent() {
return (
<div>
<Suspense fallback={<h3>Loading inner 1</h3>}>
<Child>Hello</Child>
</Suspense>
<Suspense fallback={<h3>Loading inner 2</h3>}>
<Child>World</Child>
</Suspense>
</div>
);
}

function Child({ children }) {
return <p>{children}</p>;
}

export default SuspenseTree;
2 changes: 2 additions & 0 deletions shells/dev/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ElementTypes from './ElementTypes';
import InspectableElements from './InspectableElements';
import InteractionTracing from './InteractionTracing';
import ToDoList from './ToDoList';
import SuspenseTree from './SuspenseTree';

import './styles.css';

Expand All @@ -32,6 +33,7 @@ function mountTestApp() {
mountHelper(ElementTypes);
mountHelper(EditableProps);
mountHelper(DeeplyNestedComponents);
mountHelper(SuspenseTree);
}

function unmountTestApp() {
Expand Down
10 changes: 10 additions & 0 deletions src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default class Agent extends EventEmitter {
bridge.addListener('startProfiling', this.startProfiling);
bridge.addListener('stopInspectingDOM', this.stopInspectingDOM);
bridge.addListener('stopProfiling', this.stopProfiling);
bridge.addListener('toggleSuspense', this.toggleSuspense);
bridge.addListener('shutdown', this.shutdown);
bridge.addListener('viewElementSource', this.viewElementSource);

Expand Down Expand Up @@ -307,6 +308,15 @@ export default class Agent extends EventEmitter {
this._bridge.send('profilingStatus', this._isProfiling);
};

toggleSuspense = ({ id, rendererID }: InspectSelectParams) => {
const renderer = this._rendererInterfaces[rendererID];
if (renderer == null) {
console.warn(`Invalid renderer id "${rendererID}" for element "${id}"`);
} else {
renderer.toggleSuspense(id);
}
};

viewElementSource = ({ id, rendererID }: InspectSelectParams) => {
const renderer = this._rendererInterfaces[rendererID];
if (renderer == null) {
Expand Down
33 changes: 33 additions & 0 deletions src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,12 @@ export function attach(
// Does the current renderer support editable function props?
canEditFunctionProps: typeof overrideProps === 'function',

// Is this Suspense, and can we toggle it?
// TODO: this will incorrectly enable it on old versions.
canToggleSuspense:
typeof overrideProps === 'function' &&
tag === ReactTypeOfWork.SuspenseComponent,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice TODO. This should probably look for a new injected flag that signals whether the current version and build supports the override behavior.


// Can view component source location.
canViewSource,

Expand Down Expand Up @@ -1554,6 +1560,31 @@ export function attach(
isProfiling = false;
}

let suspendedIds = new Set();

function shouldSuspendFiber(fiber) {
if (suspendedIds.size === 0) {
return false;
}
// TODO: maybe optimize this.
const id = getFiberID(getPrimaryFiber(((fiber: any): Fiber)));
return suspendedIds.has(id);
}

function toggleSuspense(id) {
if (typeof overrideProps !== 'function') {
return;
}
if (suspendedIds.has(id)) {
suspendedIds.delete(id);
} else {
suspendedIds.add(id);
}
// Force a re-render.
const fiber = idToFiberMap.get(id);
overrideProps(fiber, [], null);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious: why not inject a method like e.g. suspendFiber(true) that could be used as our signal for support as well as our method for re-rendering?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to preserve our ability to do filtering based on something other than just putting it in a Set.

}

return {
cleanup,
getCommitDetails,
Expand All @@ -1571,6 +1602,8 @@ export function attach(
setInHook,
setInProps,
setInState,
shouldSuspendFiber,
toggleSuspense,
startProfiling,
stopProfiling,
walkTree,
Expand Down
2 changes: 2 additions & 0 deletions src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export type RendererInterface = {
) => void,
setInProps: (id: number, path: Array<string | number>, value: any) => void,
setInState: (id: number, path: Array<string | number>, value: any) => void,
toggleSuspense: (id: number) => void,
shouldSuspendFiber: (fiber: Object) => boolean,
startProfiling: () => void,
stopProfiling: () => void,
walkTree: () => void,
Expand Down
3 changes: 3 additions & 0 deletions src/devtools/views/ButtonIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type IconType =
| 'record'
| 'reload'
| 'search'
| 'toggle-suspense'
| 'undo'
| 'up'
| 'view-dom'
Expand Down Expand Up @@ -60,6 +61,8 @@ export default function ButtonIcon({ type }: Props) {
case 'search':
pathData = PATH_SEARCH;
break;
case 'toggle-suspense':
return '🌀'; // TODO
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 You love this emoji

case 'undo':
pathData = PATH_UNDO;
break;
Expand Down
30 changes: 29 additions & 1 deletion src/devtools/views/Elements/SelectedElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import HooksTree from './HooksTree';
import InspectedElementTree from './InspectedElementTree';
import { hydrate } from 'src/hydration';
import styles from './SelectedElement.css';
import { ElementTypeClass, ElementTypeFunction } from '../../types';
import {
ElementTypeClass,
ElementTypeFunction,
ElementTypeSuspense,
} from '../../types';

import type { InspectedElement } from './types';
import type { DehydratedData, Element } from './types';
Expand Down Expand Up @@ -52,6 +56,17 @@ export default function SelectedElement(_: Props) {
}
}, [selectedElementID, viewElementSource]);

const toggleSuspense = useCallback(() => {
if (element === null) {
return;
}
const rendererID = store.getRendererIDForElement(element.id);
bridge.send('toggleSuspense', {
id: element.id,
rendererID,
});
}, [element]);

if (element === null) {
return (
<div className={styles.SelectedElement}>
Expand All @@ -65,6 +80,9 @@ export default function SelectedElement(_: Props) {
inspectedElement.canViewSource &&
viewElementSource !== null;

const canToggleSuspense =
inspectedElement && inspectedElement.canToggleSuspense;

return (
<div className={styles.SelectedElement}>
<div className={styles.TitleRow}>
Expand All @@ -74,6 +92,16 @@ export default function SelectedElement(_: Props) {
</div>
</div>

{canToggleSuspense && (
<Button
className={styles.IconButton}
onClick={toggleSuspense}
title="Toggle Suspense loading state"
>
<ButtonIcon type="toggle-suspense" />
</Button>
)}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wherever we end up placing this toggle, we should also:

  • Show it as a toggle (with a clear on/off state).
  • Only show it if the Suspense boundary has actually resolved. (Updates that trigger new suspends might be a little tricky, but maybe we can just ignore that.)


<Button
className={styles.IconButton}
onClick={highlightElement}
Expand Down
3 changes: 3 additions & 0 deletions src/devtools/views/Elements/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export type InspectedElement = {|
// Can view component source location.
canViewSource: boolean,

// Is this a Suspense, and can we toggle it?
canToggleSuspense: boolean,

// Inspectable properties.
context: Object | null,
hooks: Object | null,
Expand Down
10 changes: 10 additions & 0 deletions src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ export function installHook(target: any): DevToolsHook | null {
}
}

function shouldSuspendFiber(rendererID, fiber) {
const rendererInterface = rendererInterfaces.get(rendererID);
if (rendererInterface != null) {
// TODO: does this need a check?
return rendererInterface.shouldSuspendFiber(fiber);
}
return false;
}

// TODO: More meaningful names for "rendererInterfaces" and "renderers".
const fiberRoots = {};
const rendererInterfaces = new Map();
Expand All @@ -175,6 +184,7 @@ export function installHook(target: any): DevToolsHook | null {
checkDCE,
onCommitFiberUnmount,
onCommitFiberRoot,
shouldSuspendFiber,
};

Object.defineProperty(
Expand Down