本文整理汇总了TypeScript中axios.CancelToken类的典型用法代码示例。如果您正苦于以下问题:TypeScript CancelToken类的具体用法?TypeScript CancelToken怎么用?TypeScript CancelToken使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CancelToken类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: reduceRequestConfig
export const CancellableRequest = <T>(
...fns: Function[]
): CancellableRequest<T> => {
const config = reduceRequestConfig(...fns);
const source = Axios.CancelToken.source();
if (config.validationErrors) {
return {
cancel: () => null,
request: () =>
Promise.reject({
config: omit(['validationErrors'], config),
response: { data: { errors: config.validationErrors } }
})
};
}
return {
cancel: source.cancel,
request: () =>
Axios({ ...config, cancelToken: source.token }).then(
response => response.data
)
};
};
示例2: constructor
constructor(config?: AxiosRequestConfig) {
// initalize config if does not supply
this.cancelTokenSource = Axios.CancelToken.source();
const defualtConfig = { cancelToken: this.cancelTokenSource.token };
const axiosConfig = config ? Object.assign({}, config, defualtConfig) : defualtConfig;
this._config = axiosConfig;
this._axios = Axios.create(this._config);
this.interceptors = {
request: this._axios.interceptors.request,
response: this._axios.interceptors.response
}
}
示例3: clearTimeouts
store.subscribe((): void => {
const state = store.getState(); // The state is immutable Map.
const example = state.get("example");
if (!example || example === prevState) {
if (prevState && !example) {
prevState = "";
clearTimeouts(_timerRef);
}
return void 0;
}
if (example && example !== prevState) {
prevState = example;
let value = "";
let filter = "";
if (example === "random") {
const _arr = new Uint32Array(120);
(window || global).crypto.getRandomValues(_arr);
_arr.forEach((item) => value += item.toString(16));
filter = "b32B32H16H16f2x128a32 bin Bin Hex hex float ascii";
_exampleReplayer(value, filter, store);
return void 0;
}
if (examples.indexOf(example) > -1) {
store.dispatch(actions.startLoading());
// Cancel inflight request (if any)
if (_cancelTokens.length) {
cancelRequests();
}
const cancelSource = axios.CancelToken.source();
_cancelTokens.push(cancelSource);
fetchExample(example, cancelSource, store);
return void 0;
}
}
});
示例4: next
export const simpleApiCall = ({ dispatch, getState }) => next => (action) => {
if (Array.isArray(action)) {
// batch action: pass it on
return next(action);
}
const {
types,
auto = false,
getCancelSource = () => undefined,
shouldCallApi = () => true,
requiresAuth = true,
onSuccess = () => ({}),
batchSuccess = () => undefined,
onError = error => ({ error: error.response.data }),
payload = {},
url,
method,
params,
data,
headers = {},
cancellable = false,
} = action;
if (!types) {
// normal action: pass it on
return next(action);
}
if (!Array.isArray(types) || types.length !== 3 || !types.every(type => typeof type === 'string')) {
throw new Error('Expected an array of three string types.');
}
const state = getState();
if (requiresAuth) {
if (!state.user.data) {
throw new Error('Authentication is required for this action');
}
}
if (!shouldCallApi(state)) {
return;
}
const source = getCancelSource(state);
if (!auto && source) {
source.cancel('Request is no longer valid, cancelling.');
}
const cancelSource = cancellable ? axios.CancelToken.source() : undefined;
const token = cancelSource ? cancelSource.token : undefined;
const [requestType, successType, failureType] = types;
dispatch({ ...payload, cancelSource, type: requestType });
const csrftoken = cookie.load('csrftoken');
return axios({
url,
method,
params,
data,
headers: { 'X-CSRFToken': csrftoken, ...headers },
cancelToken: token,
}).then((response) => {
const batched = batchSuccess(response, state);
if (batched) {
dispatch([
{ ...payload, type: successType, ...onSuccess(response) },
...batched,
]);
} else {
dispatch({ ...payload, type: successType, ...onSuccess(response) });
}
}).catch(error => {
if (axios.isCancel(error)) {
console.warn(error.message);
} else {
dispatch({ ...payload, type: failureType, ...onError(error) });
}
});
};