-
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.
Prior work in #19060 TL;DR p4TIVU-86D-p2 This is a read-only abstraction built upon the data layer to make it easy to add "shove it" kinds of data into Calypso from an API call without needing to write reducers, query components, data layer handlers, and other common snippets traditionally required for new data sources. Do you have a reducer that does nothing more than store data opaquely on a single action type? Do you have a query component you don't think you need? This is for you
- Loading branch information
Showing
7 changed files
with
184 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** @format */ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { HTTP_DATA_REQUEST, HTTP_DATA_TICK } from 'state/action-types'; | ||
|
||
export const reducer = ( state = 0, { type } ) => ( HTTP_DATA_TICK === type ? state + 1 : state ); | ||
|
||
export const httpData = new Map(); | ||
|
||
export const update = ( id, state, data ) => { | ||
const lastUpdated = Date.now(); | ||
const item = httpData.get( id ); | ||
const hasItem = item !== undefined; | ||
|
||
// We could have left out the keys for | ||
// the previous properties if they didn't | ||
// exist but I wanted to make sure we can | ||
// get our hidden classes to optimize here. | ||
switch ( state ) { | ||
case 'failure': | ||
return httpData.set( id, { | ||
state, | ||
data: hasItem ? item.data : undefined, | ||
error: data, | ||
lastUpdated: hasItem ? item.lastUpdated : -Infinity, | ||
pendingSince: undefined, | ||
} ); | ||
|
||
case 'pending': | ||
return httpData.set( id, { | ||
state, | ||
data: hasItem ? item.data : undefined, | ||
error: undefined, | ||
lastUpdated: hasItem ? item.lastUpdated : -Infinity, | ||
pendingSince: lastUpdated, | ||
} ); | ||
|
||
case 'success': | ||
return httpData.set( id, { | ||
state, | ||
data, | ||
error: undefined, | ||
lastUpdated, | ||
pendingSince: undefined, | ||
} ); | ||
} | ||
}; | ||
|
||
const empty = Object.freeze( { | ||
state: 'uninitialized', | ||
data: undefined, | ||
error: undefined, | ||
lastUpdated: -Infinity, | ||
pendingSince: undefined, | ||
} ); | ||
|
||
let dispatch; | ||
|
||
export const enhancer = next => ( ...args ) => { | ||
const store = next( ...args ); | ||
|
||
dispatch = store.dispatch; | ||
|
||
return store; | ||
}; | ||
|
||
export const getHttpData = id => httpData.get( id ) || empty; | ||
|
||
export const requestHttpData = ( id, fetchAction, { fromApi, freshness } ) => { | ||
const data = getHttpData( id ); | ||
const { state, lastUpdated } = data; | ||
|
||
if ( | ||
'uninitialized' === state || | ||
( 'number' === typeof freshness && 'pending' !== state && Date.now() - lastUpdated > freshness ) | ||
) { | ||
if ( 'development' === process.env.NODE_ENV && 'function' !== typeof dispatch ) { | ||
throw new Error( 'Cannot use HTTP data without injecting Redux store enhancer!' ); | ||
} | ||
|
||
dispatch( { type: HTTP_DATA_REQUEST, id, fetch: fetchAction, fromApi } ); | ||
} | ||
|
||
return data; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** @format */ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { HTTP_DATA_REQUEST, HTTP_DATA_TICK } from 'state/action-types'; | ||
import { dispatchRequestEx } from 'state/data-layer/wpcom-http/utils'; | ||
import { update } from './common'; | ||
|
||
const fetch = action => { | ||
update( action.id, 'pending' ); | ||
|
||
return [ | ||
{ | ||
...action.fetch, | ||
onSuccess: action, | ||
onFailure: action, | ||
onProgress: action, | ||
}, | ||
{ type: HTTP_DATA_TICK }, | ||
]; | ||
}; | ||
|
||
const onError = ( action, error ) => { | ||
update( action.id, 'failure', error ); | ||
|
||
return { type: HTTP_DATA_TICK }; | ||
}; | ||
|
||
/** | ||
* Transforms API response data into storable data | ||
* Returns pairs of data ids and data plus an error indicator | ||
* | ||
* [ error?, [ [ id, data ], [ id, data ], … ] ] | ||
* | ||
* @example: | ||
* --input-- | ||
* { data: { sites: { | ||
* 14: { is_active: true, name: 'foo' }, | ||
* 19: { is_active: false, name: 'bar' } | ||
* } } } | ||
* | ||
* --output-- | ||
* [ [ 'site-names-14', 'foo' ] ] | ||
* | ||
* @param {*} data input data from API response | ||
* @param {function} fromApi transforms API response data | ||
* @return {Array<boolean, Array<Array<string, *>>>} output data to store | ||
*/ | ||
const parseResponse = ( data, fromApi ) => { | ||
try { | ||
return [ undefined, fromApi( data ) ]; | ||
} catch ( error ) { | ||
return [ error, undefined ]; | ||
} | ||
}; | ||
|
||
const onSuccess = ( action, apiData ) => { | ||
const [ error, data ] = | ||
'function' === typeof apiData ? parseResponse( apiData, action.fromApi ) : [ undefined, [] ]; | ||
|
||
if ( undefined !== error ) { | ||
return onError( action, error ); | ||
} | ||
|
||
update( action.id, 'success', apiData ); | ||
data.forEach( ( [ id, resource ] ) => update( id, 'success', resource ) ); | ||
|
||
return { type: HTTP_DATA_TICK }; | ||
}; | ||
|
||
export default { | ||
[ HTTP_DATA_REQUEST ]: [ | ||
dispatchRequestEx( { | ||
fetch, | ||
onSuccess, | ||
onError, | ||
} ), | ||
], | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** @format */ | ||
|
||
export const readerTags = () => 'reader-tags'; | ||
|
||
// for example… | ||
export const post = ( siteId, postId ) => `post-${ siteId }-${ postId }`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/** @format */ | ||
|
||
export { getHttpData as default } from 'state/data-layer/http-data/common'; |