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


TypeScript ClientRequest.setTimeout方法代码示例

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


在下文中一共展示了ClientRequest.setTimeout方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: default

export default (request: ClientRequest, delays: Delays, options: any) => {
	/* istanbul ignore next: this makes sure timed-out isn't called twice */
	if (Reflect.has(request, reentry)) {
		return noop;
	}

	(request as any)[reentry] = true;

	let stopNewTimeouts = false;

	const addTimeout = (delay: number, callback: (...args: any) => void, ...args: any): (() => void) => {
		// An error had been thrown before. Going further would result in uncaught errors.
		// See https://github.com/sindresorhus/got/issues/631#issuecomment-435675051
		if (stopNewTimeouts) {
			return noop;
		}

		// Event loop order is timers, poll, immediates.
		// The timed event may emit during the current tick poll phase, so
		// defer calling the handler until the poll phase completes.
		let immediate: NodeJS.Immediate;
		const timeout: NodeJS.Timeout = setTimeout(() => {
			immediate = setImmediate(callback, delay, ...args);
			/* istanbul ignore next: added in node v9.7.0 */
			if (immediate.unref) {
				immediate.unref();
			}
		}, delay);

		/* istanbul ignore next: in order to support electron renderer */
		if (timeout.unref) {
			timeout.unref();
		}

		const cancel = (): void => {
			clearTimeout(timeout);
			clearImmediate(immediate);
		};

		cancelers.push(cancel);

		return cancel;
	};

	const {host, hostname} = options;
	const timeoutHandler = (delay: number, event: string): void => {
		request.emit('error', new TimeoutError(delay, event));
		request.abort();
	};

	const cancelers: Array<() => void> = [];
	const cancelTimeouts = (): void => {
		stopNewTimeouts = true;
		cancelers.forEach(cancelTimeout => cancelTimeout());
	};

	request.on('error', (error: Error): void => {
		if (error.message !== 'socket hang up') {
			cancelTimeouts();
		}
	});
	request.once('response', response => {
		response.once('end', cancelTimeouts);
	});

	if (delays.request !== undefined) {
		addTimeout(delays.request, timeoutHandler, 'request');
	}

	if (delays.socket !== undefined) {
		const socketTimeoutHandler = (): void => {
			timeoutHandler(delays.socket!, 'socket');
		};

		request.setTimeout(delays.socket, socketTimeoutHandler);

		// `request.setTimeout(0)` causes a memory leak.
		// We can just remove the listener and forget about the timer - it's unreffed.
		// See https://github.com/sindresorhus/got/issues/690
		cancelers.push((): void => {
			request.removeListener('timeout', socketTimeoutHandler);
		});
	}

	request.once('socket', (socket: net.Socket): void => {
		const {socketPath} = request as any;

		/* istanbul ignore next: hard to test */
		if (socket.connecting) {
			if (delays.lookup !== undefined && !socketPath && !net.isIP(hostname || host)) {
				const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
				socket.once('lookup', cancelTimeout);
			}

			if (delays.connect !== undefined) {
				const timeConnect = () => addTimeout(delays.connect!, timeoutHandler, 'connect');

				if (socketPath || net.isIP(hostname || host)) {
					socket.once('connect', timeConnect());
				} else {
//.........这里部分代码省略.........
开发者ID:sindresorhus,项目名称:got,代码行数:101,代码来源:timed-out.ts


注:本文中的http.ClientRequest.setTimeout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。