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

[react-cache] Remove cache as argument to read #13865

Merged
merged 3 commits into from
Oct 23, 2018

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Oct 17, 2018

Updated is API is Resource.read(key) instead of Resource.read(cache, key).

The cache is read from context using readContext.

This also removes cache invalidation entirely (other than the default LRU mechanism), as well as the ability to have multiple caches. We'll add it back once Context.write (some version of #13293) lands and we can implement it the right way.

Since there's now only a single cache (the global one), we don't actually need to use context yet, but I've added a dummy context anyway so the user gets an error if they attempt to read outside the render phase.

@sizebot
Copy link

sizebot commented Oct 17, 2018

Fails
🚫

node` failed.

Log

Error:  { FetchError: invalid json response body at http://react.zpao.com/builds/master/_commits/80a0c05ce3e0e1babe5c8fc97ef24fd67cb4e735/results.json reason: Unexpected token < in JSON at position 0
    at /home/circleci/project/node_modules/node-fetch/lib/body.js:48:31
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
  name: 'FetchError',
  message: 'invalid json response body at http://react.zpao.com/builds/master/_commits/80a0c05ce3e0e1babe5c8fc97ef24fd67cb4e735/results.json reason: Unexpected token < in JSON at position 0',
  type: 'invalid-json' }

Generated by 🚫 dangerJS

ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
ReactFeatureFlags.enableSuspense = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC, enableSuspense has been removed in #13799 ?

throw new Error(
'react-cache: read and preload may only be called from within a ' +
"component's render. They are not supported in event handlers or " +
'life-cycles.',
Copy link
Collaborator

Choose a reason for hiding this comment

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

“lifecycle methods”?


function readContext(Context, observedBits) {
const dispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let’s hoist CurrentOwner? No need to access it many times.

<Suspense>
<AsyncText ms={100} text={1} />
<AsyncText ms={100} text={2} />
<AsyncText ms={100} text={3} />
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use or add a PureComponent of AsyncText case like :

<AsyncText ms={100} text={2} />
<AsyncText ms={100} text={3} />
<AsyncText ms={100} text={1} />

here? IMO, we need a case which test the cache read and common reconciliation together.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This test does read from the cache? And idk what you mean by "common reconciliation"

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry. I mean, this just test the function component(the AsyncText) but in real world, we have class component which can be PureComponent or normal Component, so in the common reconciliation and update, they will go to different switch cases or codes, I think maybe it also should be tested with the cache read processing. @acdlite

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

Dan's comments.

resolvedRecord.status = Resolved;
resolvedRecord.suspender = null;
resolvedRecord.value = value;
if (newResult.status === Pending) {
Copy link
Contributor

@aweary aweary Oct 23, 2018

Choose a reason for hiding this comment

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

What is this status check guarding against?

Copy link
Collaborator Author

@acdlite acdlite Oct 23, 2018

Choose a reason for hiding this comment

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

Guards against something resolving multiple times. Promises don't do that but a thenable might. Thanks for pointing this out, though, because now I realize I didn't write a test for it.

Updated is API is `Resource.read(key)` instead of
`Resource.read(cache, key)`.

The cache is read from context using `readContext`.

This also removes cache invalidation entirely (other than the default
LRU mechanism), as well as the ability to have multiple caches. We'll
add it back once `Context.write` lands and we can implement it the
right way.

Since there's now only a single cache (the global one), we don't
actually need to use context yet, but I've added a dummy context
anyway so the user gets an error if they attempt to read outside the
render phase.
@acdlite acdlite merged commit d8e03de into facebook:master Oct 23, 2018
@gaearon gaearon mentioned this pull request Oct 23, 2018
topheman added a commit to topheman/react-fiber-experiments that referenced this pull request Oct 26, 2018
Some breaking changes in the unstable APIs of Suspense:

* No more need to use `enableSuspense`
  * flag in https://github.com/facebook/react/blob/master/packages/shared/ReactFeatureFlags.js#L48
* To generate custom build, use: `yarn build dom-client,core,react-cache,scheduler --type=NODE`
  * facebook/react@8af6728#diff-e7efbec821bc95957204c2fcbd251e74
* `<Placeholder delayMs />` renamed to `<Suspense maxDuration />`
  * facebook/react@8af6728
  * facebook/react@d75c69e
* react-cache
  * Add "unstable_" prefix - `createResource` becomes `unstable_createResource`
    * facebook/react@915e4ea
  * Remove `cache` as argument to `read`
    * facebook/react#13865
topheman added a commit to topheman/react-fiber-experiments that referenced this pull request Oct 26, 2018
Some breaking changes in the unstable APIs of Suspense:

* No more need to use `enableSuspense`
  * flag in https://github.com/facebook/react/blob/master/packages/shared/ReactFeatureFlags.js#L48
* To generate custom build, use: `yarn build dom-client,core,react-cache,scheduler --type=NODE`
  * facebook/react@8af6728#diff-e7efbec821bc95957204c2fcbd251e74
* `<Placeholder delayMs />` renamed to `<Suspense maxDuration />`
  * facebook/react@8af6728
  * facebook/react@d75c69e
* react-cache
  * Add "unstable_" prefix - `createResource` becomes `unstable_createResource`
    * facebook/react@915e4ea
  * Remove `cache` as argument to `read`
    * facebook/react#13865
jetoneza pushed a commit to jetoneza/react that referenced this pull request Jan 23, 2019
* [react-cache] Remove `cache` as argument to `read`

Updated is API is `Resource.read(key)` instead of
`Resource.read(cache, key)`.

The cache is read from context using `readContext`.

This also removes cache invalidation entirely (other than the default
LRU mechanism), as well as the ability to have multiple caches. We'll
add it back once `Context.write` lands and we can implement it the
right way.

Since there's now only a single cache (the global one), we don't
actually need to use context yet, but I've added a dummy context
anyway so the user gets an error if they attempt to read outside the
render phase.

* nits

* Add test for thenables that resolve multiple times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants