This function create a new data loader from loaderDescriptor.
const exampleLoader = createLoader('ACTION_TYPE_REQUEST',{
shouldFetch: (context) => {
// ...
},
fetch: (context) => {
// ...
},
loading: (context) => {
// ...
},
success: (context, result) => {
},
error: (context, err) => {
// ...
}
}, {
ttl: 10000,
retryTimes: 3,
retryWait: fixedWait(500),
})
When an action that matches pattern is dispatched, the created loader will start loading data.
(required)
pattern
uses the following rules:
- If the pattern is an
object
,action
is matched by_.isEqual(action, pattern
) withlodash
. - If the pattern is a
function
,action
is matched bypattern(action) === true
. - Otherwise,
action
is matched byaction.type === pattern
.
(required)
loaderDescriptor
is an object contains several functions. It MAY contains the following keys:
shouldFetch
:function optionalloading
: function optionalfetch
: function requiredsuccess
: function requirederror
: function required
(optional)
This function is called first. If it returns true
, this data loader will do nothing.
(required)
This function is used to fetch data. It is only called if shouldFetch()
returns true
.
(required)
Must returns an action. Called whenever a value resolves.
(required)
Must returns an action. Called whenever a value rejects.
(optional)
Must returns an action. Called before fetching data.
functions
will be called in this order:
##### shouldFetch -> loading -> fetch -> success or error
context
usually contains
dispatch
,getState
action
- (other variables provided by middleware)
dispatch
and getState
are functions of redux store.
action
is original dispatched action. It usually contains parameters for fetching data.
You can use createDataLoaderMiddleware(loaders, args)
to add extra objects to context.
(optional)
options
provides following optional values:
ttl
: Provides a value in millisecond to cache the loader in order to prevent duplicated requests. (default: 10000, n/a to server side applications)retryTimes
: Total try times when fetching failed. (default: 1)retryWait
: Wait stategy that sleeps before retrying: (default: fixedWait(0)), see wait-strategies.js for detail.
Returns a redux middleware.
(required)
An array of created data loaders.
(_optional)
All the args(key-value) will be added to context.
Returns a Promise
.
Wrap an action and returns a Promise
that can be processed by dataLoaderMiddleware.