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

Make the AsyncModeProvider API a stable API #18154

Merged
merged 2 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* WordPress dependencies
*/
import {
__experimentalAsyncModeProvider as AsyncModeProvider,
useSelect,
} from '@wordpress/data';
import { AsyncModeProvider, useSelect } from '@wordpress/data';

const BlockAsyncModeProvider = ( { children, clientId, isBlockInSelection } ) => {
const isParentOfSelectedBlock = useSelect( ( select ) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Component } from '@wordpress/element';
import {
withSelect,
withDispatch,
__experimentalAsyncModeProvider as AsyncModeProvider,
AsyncModeProvider,
} from '@wordpress/data';
import { compose } from '@wordpress/compose';

Expand Down
40 changes: 40 additions & 0 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,46 @@ Specific implementation differences from Redux and React Redux:

<!-- START TOKEN(Autogenerated API docs) -->

<a name="AsyncModeProvider" href="#AsyncModeProvider">#</a> **AsyncModeProvider**

Context Provider Component used to switch the data module component rerendering
between Sync and Async modes.

_Usage_

```js
import { useSelect, AsyncModeProvider } from '@wordpress/data';

function BlockCount() {
const count = useSelect( ( select ) => {
return select( 'core/block-editor' ).getBlockCount()
} );

return count;
}

function App() {
return (
<AsyncModeProvider value={ true }>
<BlockCount />
</AsyncModeProvider>
);
}
```

In this example, the BlockCount component is rerendered asynchronously.
It means if a more critical task is being performed (like typing in an input),
the rerendering is delayed until the browser becomes IDLE.
It is possible to nest multiple levels of AsyncModeProvider to fine-tune the rendering behavior.

youknowriad marked this conversation as resolved.
Show resolved Hide resolved
_Parameters_

- _props.value_ `boolean`: Enable Async Mode.

_Returns_

- `WPComponent`: The component to be rendered.

<a name="combineReducers" href="#combineReducers">#</a> **combineReducers**

The combineReducers helper function turns an object whose values are different
Expand Down
34 changes: 34 additions & 0 deletions packages/data/src/components/async-mode-provider/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,38 @@ const { Consumer, Provider } = Context;

export const AsyncModeConsumer = Consumer;

/**
Copy link
Member

Choose a reason for hiding this comment

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

We need an example here. It's a very complex use case, so I would also include more details. We should also share some recommendations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be fair, this needs a blog post to be explained :) so I'm not really sure what to add here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll try to expand a bit, but not sure how good it will be :P

Copy link
Member

Choose a reason for hiding this comment

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

Documenting is hard :) You can always post on make.wordpress.org/core and reference it from here 🤷‍♂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, let me know what you think.

* Context Provider Component used to switch the data module component rerendering
* between Sync and Async modes.
*
* @example
*
* ```js
* import { useSelect, AsyncModeProvider } from '@wordpress/data';
*
* function BlockCount() {
* const count = useSelect( ( select ) => {
* return select( 'core/block-editor' ).getBlockCount()
* } );
*
* return count;
* }
*
* function App() {
* return (
* <AsyncModeProvider value={ true }>
* <BlockCount />
* </AsyncModeProvider>
* );
* }
* ```
*
* In this example, the BlockCount component is rerendered asynchronously.
* It means if a more critical task is being performed (like typing in an input),
* the rerendering is delayed until the browser becomes IDLE.
* It is possible to nest multiple levels of AsyncModeProvider to fine-tune the rendering behavior.
*
* @param {boolean} props.value Enable Async Mode.
* @return {WPComponent} The component to be rendered.
*/
export default Provider;
4 changes: 1 addition & 3 deletions packages/data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export {
} from './components/registry-provider';
export { default as useSelect } from './components/use-select';
export { useDispatch } from './components/use-dispatch';
export {
AsyncModeProvider as __experimentalAsyncModeProvider,
} from './components/async-mode-provider';
export { AsyncModeProvider } from './components/async-mode-provider';
export { createRegistry } from './registry';
export { createRegistrySelector, createRegistryControl } from './factory';

Expand Down