-
Notifications
You must be signed in to change notification settings - Fork 48.8k
Skeleton for store #33215
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
base: main
Are you sure you want to change the base?
Skeleton for store #33215
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and 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'; | ||
|
||
let React; | ||
let ReactNoop; | ||
let Scheduler; | ||
let act; | ||
let use; | ||
let assertLog; | ||
let createStore; | ||
|
||
describe('ReactUse', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
Scheduler = require('scheduler'); | ||
act = require('internal-test-utils').act; | ||
use = React.use; | ||
createStore = React.unstable_createStore; | ||
|
||
const InternalTestUtils = require('internal-test-utils'); | ||
assertLog = InternalTestUtils.assertLog; | ||
}); | ||
|
||
// @gate enableStore | ||
it('should read the current value', async () => { | ||
const store = createStore(1); | ||
|
||
function App() { | ||
const value = use(store); | ||
Scheduler.log(value); | ||
return <span>{value}</span>; | ||
} | ||
|
||
const root = ReactNoop.createRoot(); | ||
await act(() => { | ||
root.render(<App />); | ||
}); | ||
assertLog([1]); | ||
expect(root).toMatchRenderedOutput(<span>1</span>); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Copyright (c) Meta Platforms, Inc. and 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 {ReactStore} from 'shared/ReactTypes'; | ||||||||||||||||||||||||||||||
import {REACT_STORE_TYPE} from 'shared/ReactSymbols'; | ||||||||||||||||||||||||||||||
import {enableStore} from 'shared/ReactFeatureFlags'; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
export function createStore<T>( | ||||||||||||||||||||||||||||||
defaultValue: T, | ||||||||||||||||||||||||||||||
reducer?: (T, mixed) => T, | ||||||||||||||||||||||||||||||
): ReactStore<T> { | ||||||||||||||||||||||||||||||
Comment on lines
+14
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be able have strong types for reducer actions while keeping both reducers and actions optional.
Suggested change
|
||||||||||||||||||||||||||||||
if (!enableStore) { | ||||||||||||||||||||||||||||||
throw new Error('Not implemented.'); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const store: ReactStore<T> = { | ||||||||||||||||||||||||||||||
$$typeof: REACT_STORE_TYPE, | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
_current: defaultValue, | ||||||||||||||||||||||||||||||
_sync: defaultValue, | ||||||||||||||||||||||||||||||
_transition: defaultValue, | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
Comment on lines
+22
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return store; | ||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -64,6 +64,13 @@ export type ReactContext<T> = { | |||||||||||||||||||||||||
displayName?: string, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export type ReactStore<T> = { | ||||||||||||||||||||||||||
$$typeof: symbol, | ||||||||||||||||||||||||||
_current: T, | ||||||||||||||||||||||||||
_sync: T, | ||||||||||||||||||||||||||
_transition: T, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Comment on lines
+67
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if the While the
Suggested change
|
||||||||||||||||||||||||||
export type ReactPortal = { | ||||||||||||||||||||||||||
$$typeof: symbol | number, | ||||||||||||||||||||||||||
key: null | string, | ||||||||||||||||||||||||||
|
@@ -141,7 +148,7 @@ export type StartTransitionOptions = { | |||||||||||||||||||||||||
name?: string, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export type Usable<T> = Thenable<T> | ReactContext<T>; | ||||||||||||||||||||||||||
export type Usable<T> = Thenable<T> | ReactContext<T> | ReactStore<T>; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export type ReactCustomFormAction = { | ||||||||||||||||||||||||||
name?: string, | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReactUseStore?