当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript CancelToken.source方法代码示例

本文整理汇总了TypeScript中axios.CancelToken.source方法的典型用法代码示例。如果您正苦于以下问题:TypeScript CancelToken.source方法的具体用法?TypeScript CancelToken.source怎么用?TypeScript CancelToken.source使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在axios.CancelToken的用法示例。


在下文中一共展示了CancelToken.source方法的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.source方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。