-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data Layer: Track data request meta information (#17263)
* Data Layer: Track data request meta information We often have a legitimate need in a React component to know if data has been requested yet or if requests for that data have failed (though not as often as you might think!) In this patch we're adding in a new system into the `dispatchRequest` wrapper whereby requests are tracked and that meta information is retrievable. Currently we track two properties: what is the current state of a request and when did we last receive an update in Calypso? This tracking is based off of the assumption that we have a correspondance between the `_REQUEST` Redux action and an associated `HTTP_REQUEST` to get it _and_ that we pass in the original action to the call to `http()`. However, if those conditions are met then we can track the request successfully and key it by the original action. That is, suppose we issue `{ type: SITE_REQUEST, siteId }`: Calypso UI code only cares about declaring that the data is needed. How does it know the answers to the following questions? - _Is the data in state?_ - just check state - _Have we attempted to get the data?_ - `getRequestMeta( { type: SITE_REQUEST, siteId } ).status !== 'null'` - _When was our last update?_ - `getRequestMeta( ... ).lastUpdate` - _Was it a failed request?_ - `getRequestMeta( ... ).status === 'failure'` - _Is a request for this data going on right now?_ - `getRequestMeta( ... ).status === 'pending'` This is an attempt to provide wanted information without polluting the Redux state tree. We need to ascertain if the assumptions it makes are safe enough to use; I suspect they are. Expose data request metadata to window in dev environment By exporting the request metadata to `window` in development environments we can interact with it and manually change certain values for testing purposes. For example, we may want to fake a failed request. We can do this by opening the developer console and using `dataRequests.set()` ```js dataRequests.set("'type'='PLANS_REQUEST'", {lastUpdated: Date.now(), status: 'failure'}) ``` Track requests with a unique id Previously we were losing updates to requests when the returned actions were different than the first one which originated the request. Now an additional meta property is added to the actions so that they will carry through and we can track via mapping to the original request. We could have also injected a custom `dispatch()` to guarantee the tracking but this change is simpler and may be safe enough as most cases so far don't use different actions for the HTTP request responders.
- Loading branch information
Showing
4 changed files
with
148 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters