woocommerce/plugins/woocommerce-admin/client/wc-api/wp-data-store/create-api-client.js

58 lines
1.3 KiB
JavaScript

/** @format */
/**
* External dependencies
*/
import { ApiClient } from '@fresh-data/framework';
import { createStore as createReduxStore } from 'redux';
/**
* Internal dependencies
*/
import reducer from './reducer';
function createStore( name ) {
const devTools = window.__REDUX_DEVTOOLS_EXTENSION__;
return createReduxStore( reducer, devTools && devTools( { name: name, instanceId: 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,
time: new Date(),
} );
},
dataReceived: resources => {
store.dispatch( {
type: 'FRESH_DATA_RECEIVED',
resources,
time: new Date(),
} );
},
};
}
function createApiClient( name, apiSpec ) {
const store = createStore( name );
const dataHandlers = createDataHandlers( store );
const apiClient = new ApiClient( apiSpec );
apiClient.setDataHandlers( dataHandlers );
const storeChanged = () => {
apiClient.setState( store.getState() );
};
store.subscribe( storeChanged );
return apiClient;
}
export default createApiClient;