Skip to content

Commit

Permalink
Try async mode in the data module
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 21, 2018
1 parent c123f1d commit 8b9d156
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
10 changes: 10 additions & 0 deletions packages/data/src/components/async-mode-provider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';

const { Consumer, Provider } = createContext( false );

export const AsyncModeConsumer = Consumer;

export default Provider;
54 changes: 46 additions & 8 deletions packages/data/src/components/with-select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ import { createHigherOrderComponent } from '@wordpress/compose';
* Internal dependencies
*/
import { RegistryConsumer } from '../registry-provider';
import { AsyncModeConsumer } from '../async-mode-provider';

const waitingList = [];
const componentsMap = new WeakMap();
let isRunning = false;
const runWaitingList = () => {
if ( waitingList.length === 0 ) {
isRunning = false;
return;
}
const next = waitingList.shift();
next();
window.requestAnimationFrame( runWaitingList );
};

const addToWaitingList = ( component, item ) => {
if ( componentsMap.has( component ) ) {
waitingList[ componentsMap.get( component ) ] = item;
} else {
componentsMap.set( component, waitingList.length );
waitingList.push( item );
}
if ( ! isRunning ) {
isRunning = true;
window.requestAnimationFrame( runWaitingList );
}
};

/**
* Higher-order component used to inject state-derived props using registered
Expand Down Expand Up @@ -137,7 +164,13 @@ const withSelect = ( mapSelectToProps ) => createHigherOrderComponent( ( Wrapped
}

subscribe( registry ) {
this.unsubscribe = registry.subscribe( this.onStoreChange );
this.unsubscribe = registry.subscribe( () => {
if ( this.props.isAsync ) {
addToWaitingList( this, this.onStoreChange );
} else {
this.onStoreChange();
}
} );
}

render() {
Expand All @@ -146,14 +179,19 @@ const withSelect = ( mapSelectToProps ) => createHigherOrderComponent( ( Wrapped
}

return ( ownProps ) => (
<RegistryConsumer>
{ ( registry ) => (
<ComponentWithSelect
ownProps={ ownProps }
registry={ registry }
/>
<AsyncModeConsumer>
{ ( isAsync ) => (
<RegistryConsumer>
{ ( registry ) => (
<ComponentWithSelect
ownProps={ ownProps }
registry={ registry }
isAsync={ isAsync }
/>
) }
</RegistryConsumer>
) }
</RegistryConsumer>
</AsyncModeConsumer>
);
}, 'withSelect' );

Expand Down
1 change: 1 addition & 0 deletions packages/data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as plugins from './plugins';
export { default as withSelect } from './components/with-select';
export { default as withDispatch } from './components/with-dispatch';
export { default as RegistryProvider, RegistryConsumer } from './components/registry-provider';
export { default as AsyncModeProvider } from './components/async-mode-provider';
export { createRegistry } from './registry';
export { plugins };

Expand Down
36 changes: 19 additions & 17 deletions packages/editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { withSelect, withDispatch, AsyncModeProvider } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -185,22 +185,24 @@ class BlockList extends Component {
} = this.props;

return (
<div className="editor-block-list__layout">
{ map( blockClientIds, ( clientId, blockIndex ) => (
<BlockListBlock
key={ 'block-' + clientId }
index={ blockIndex }
clientId={ clientId }
blockRef={ this.setBlockRef }
onSelectionStart={ this.onSelectionStart }
rootClientId={ rootClientId }
isFirst={ blockIndex === 0 }
isLast={ blockIndex === blockClientIds.length - 1 }
isDraggable={ isDraggable }
/>
) ) }
<BlockListAppender rootClientId={ rootClientId } />
</div>
<AsyncModeProvider value={ true }>
<div className="editor-block-list__layout">
{ map( blockClientIds, ( clientId, blockIndex ) => (
<BlockListBlock
key={ 'block-' + clientId }
index={ blockIndex }
clientId={ clientId }
blockRef={ this.setBlockRef }
onSelectionStart={ this.onSelectionStart }
rootClientId={ rootClientId }
isFirst={ blockIndex === 0 }
isLast={ blockIndex === blockClientIds.length - 1 }
isDraggable={ isDraggable }
/>
) ) }
<BlockListAppender rootClientId={ rootClientId } />
</div>
</AsyncModeProvider>
);
}
}
Expand Down

0 comments on commit 8b9d156

Please sign in to comment.