forked from facebook/react
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Blocks] Initial implementation of cache and data/fetch (facebook#18774)
* Rename ReactCache -> ReactCacheOld We still use it in some tests so I'm going to leave it for now. I'll start making the new one in parallel in the react package. * Add react/unstable-cache entry point * Add react-data entry point * Initial implementation of cache and data/fetch * Address review
- Loading branch information
Showing
25 changed files
with
367 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
|
||
'use strict'; | ||
|
||
export * from './src/ReactCache'; | ||
export * from './src/ReactCacheOld'; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# react-data | ||
|
||
This package is meant to be used alongside yet-to-be-released, experimental React features. It's unlikely to be useful in any other context. | ||
|
||
**Do not use in a real application.** We're publishing this early for | ||
demonstration purposes. | ||
|
||
**Use it at your own risk.** | ||
|
||
# No, Really, It Is Unstable | ||
|
||
The API ~~may~~ will change wildly between versions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
export * from './src/fetch/ReactDataFetch'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
export * from './src/ReactData'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/react-data-fetch.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/react-data-fetch.development.js'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/react-data.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/react-data.development.js'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"private": true, | ||
"name": "react-data", | ||
"description": "Helpers for creating React data sources", | ||
"version": "0.0.0", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/facebook/react.git", | ||
"directory": "packages/react-data" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"build-info.json", | ||
"index.js", | ||
"fetch.js", | ||
"cjs/" | ||
], | ||
"peerDependencies": { | ||
"react": "^16.13.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export function createResource(): any { | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('ReactData', () => { | ||
let ReactData; | ||
|
||
beforeEach(() => { | ||
ReactData = require('react-data'); | ||
}); | ||
|
||
// TODO: test something useful. | ||
it('exports something', () => { | ||
expect(ReactData.createResource).not.toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('ReactDataFetch', () => { | ||
let ReactDataFetch; | ||
|
||
beforeEach(() => { | ||
ReactDataFetch = require('react-data/fetch'); | ||
}); | ||
|
||
// TODO: test something useful. | ||
it('exports something', () => { | ||
expect(ReactDataFetch.fetch).not.toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {Wakeable} from 'shared/ReactTypes'; | ||
|
||
import {readCache} from 'react/unstable-cache'; | ||
|
||
const Pending = 0; | ||
const Resolved = 1; | ||
const Rejected = 2; | ||
|
||
type PendingResult = {| | ||
status: 0, | ||
value: Wakeable, | ||
|}; | ||
|
||
type ResolvedResult = {| | ||
status: 1, | ||
value: mixed, | ||
|}; | ||
|
||
type RejectedResult = {| | ||
status: 2, | ||
value: mixed, | ||
|}; | ||
|
||
type Result = PendingResult | ResolvedResult | RejectedResult; | ||
|
||
const fetchKey = {}; | ||
|
||
function readResultMap(): Map<string, Result> { | ||
const resources = readCache().resources; | ||
let map = resources.get(fetchKey); | ||
if (map === undefined) { | ||
map = new Map(); | ||
resources.set(fetchKey, map); | ||
} | ||
return map; | ||
} | ||
|
||
// TODO: options, auth, etc. | ||
export function fetch(url: string): Object { | ||
const map = readResultMap(); | ||
const entry = map.get(url); | ||
if (entry === undefined) { | ||
let resolve = () => {}; | ||
const wakeable: Wakeable = new Promise(r => { | ||
// TODO: should this be a plain thenable instead? | ||
resolve = r; | ||
}); | ||
const result: Result = { | ||
status: Pending, | ||
value: wakeable, | ||
}; | ||
map.set(url, result); | ||
const xhr = new XMLHttpRequest(); | ||
xhr.onload = function() { | ||
// TODO: should we handle status codes? | ||
if (result.status !== Pending) { | ||
return; | ||
} | ||
const resolvedResult = ((result: any): ResolvedResult); | ||
resolvedResult.status = Resolved; | ||
resolvedResult.value = xhr.response; | ||
resolve(); | ||
}; | ||
xhr.onerror = function() { | ||
if (result.status !== Pending) { | ||
return; | ||
} | ||
const rejectedResult = ((result: any): RejectedResult); | ||
rejectedResult.status = Rejected; | ||
// TODO: use something else as the error value? | ||
rejectedResult.value = xhr; | ||
resolve(); | ||
}; | ||
xhr.open('GET', url); | ||
xhr.send(); | ||
throw wakeable; | ||
} | ||
const result: Result = entry; | ||
if (result.status === Resolved) { | ||
return result.value; | ||
} else { | ||
throw result.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/react-unstable-cache.production.min.js'); | ||
} else { | ||
module.exports = require('./cjs/react-unstable-cache.development.js'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.