Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(javascript): type setTimeout in a way compatible with node & browser #527

Merged
merged 1 commit into from
May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type {
Response,
} from '@experimental-api-clients-automation/client-common';

type Timeout = ReturnType<typeof setTimeout>;

export function createXhrRequester(): Requester {
function send(request: EndRequest): Promise<Response> {
return new Promise((resolve) => {
Expand All @@ -14,10 +16,7 @@ export function createXhrRequester(): Requester {
baseRequester.setRequestHeader(key, request.headers[key])
);

const createTimeout = (
timeout: number,
content: string
): NodeJS.Timeout => {
const createTimeout = (timeout: number, content: string): Timeout => {
return setTimeout(() => {
baseRequester.abort();

Expand All @@ -34,7 +33,7 @@ export function createXhrRequester(): Requester {
'Connection timeout'
);

let responseTimeout: NodeJS.Timeout | undefined;
let responseTimeout: Timeout | undefined;

baseRequester.onreadystatechange = (): void => {
if (
Expand All @@ -54,7 +53,7 @@ export function createXhrRequester(): Requester {
// istanbul ignore next
if (baseRequester.status === 0) {
clearTimeout(connectTimeout);
clearTimeout(responseTimeout as NodeJS.Timeout);
clearTimeout(responseTimeout!);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a if to avoid having !?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not actually needed, clearTimeout accepts null and undefined too, just type types don't allow that


resolve({
content: baseRequester.responseText || 'Network request failed',
Expand All @@ -66,7 +65,7 @@ export function createXhrRequester(): Requester {

baseRequester.onload = (): void => {
clearTimeout(connectTimeout);
clearTimeout(responseTimeout as NodeJS.Timeout);
clearTimeout(responseTimeout!);

resolve({
content: baseRequester.responseText,
Expand Down