README
Call Gate
The call gate allows components throughout an application to trigger actions which make HTTP requests without having to worry about managing request state or request duplication. For instance, if 10 different components in your application need "account" data, they can all request it at mount time, but only a single request will be issued.
This enabled through two mechanisms:
- Lock in-progress state
- Lock duration
If a lock is "in progress" with a specific name, all subsequent calls will be no-op until the lock is no longer in progress.
If a lock has a duration, all subsequent calls will be no-op until the duration expires. This is especially useful in cases where data is very unlikely to change.
Example
import CallGate from 'lib/call-gate';
import { duration } from 'moment';
import { getAccounts } from '../apis/account';
import { account as types } from 'lib/constants/action-types';
const gate = new CallGate();
export function accountsRequested() {
return (dispatch, getState) => {
const lock = gate.createLock('accountsRequested', () => {
dispatch({ type: types.ACCOUNTS_REQUESTED });
getAccounts(getState().auth, (error, response, body) => {
// Mark the current locked call as done:
lock.done('accountsRequested');
if (error) {
dispatch(accountsRequestFailed(error));
} else {
const accounts = JSON.parse(response.body);
dispatch(accountsRequestReceived(accounts));
}
});
}, duration(30, 'seconds'));
};
};
Build
To compile this library for release, run npm run build
.
Test
Tests are run with mocha
via npm test
.