Improve hidden tab suppression/fix rapid requests

This fixes the rapid request problem that was happening when switching
tabs while having timed out requests. It changes the approach from
suppressing the actions sent out to preventing the read function from
being called at all.

I think the original problem has occurred because the original approach
was relying on an internal implementation of apiFetch from wp.data. This
new approach does not have such a dependency. It prevents apiFetch from
even being called. However, in the process, it will also prevent any
newly required resources from being fetched if the user manages to
switch tabs before it is requested. (e.g. refresh, then switch tabs
quickly).
This commit is contained in:
coderkevin 2019-06-10 17:03:08 -05:00
parent 8ed63c5547
commit 78e85f87c3
2 changed files with 5 additions and 5 deletions

View File

@ -36,6 +36,11 @@ function createWcApiSpec() {
},
operations: {
read( resourceNames ) {
if ( document.hidden ) {
// Don't do any read updates while the tab isn't active.
return [];
}
return [
...imports.operations.read( resourceNames ),
...items.operations.read( resourceNames ),

View File

@ -19,11 +19,6 @@ function createStore( name ) {
function createDataHandlers( store ) {
return {
dataRequested: resourceNames => {
const { resources } = store.getState();
const newResources = resourceNames.some( resourceName => ! resources[ resourceName ] );
if ( ! newResources && document.hidden ) {
return;
}
store.dispatch( {
type: 'FRESH_DATA_REQUESTED',
resourceNames,