當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript axios.CancelToken類代碼示例

本文整理匯總了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
      )
  };
};
開發者ID:linode,項目名稱:manager,代碼行數:25,代碼來源:index.ts

示例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
    }
  }
開發者ID:JiarongGu,項目名稱:ReactCoreTemplate,代碼行數:13,代碼來源:HttpClient.ts

示例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;
            }
        }
    });
開發者ID:shuntksh,項目名稱:binaryscanr,代碼行數:37,代碼來源:exampleHandler.ts

示例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) });
        }
    });
};
開發者ID:terranodo,項目名稱:eventkit-cloud,代碼行數:84,代碼來源:middlewares.ts


注:本文中的axios.CancelToken類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。