Commit Graph

14926 Commits

Author SHA1 Message Date
Luna Ruan d63cd97245 don't stringify objects for console log second render (#24373)
Fixes #24302 based on #24306.
---

The current implementation for strict mode double logging stringiness and dims the second log. However, because we stringify everything, including objects, this causes objects to be logged as `[object Object]` etc.

This PR creates a new function that formats console log arguments with a specified style. It does this by:
1. The first param is a string that contains %c: Bail out and return the args without modifying the styles. We don't want to affect styles that the developer deliberately set.
2. The first param is a string that doesn't contain %c but contains string formatting: `[`%c${args[0]}`, style, ...args.slice(1)]` Note: we assume that the string formatting that the developer uses is correct.
3. The first param is a string that doesn't contain string formatting OR is not a string: Create a formatting string where:
   -  boolean, string, symbol -> %s
   -  number -> %f OR %i depending on if it's an int or float
   -  default -> %o
---
Co-authored-by: Billy Janitsch <billy@kensho.com>
2022-04-14 11:30:04 -05:00
Luna Ruan ddb1ab1e97 Rename react-dom/testing to react-dom/unstable_testing in yarn build-for-devtools (#24364)
The shell package wasn't compiling because yarn build-for-devtools was incorrect. The react-dom/test package was renamed to react-dom/unstable_testing. This PR fixes this in the package.json.

Note: Adding packages to the yarn build-for-devtools command isn't great in the long run. Eventually we should make devtools have its own build script.
2022-04-13 11:07:26 -07:00
Andrew Clark 168da8d557 Fix typo that happened during rebasing
I changed the type of this functions returned value but forgot to change
the check.

It happens to work before anyway, because eventually the interleaved
updates will get transferred at the beginning of the next render phase.
But this is more logically consistent.
2022-04-12 15:43:51 -04:00
Andrew Clark 8bc527a4cf Bugfix: Fix race condition between interleaved and non-interleaved updates (#24353)
* Regression test: Interleaved update race condition

Demonstrates the bug reported in #24350.

* Bugfix: Last update wins, even if interleaved

"Interleaved" updates are updates that are scheduled while a render is
already in progress. We put these on a special queue so that they don't
get processed during the current render. Then we transfer them to
the "real" queue after the render has finished.

There was a race condition where an update is received after the render
has finished but before the interleaved update queue had been
transferred, causing the updates to be queued in the wrong order.

The fix I chose is to check if the interleaved updates queue is empty
before adding any update to the real queue. If it's not empty, then
the new update must also be treated as interleaved.
2022-04-12 15:39:11 -04:00
Luna Ruan f7cf077cca [Transition Tracing] Add Offscreen Queue (#24341)
Adds an Offscreen Queue. We use the offscreen queue to store not yet processed transitions. During the commit phase, we will add these transitions to the transitions field in memoizedState (in the subsequent PR) and clear the transitions field in the updateQueue
2022-04-12 08:42:08 -07:00
sunderls 4fc394bbec Fix suspense fallback throttling (#24253)
* fix suspense throttling

* fix lint

* Tweak tests + another test

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
2022-04-12 10:42:05 -04:00
Sebastian Markbåge 80170a0681 Match bundle.name and match upper case entry points (#24346)
Fix matching in the build script.

It's possible to provide a custom bundle name in the case we build deep
imports. We should match those names as a convenience.

The script also calls toLowerCase on requested names but some entries have
upper case now.
2022-04-11 21:01:48 -04:00
Luna Ruan fea6f8da6a [Transition Tracing] Add transition to OffscreenState and pendingSuspenseBoundaries to RootState (#24340)
In this PR we:

Add transitions boilerplate to the OffscreenState. The transitions field will be null on initiation. During the commit phase, if there are any new transitions, we will add any new transitions (either as a result of a transition occurring or a parent suspense boundary completing) to the transitions field. Once the suspense boundary resolves, we no longer need to store the transitions on the boundary, so we can put this field on the Offscreen memoized state
Add pendingSuspenseBoundaries boilerplate to the RootState. This field starts as null. During the commit phase, if a suspense boundary has either gone from fallback to resolved or from resolved to fallback, we will create a new Map if there isn't one, and if there is, we will add (if the boundary is a fallback) or remove the suspense boundary (if the boundary has resolved) from the map.
Add an optional name field to the Suspense boundary
2022-04-11 14:56:20 -07:00
Luna Ruan 8e2f9b086e move passive flag (#24339)
Previously, we were only adding the passive flag when we add the Visibility flag, which is only set when we go from primary to fallback. Now, we add the passive flag BOTH when we go from primary to fallback and from fallback to primary.

An alternate solution is to add the passive flag in the same place as the visibility flag in the offscreen complete phase (rather than the suspense complete phase), but this feature is currently only for suspense, and offscreen can be used in different ways, so for now we add the passive flag only in the suspense component's complete phase. We might want to revisit this later when we think about how offscreen should work with transition tracing.
2022-04-11 14:54:57 -07:00
Luna Ruan 55a21ef7e7 fix pushTransition for transition tracing (#24338)
We forgot to move pushTransition out from the enableCache flag in #24321 in a place that both transition tracing and cache need to push transitions. Move it out from behind the enableCache to prepare for the next PRs.
2022-04-11 14:54:25 -07:00
Afzal Sayed 069d23bb74 [eslint-plugin-exhaustive-deps] Fix exhaustive deps check for unstable vars (#24343)
* Fix exhaustive deps for unstable vars

* Fix formatting

* Optimise iterations

* Fix linting
2022-04-11 21:43:16 +01:00
dan 4997515b96 Point useSubscription to useSyncExternalStore shim (#24289)
* Point useSubscription to useSyncExternalStore shim

* Update tests

* Update README

* Ad hoc case
2022-04-11 21:15:13 +01:00
Dan Abramov df5d32f230 Remove create-subscription from the list 2022-04-11 20:12:59 +01:00
zhoulixiang 01e2bff1dc Remove unnecessary check (#24332) 2022-04-11 20:08:29 +01:00
dan d9a0f9e203 Delete create-subscription folder (#24288) 2022-04-11 20:07:22 +01:00
Andrew Clark f993ffc514 Fix infinite update loop that happens when an unmemoized value is passed to useDeferredValue (#24247)
* Fix infinite loop if unmemoized val passed to uDV

The current implementation of useDeferredValue will spawn a new
render any time the input value is different from the previous one. So
if you pass an unmemoized value (like an inline object), it will never
stop spawning new renders.

The fix is to only defer during an urgent render. If we're already
inside a transition, retry, offscreen, or other non-urgen render, then
we can use the latest value.

* Temporarily disable "long nested update" warning

DevTools' timeline profiler warns if an update inside a layout effect
results in an expensive re-render. However, it misattributes renders
that are spawned from a sync render at lower priority. This affects the
new implementation of useDeferredValue but it would also apply to things
like Offscreen.

It's not obvious to me how to fix this given how DevTools models the
idea of a "nested update" so I'm disabling the warning for now to
unblock the bugfix for useDeferredValue.
2022-04-11 12:34:03 -04:00
Josh Story fa58002262 [Fizz] Pipeable Stream Perf (#24291)
* Add fixture for comparing baseline render perf for renderToString and renderToPipeableStream

Modified from ssr2 and https://github.com/SuperOleg39/react-ssr-perf-test

* Implement buffering in pipeable streams

The previous implementation of pipeable streaming (Node) suffered some performance issues brought about by the high chunk counts and innefficiencies with how node streams handle this situation. In particular the use of cork/uncork was meant to alleviate this but these methods do not do anything unless the receiving Writable Stream implements _writev which many won't.

This change adopts the view based buffering techniques previously implemented for the Browser execution context. The main difference is the use of backpressure provided by the writable stream which is not implementable in the other context. Another change to note is the use of standards constructs like TextEncoder and TypedArrays.

* Implement encodeInto during flushCompletedQueues

encodeInto allows us to write directly to the view buffer that will end up getting streamed instead of encoding into an intermediate buffer and then copying that data.
2022-04-11 09:13:44 -07:00
Leo 0568c0f8cd Replace zero with NoLanes for consistency in FiberLane (#24327) 2022-04-09 20:29:08 +01:00
Luna Ruan e0160d50c5 add transition tracing transitions stack (#24321)
Added a transitions stack for to keep track of which transitions are still happening for the current boundary.
* On the root, we will get all transitions that have been initiated for the corresponding lanes.
* Whenever we encounter a suspended boundary, we will add all transitions on the stack onto the boundary
* Whenever we encounter a boundary that just unsuspended, we will add all transitions on the boundary onto the stack
A transition will be considered complete when there are no boundaries that have the associated transition
2022-04-08 17:48:30 -07:00
Luna Ruan b0f13e5d39 add pendingPassiveTransitions (#24320)
Add pendingPassiveTransitions work loop module level variable. Because workInProgressTransitions might change before we process it in the passive effects, we introduce a new variable, pendingPassiveTransitions, where we store the transitions until we can actually process them in the commit phase.
2022-04-08 16:35:18 -07:00
Luna Ruan 60e63b960f remove console.error in ReactFiberLane (#24319)
We changed the implementation of root.transitionLanes so that, if there is no transitions for a given lane, we use null instead of an array. This means that this error is no longer valid, so we are removing it
2022-04-08 15:33:52 -07:00
Andrew Clark ec52a5698e Fix: Don't call cWU if already unmounted
When a tree goes offscreen, we unmount all the effects just like we
would in a normal deletion. (Conceptually it _is_ a deletion; we keep
the fiber around so we can reuse its state if the tree mounts again.)

If an offscreen component gets deleted "for real", we shouldn't unmount
it again.

The fix is to track on the stack whether we're inside a hidden tree.

We already had a stack variable for this purpose, called
`offscreenSubtreeWasHidden`, in another part of the commit phase, so I
reused that variable instead of creating a new one. (The name is a bit
confusing: "was" refers to the current tree before this commit. So, the
"previous current".)

Co-authored-by: dan <dan.abramov@me.com>
2022-04-08 18:03:15 -04:00
Andrew Clark 46db4e996d Combine deletion phase into single recursive function
Similar to the previous step, this converts the deletion phase into
a single recursive function. Although there's less code, this one is
a bit trickier because it's already contains some stack-like logic
for tracking the nearest host parent. But instead of using the actual
stack, it repeatedly searches up the fiber return path to find the
nearest host parent.

Instead, I've changed it to track the nearest host parent on the
JS stack.

(We still search up the return path once, to set the initial host parent
right before entering a deleted tree. As a follow up, we can instead
push this to the stack as we traverse during the main mutation phase.)
2022-04-08 18:01:39 -04:00
Andrew Clark 481dece580 Use recursion to traverse during mutation phase
Most of the commit phase uses iterative loops to traverse the tree.
Originally we thought this would be faster than using recursion, but
a while back @trueadm did some performance testing and found that the
loop was slower because we assign to the `return` pointer before
entering a subtree (which we have to do because the `return` pointer
is not always consistent; it could point to one of two fibers).

The other motivation is so we can take advantage of the JS stack to
track contextual information, like the nearest host parent.

We already use recursion in a few places; this changes the mutation
phase to use it, too.
2022-04-08 18:01:21 -04:00
Andrew Clark f9e6aef828 Wrap try-catch directly around each user function
This moves the try-catch from around each fiber's mutation phase to
direclty around each user function (effect function, callback, etc).

We already do this when unmounting because if one unmount function
errors, we still need to call all the others so they can clean up
their resources.

Previously we didn't bother to do this for anything but unmount,
because if a mount effect throws, we're going to delete that whole
tree anyway.

But now that we're switching from an iterative loop to a recursive one,
we don't want every call frame on the stack to have a try-catch, since
the error handling requires additional memory.

Wrapping every user function is a bit tedious, but it's better
for performance. Many of them already had try blocks around
them already.
2022-04-08 18:00:42 -04:00
Andrew Clark bcc1b3121e Move reportUncaughtErrorInDev to captureCommitPhaseError
reportUncaughtErrorInDev is always followed by captureCommitPhaseError,
so we can move it into that function.
2022-04-08 18:00:33 -04:00
Andrew Clark c99c5f1df6 Move ad hoc flag checks into main switch statement
We should always refine the type of fiber before checking the effect
flag, because the fiber tag is more specific.

Now we have a single switch statement for all mutation effects.
2022-04-08 18:00:13 -04:00
Andrew Clark 54b5b32d53 Move Update flag check into each switch case
The fiber tag is more specific than the effect flag, so we should always
refine the type of work first, to minimize redundant checks.

In the next step I'll move all other other flag checks in this function
into the same switch statement.
2022-04-08 17:59:54 -04:00
Andrew Clark e66e7a0fb8 Inline commitWork into commitMutationOnFiber
There's not really any reason these should be separate functions. The
factoring has gotten sloppy and redundant because there's similar logic
in both places, which is more obvious now that they're combined.

Next I'll start combining the redundant branches.
2022-04-08 17:59:32 -04:00
Andrew Clark 12d7a9ad70 Combine commitWork into single switch statement
commitWork is forked into a separate implementation for mutation mode
(DOM) and persistent mode (React Native). But unlike when it was first
introduced, there's more overlap than differences between the forks,
mainly because we've added new types of fibers. So this joins the two
forks and adds more local branches where the behavior actually
diverges: host nodes, host containers, and portals.
2022-04-08 17:59:14 -04:00
Andrew Clark ea7b2ec289 Remove wrong return pointer warning
I'm about to refactor part of the commit phase to use recursion instead
of iteration. As part of that change, we will no longer assign the
`return` pointer when traversing into a subtree. So I'm disabling
the internal warning that fires if the return pointer is not consistent
with the parent during the commit phase.

I had originally added this warning to help prevent mistakes when
traversing the tree iteratively, but since we're intentionally switching
to recursion instead, we don't need it.
2022-04-08 17:58:52 -04:00
Ricky 8dcedba15a Add fallback shim for AbortController (#24285)
* Add fallback shim for AbortController

* Replace shim with a minimal stub

* replace-fork

* Better minification

* Fix flow

* Even smaller

* replace-fork

* Revert back to object constructor

* replace-fork
2022-04-08 15:53:40 -04:00
Ricky b86baa1cb7 Add back lost cache test (#24317) 2022-04-08 15:34:41 -04:00
Zhongjan a9add2fe08 Fix file path separator compatibility in scripts/babel (#24318)
The problem in scripts\babel\transform-object-assign.js is that file path separator has '/' and '\' between Linux, MacOS and Windows, which causes yarn build error. See https://github.com/facebook/react/issues/24103
2022-04-08 19:47:31 +01:00
Leo bafe912a5f update types for InputContinuousLane and DefaultLane (#24316) 2022-04-08 18:51:40 +01:00
Luna Ruan 4ebaeae40d moved mutation code to passive (#24251)
This PR moves the code for transition tracing in the mutation phase that adds transitions to the pending callbacks object (to be called sometime later after paint) from the mutation to the passive phase.

Things to think about:

Passive effects can be flushed before or after paint. How do we make sure that we get the correct end time for the interaction?
2022-04-08 09:28:20 -07:00
Brian Vaughn 5b2e7253f9 React DevTools 4.24.3 -> 4.24.4 (#24315) 2022-04-08 11:38:07 -04:00
Leo caa60e8fcc update types for NonIdleLanes and IdleLane (#24313) 2022-04-08 11:01:18 -04:00
Brian Vaughn 65f35035a6 Allow react-devtools-inline createStore() method to override Store config params (#24303) 2022-04-08 08:59:12 -04:00
Stephen Cyron 1f7a901d7b Fix false positive lint error with large number of branches (#24287)
* Switched RulesOfHooks.js to use BigInt. Added test and updated .eslintrc.js to use es2020.

* Added BigInt as readonly global in eslintrc.cjs.js and eslintrc.cjs2015.js

* Added comment to RulesOfHooks.js that gets rid of BigInt eslint error

* Got rid of changes in .eslintrc.js and yarn.lock

* Move global down

Co-authored-by: stephen cyron <stephen.cyron@fdmgroup.com>
Co-authored-by: dan <dan.abramov@gmail.com>
2022-04-08 00:22:47 +01:00
dan f56dfe950b Warn on setState() in useInsertionEffect() (#24298)
* Warn on setState() in useInsertionEffect()

* Use existing DEV reset mechanism
2022-04-07 20:12:49 +01:00
Sebastian Silbermann 548b542b41 Update renderToPipeableStream#options.onShellError to match usage (#24299) 2022-04-07 20:06:54 +01:00
dan d68b09defc Fix warning about setState in useEffect (#24295)
* Fix warning about setState in useEffect

* Fix test

* Fix multiple roots
2022-04-07 18:06:35 +01:00
dan 0579154772 Update create-subscription README (#24294) 2022-04-07 16:41:38 +01:00
dan e8f4a6653d Fix import in example 2022-04-07 14:12:58 +01:00
dan 3e8c91c5f8 Fix import in README 2022-04-07 13:46:36 +01:00
dan bb49abea23 Update some READMEs (#24290)
* Update some READMEs

* Update README.md
2022-04-07 02:35:01 +01:00
Sebastian Markbåge 4bc465a16f Rename Controls to PipeableStream (#24286)
This type isn't exported so it's technically not public.

This object mimics a ReadableStream.

Currently this is safe to destructure and call separately but I'm not sure
that's even guaranteed. It should probably be treated as a class in docs.
2022-04-06 19:27:38 -04:00
zhoulixiang ece5295e5a Remove unnecessary flag check (#24284) 2022-04-06 17:42:53 +01:00
Sebastian Silbermann af730436c0 test: Update attribute fixture snapshot (#24083)
* test: Update attribute fixture snapshot

* Poke CircleCI

* Poke CircleCI
2022-04-05 02:59:26 +01:00