Commit Graph

40 Commits

Author SHA1 Message Date
Ming Ye 71cace4d32
Migrate testRunner from jasmine2 to jest-circus (#26144)
## Summary

In jest v27, jest-circus as default test runner
(https://github.com/facebook/jest/pull/10686)

## How did you test this change?

ci green
2023-02-10 13:39:14 -05:00
Jan Kassens 6b30832666
Upgrade prettier (#26081)
The old version of prettier we were using didn't support the Flow syntax
to access properties in a type using `SomeType['prop']`. This updates
`prettier` and `rollup-plugin-prettier` to the latest versions.

I added the prettier config `arrowParens: "avoid"` to reduce the diff
size as the default has changed in Prettier 2.0. The largest amount of
changes comes from function expressions now having a space. This doesn't
have an option to preserve the old behavior, so we have to update this.
2023-01-31 08:25:05 -05:00
Sebastian Markbåge 7b17f7bbf3
Enable warning for defaultProps on function components for everyone (#25699)
This also fixes a gap where were weren't warning on memo components.
2022-11-17 12:22:23 -05:00
Andrew Clark a8c9cb18b7
Land enableSuspenseLayoutEffectSemantics flag (#24713)
This was released to open source in 18.0 and is already hardcoded to
true in www.
2022-06-13 11:27:40 -04:00
Andrew Clark 1e748b4528
Land enableLazyElements flag (#24407)
This flag is already enabled on all relevant surfaces. We can remove it.
2022-04-20 10:17:52 -04:00
Sebastian Markbåge 5aa0c5671f
Fix Issue with Undefined Lazy Imports By Refactoring Lazy Initialization Order (#21642)
* Add a DEV warning for common case

* Don't set Pending flag before we know it's a promise

* Move default exports extraction to render phase

This is really where most unwrapping happen. The resolved promise is the
module object and then we read things from it.

This way it lines up a bit closer with the Promise model too since the
promise resolving to React gets passed this same value.

If this throws, then it throws during render so it's caught properly and
you can break on it and even see it on the right stack.

* Check if the default is in the module object instead of if it's undefined

Normally we'd just check if something is undefined but in this case it's
valid to have an undefined value in the export but if you don't have a
property then you're probably importing the wrong kind of object.

* We need to check if it's uninitialized for sync resolution

Co-authored-by: Dan Abramov <dan.abramov@me.com>
2021-06-07 14:47:18 -07:00
Brian Vaughn 2bf4805e4b
Update entry point exports (#21488)
The following APIs have been added to the `react` stable entry point:
* `SuspenseList`
* `startTransition`
* `unstable_createMutableSource`
* `unstable_useMutableSource`
* `useDeferredValue`
* `useTransition`

The following APIs have been added or removed from the `react-dom` stable entry point:
* `createRoot`
* `unstable_createPortal` (removed)

The following APIs have been added to the `react-is` stable entry point:
* `SuspenseList`
* `isSuspenseList`

The following feature flags have been changed from experimental to true:
* `enableLazyElements`
* `enableSelectiveHydration`
* `enableSuspenseServerRenderer`
2021-05-12 11:28:14 -04:00
Ricky 933880b454
Make time-slicing opt-in (#21072)
* Add enableSyncDefaultUpdates feature flag

* Add enableSyncDefaultUpdates implementation

* Fix tests

* Switch feature flag to true by default

* Finish concurrent render whenever for non-sync lanes

* Also return DefaultLane with eventLane

* Gate interruption test

* Add continuout native event test

* Fix tests from rebasing main

* Hardcode lanes, remove added export

* Sync forks
2021-04-09 19:50:09 -04:00
Brian Vaughn 7c1ba2b57d
Proposed new Suspense layout effect semantics (#21079)
This commit contains a proposed change to layout effect semantics within Suspense subtrees: If a component mounts within a Suspense boundary and is later hidden (because of something else suspending) React will cleanup that component’s layout effects (including React-managed refs).

This change will hopefully fix existing bugs that occur because of things like reading layout in a hidden tree and will also enable a point at which to e.g. pause videos and hide user-managed portals. After the suspended boundary resolves, React will setup the component’s layout effects again (including React-managed refs).

The scenario described above is not common. The useTransition API should ensure that Suspense does not revert to its fallback state after being mounted.

Note that these changes are primarily written in terms of the (as of yet internal) Offscreen API as we intend to provide similar effects semantics within recently shown/hidden Offscreen trees in the future. (More to follow.)

(Note that all changes in this PR are behind a new feature flag, enableSuspenseLayoutEffectSemantics, which is disabled for now.)
2021-04-06 09:21:02 -04:00
Sebastian Markbåge 5711811da1
Reconcile element types of lazy component yielding the same type (#20357)
* Reconcile element types of lazy component yielding the same type

* Add some legacy mode and suspense boundary flushing tests

* Fix infinite loop in legacy mode

In legacy mode we typically commit the suspending fiber and then rerender
the nearest boundary to render the fallback in a separate commit.

We can't do that when the boundary itself suspends because when we try to
do the second pass, it'll suspend again and infinite loop.

Interestingly the legacy semantics are not needed in this case because
they exist to let an existing partial render fully commit its partial state.

In this case there's no partial state, so we can just render the fallback
immediately instead.

* Check fast refresh compatibility first

resolveLazy can suspend and if it does, it can resuspend. Fast refresh
assumes that we don't resuspend. Instead it relies on updating the inner
component later.

* Use timers instead of act to force fallbacks to show
2020-12-01 07:14:04 -08:00
Dan Abramov bc6b7b6b16
Don't trigger lazy in DEV during element creation (#19871) 2020-09-21 16:04:49 +01:00
Andrew Clark 8f05f2bd6d
Land Lanes implementation in old fork (#19108)
* Add autofix to cross-fork lint rule

* replace-fork: Replaces old fork contents with new

For each file in the new fork, copies the contents into the
corresponding file of the old fork, replacing what was already there.

In contrast to merge-fork, which performs a three-way merge.

* Replace old fork contents with new fork

First I ran  `yarn replace-fork`.

Then I ran `yarn lint` with autofix enabled. There's currently no way to
do that from the command line (we should fix that), so I had to edit the
lint script file.

* Manual fix-ups

Removes dead branches, removes prefixes from internal fields.  Stuff
like that.

* Fix DevTools tests

DevTools tests only run against the old fork, which is why I didn't
catch these earlier.

There is one test that is still failing. I'm fairly certain it's related
to the layout of the Suspense fiber: we no longer conditionally wrap the
primary children. They are always wrapped in an extra fiber.

Since this has been running in www for weeks without major issues, I'll
defer fixing the remaining test to a follow up.
2020-06-11 20:05:15 -07:00
Sebastian Markbåge 518ce9c25f
Add Lazy Elements Behind a Flag (#19033)
We really needed this for Flight before as well but we got away with it
because Blocks were lazy but with the removal of Blocks, we'll need this
to ensure that we can lazily stream in part of the content.

Luckily LazyComponent isn't really just a Component. It's just a generic
type that can resolve into anything kind of like a Promise.

So we can use that to resolve elements just like we can components.

This allows keys and props to become lazy as well.

To accomplish this, we suspend during reconciliation. This causes us to
not be able to render siblings because we don't know if the keys will
reconcile. For initial render we could probably special case this and
just render a lazy component fiber.

Throwing in reconciliation didn't work correctly with direct nested
siblings of a Suspense boundary before but it does now so it depends
on new reconciler.
2020-05-28 14:16:35 -07:00
Moti Zilberman 3c7d52c3d6
Give unresolved lazy() a name in component stack (#16104)
* Give unresolved lazy() a name in component stack

* Normalize stack in tests

Co-authored-by: Sebastian Markbage <sema@fb.com>
2020-05-01 16:41:39 -07:00
Sebastian Markbåge 5474a83e25
Disable console.logs in the second render pass of DEV mode double render (#18547)
* Disable console log during the second rerender

* Use the disabled log to avoid double yielding values in scheduler mock

* Reenable debugRenderPhaseSideEffectsForStrictMode in tests that can
2020-04-08 16:43:51 -07:00
jddxf 241103a6fb
Fix bailout broken in lazy components due to default props resolving (#18539)
* Add failing tests for lazy components

* Fix bailout broken in lazy components due to default props resolving

We should never compare unresolved props with resolved props. Since comparing
resolved props by reference doesn't make sense, we use unresolved props in that
case. Otherwise, resolved props are used.

* Avoid reassigning props warning when we bailout
2020-04-08 10:58:57 +01:00
Sebastian Markbåge fd61f7ea53
Refactor Lazy Components to use teh Suspense (and wrap Blocks in Lazy) (#18362)
* Refactor Lazy Components

* Switch Blocks to using a Lazy component wrapper

Then resolve to a true Block inside.

* Test component names of lazy Blocks
2020-03-22 21:53:05 -07:00
Dan Abramov b979db4e72
Bump Prettier (#17811)
* Bump Prettier

* Reformat

* Use non-deprecated option
2020-01-09 13:54:11 +00:00
Dan Abramov 0b5a26a489
Rename toWarnDev -> toErrorDev, toLowPriorityWarnDev -> toWarnDev (#17605)
* Rename toWarnDev -> toErrorDev in tests

* Rename toWarnDev matcher implementation to toErrorDev

* Rename toLowPriorityWarnDev -> toWarnDev in tests and implementation
2019-12-16 12:48:16 +00:00
Andrew Clark 0bd0c5269f
Upgrade ESLint so we can use JSX Fragment syntax (#16328)
Now that we're using Babel 7, this is the last blocker.
2019-08-09 12:59:02 -07:00
Sunil Pai f62b53d908
fix some missing assertions (#16336)
These were discovered by @SimenB in https://github.com/facebook/react/pull/16332. We weren't making actual assertions on some values. This PR makes the assertions, and fixes the tests.
2019-08-09 15:34:49 +01:00
Andrew Clark 4d307de458
Prefix mock Scheduler APIs with _unstable (#15999)
For now this is only meant to be consumed via `act`.
2019-06-26 12:16:08 -07:00
Sebastian Markbåge 113497cc0e
[Suspense] Change Suspending and Restarting Heuristics (#15769)
* Track most recent commit time of a fallback globally

This value is going to be used to avoid committing too many fallback
states in quick succession. It doesn't really matter where in the tree
that happened.

This means that we now don't really need the concept of SuspenseState
other than has a flag. It could be made cheaper/simpler.

* Change suspense heuristic

This now eagerly commits non-delayed suspended trees, unless they're
only retries in which case they're throttled to 500ms.

* Restart early if we're going to suspend later

* Use the local variable where appropriate

* Make ReactLazy tests less specific on asserting intermediate states

They're not testing the exact states of the suspense boundaries, only
the result. I keep assertions that they're not already resolved early.

* Adjust Profiler tests to the new heuristics

* Update snapshot tests for user timing tests

I also added a blank initial render to ensuree that we cover the suspended
case.

* Adjust Suspense tests to account for new heuristics

Mostly this just means render the Suspense boundary first so that it
becomes an update instead of initial mount.

* Track whether we have a ping on the currently rendering level

If we get a ping on this level but have not yet suspended, we might
still suspend later. In that case we should still restart.

* Add comment about moving markers

We should add this to throwException so we get these markers earlier.
I've had to rewrite tests that test restarting to account for the delayed
restarting heuristic.

Ideally, we should also be able to restart from within throwException if
we're already ready to restart. Right now we wait until the next yield.

* Add test for restarting during throttled retry

* Add test that we don't restart for initial render

* Add Suspense Heuristics as a comment in Throw
2019-05-30 16:37:56 -07:00
Andrew Clark 9055e31e5c
Replace old Fiber Scheduler with new one (#15387)
The new Fiber Scheduler has been running in Facebook for several days
without issues. Let's switch to it.
2019-04-11 19:15:34 -07:00
Andrew Clark 4d5cb64aa2
Rewrite ReactFiberScheduler for better integration with Scheduler package (#15151)
* Rewrite ReactFiberScheduler

Adds a new implementation of ReactFiberScheduler behind a feature flag.
We will maintain both implementations in parallel until the new one
is proven stable enough to replace the old one.

The main difference between the implementations is that the new one is
integrated with the Scheduler package's priority levels.

* Conditionally add fields to FiberRoot

Some fields only used by the old scheduler, and some by the new.

* Add separate build that enables new scheduler

* Re-enable skipped test

If synchronous updates are scheduled by a passive effect, that work
should be flushed synchronously, even if flushPassiveEffects is
called inside batchedUpdates.

* Passive effects have same priority as render

* Revert ability to cancel the current callback

React doesn't need this anyway because it never schedules callbacks if
it's already rendering.

* Revert change to FiberDebugPerf

Turns out this isn't neccessary.

* Fix ReactFiberScheduler dead code elimination

Should initialize to nothing, then assign the exports conditionally,
instead of initializing to the old exports and then reassigning to the
new ones.

* Don't yield before commit during sync error retry

* Call Scheduler.flushAll unconditionally in tests

Instead of wrapping in enableNewScheduler flag.
2019-04-02 15:49:07 -07:00
Sebastian Markbåge 4c75881ee3
Remove maxDuration from tests (#15272)
We instead assume a 150ms duration.
2019-04-02 14:27:44 -07:00
Andrew Clark 69060e1da6
Swap expect(ReactNoop) for expect(Scheduler) (#14971)
* Swap expect(ReactNoop) for expect(Scheduler)

In the previous commits, I upgraded our custom Jest matchers for the
noop and test renderers to use Scheduler under the hood.

Now that all these matchers are using Scheduler, we can drop
support for passing ReactNoop and test roots and always pass
Scheduler directly.

* Externalize Scheduler in noop and test bundles

I also noticed we don't need to regenerator runtime in noop anymore.
2019-02-28 12:54:47 -08:00
Andrew Clark ccb2a8a44e
Replace test renderer's fake Scheduler implementation with mock build (#14970)
* Replace test renderer's fake Scheduler implementation with mock build

The test renderer has its own mock implementation of the Scheduler
interface, with the ability to partially render work in tests. Now that
this functionality has been lifted into a proper mock Scheduler build,
we can use that instead.

* Fix Profiler tests in prod
2019-02-28 10:50:38 -08:00
Dan Abramov 2a084f51a9
Warn about refs on lazy function components (#14645) 2019-01-21 16:57:55 +00:00
Dan Abramov 10a7a5b5ce
Fix synchronous thenable rejection (#14633)
* Fix handling of sync rejection

Reverts #14632 and adds a regression test.

* Handle rejection synchronously too

Fewer footguns and seems like nicer behavior anyway.
2019-01-19 00:42:43 +00:00
Dan Abramov 9120f6c2d8
Support sync thenables for lazy() (#14626)
* Support sync thenables for lazy()

* Don't commit twice
2019-01-18 21:16:02 +00:00
Dan Abramov d14ba87b1b
Validate propTypes for lazy() and memo() and warn about invalid patterns (#14298)
* Add a test for current defaultProps behavior in lazy

* Add a warning against definining defaultProps on the outer wrapper

* Warn about setting propTypes too

* Remove redundant async

* Validate propTypes for resolved lazy types

Note this only works for elements created after resolving. So it's not ideal. But it provides the best stack trace for those cases.

* Add a test for lazy(forwardRef()) propTypes check

* Validate memo() inner propTypes and warn about shadowing

* Add test verifying nested lazy is unsupported

* Change error wording to remove "Promise elements"

* Improve error message for nested lazy() and add tests

* Validate propTypes for memo in the reconciler when necessary

* Add comments for why we're calling checkPropTypes

* Fix Flow and lint

* Undo unintentional formatting changes

* Remove unnecessary case (it is handled by function code path)

* Add test coverage for memo(fn.defaultProps).propTypes

* Test should be agnostic of where resolving happens

That's an implementation detail and we might want to change it later. Let's keep it easy by making tests just check that validation happened, not at which stage.

* Unify traversal logic in createElement

This moves all type traversal into createElement. When lazy resolves, we call createElement once to re-check.

* Match prod behavior for propTypes/defaultProps shims closer

* Revert "Unify traversal logic in createElement"

This reverts commit 2e77ca47fe.

See https://github.com/facebook/react/pull/14298#issuecomment-442687775

* Undo unnecessary change to getComponentName
2018-11-29 20:06:28 +00:00
Dan Abramov 0c7189d923
Fix resolution of outer props with React.memo() (#14312)
* Add failing test for defaultProps between lazy() and memo()

* Add another regression test for defaultProps resolution order

* Resolve outer props for MemoComponent
2018-11-22 19:40:42 +00:00
Dan Abramov f777d196e0
Fix lazy() with defaultProps (#14112)
* Resolve defaultProps for Lazy components

* Make test fail again

* Undo the partial fix

* Make test output more compact

* Add a separate failing test for sync mode

* Clean up tests

* Add another update to both tests

* Resolve props for commit phase lifecycles

* Resolve prevProps for begin phase lifecycles

* Resolve prevProps for pre-commit lifecycles

* Only resolve props if element type differs

* Fix Flow

* Don't set instance.props/state during commit phase

This is an optimization. I'm not sure it's entirely safe. It's probably worth running internal tests and see if we can ever trigger a case where they're different.

This can mess with resuming.

* Keep setting instance.props/state before unmounting

This reverts part of the previous commit. It broke a test that verifies we use current props in componentWillUnmount if the fiber unmounts due to an error.
2018-11-06 19:54:14 +00:00
Sebastian Markbåge d75c69e0cf
Remove unstable_ prefix from Suspense (#13922)
We are using it with lazy and the combination Suspense + lazy seems pretty
stable. maxDuration is not but that's only enabled when you're in
ConcurrentMode which is still unstable.
2018-10-22 22:40:05 -07:00
Andrew Clark b753f76a74 Fix failing async tests in Node 10
Dunno why they happened to work in Node 8 but whatever. Tested on both.
2018-10-20 16:06:23 -07:00
Sebastian Markbåge 95a313ec0b Unfork Lazy Component Branches (#13902)
* Introduce elementType field

This will be used to store the wrapped type of an element. E.g. pure and
lazy.

The existing type field will be used for the unwrapped type within them.

* Store the unwrapped type on the type field of lazy components

* Use the raw tags for lazy components

Instead, we check if the elementType and type are equal to test if
we need to resolve props. This is slightly slower in the normal case
but will yield less code and branching.

* Clean up lazy branches

* Collapse work tag numbering

* Split IndeterminateComponent out from Lazy

This way we don't have to check the type in a hacky way in the
indeterminate path. Also, lets us deal with lazy that resolves to
indeterminate and such.

* Missing clean up in rebase
2018-10-19 22:22:45 -07:00
Andrew Clark 36db538226
Bugfix for #13886 (#13896)
Fixes a bug where a lazy component does not cache the result of
its constructor.
2018-10-19 13:57:42 -07:00
Andrew Clark d9a3cc070c
React.lazy constructor must return result of a dynamic import (#13886)
We may want to change the protocol later, so until then we'll be
restrictive. Heuristic is to check for existence of `default`.
2018-10-18 19:58:25 -07:00
Andrew Clark d9659e499e
Lazy components must use React.lazy (#13885)
Removes support for using arbitrary promises as the type of a React
element. Instead, promises must be wrapped in React.lazy. This gives us
flexibility later if we need to change the protocol.

The reason is that promises do not provide a way to call their
constructor multiple times. For example:

const promiseForA = new Promise(resolve => {
  fetchA(a => resolve(a));
});

Given a reference to `promiseForA`, there's no way to call `fetchA`
again. Calling `then` on the promise doesn't run the constructor again;
it only attaches another listener.

In the future we will likely introduce an API like `React.eager` that
is similar to `lazy` but eagerly calls the constructor. That gives us
the ability to call the constructor multiple times. E.g. to increase
the priority, or to retry if the first operation failed.
2018-10-18 19:57:12 -07:00