Skip to content

Commit

Permalink
Make the AsyncModeProvider API a stable API (#18154)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored and hypest committed Nov 4, 2019
1 parent 6277ffd commit 0eb16bd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
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.

_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;

/**
* 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

0 comments on commit 0eb16bd

Please sign in to comment.