Skip to content

Commit

Permalink
Use AbirtSignal.timeout() instead of promise.race()
Browse files Browse the repository at this point in the history
  • Loading branch information
oBusk committed May 22, 2024
1 parent 15f8282 commit 6dc4fc4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 32 deletions.
6 changes: 0 additions & 6 deletions src/app/[...parts]/_page/BundlephobiaDiff.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bundlephobia from "^/lib/api/bundlephobia";
import TIMED_OUT from "^/lib/api/TimedOut";
import { Bundlephobia } from "^/lib/Services";
import type SimplePackageSpec from "^/lib/SimplePackageSpec";
import suspense from "^/lib/suspense";
Expand Down Expand Up @@ -29,11 +28,6 @@ const BundlephobiaDiffInner = async ({
return null;
}

if (result === TIMED_OUT) {
console.warn(`${name} timed out`, { specs });
return null;
}

console.log(name, { specs, time });

return (
Expand Down
6 changes: 0 additions & 6 deletions src/app/[...parts]/_page/PackagephobiaDiff.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import packagephobia from "^/lib/api/packagephobia";
import TIMED_OUT from "^/lib/api/TimedOut";
import { Packagephobia } from "^/lib/Services";
import type SimplePackageSpec from "^/lib/SimplePackageSpec";
import suspense from "^/lib/suspense";
Expand All @@ -26,11 +25,6 @@ const PackagephobiaDiffInner = async ({
return null;
}

if (result === TIMED_OUT) {
console.warn(`${name} timed out`, { specs });
return null;
}

console.log(name, { specs, time });

return (
Expand Down
12 changes: 0 additions & 12 deletions src/lib/api/TimedOut.ts

This file was deleted.

9 changes: 5 additions & 4 deletions src/lib/api/bundlephobia/bundlephobia.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import npa from "npm-package-arg";
import type TIMED_OUT from "../TimedOut";
import { resultOrTimedOut } from "../TimedOut";
import type BundlephobiaResponse from "./BundlephobiaResponse";
import type BundlephobiaResults from "./BundlephobiaResults";

Expand All @@ -14,6 +12,9 @@ async function getPackage(spec: string): Promise<BundlephobiaResponse | null> {
try {
const response = await fetch(
`https://bundlephobia.com/api/size?package=${spec}`,
{
signal: AbortSignal.timeout(7_500),
},
);

if (response.status !== 200) {
Expand Down Expand Up @@ -41,6 +42,6 @@ async function getPackages(
export default async function bundlephobia([aSpec, bSpec]: [
string,
string,
]): Promise<BundlephobiaResults | null | TIMED_OUT> {
return resultOrTimedOut(getPackages(aSpec, bSpec));
]): Promise<BundlephobiaResults | null> {
return getPackages(aSpec, bSpec);
}
9 changes: 5 additions & 4 deletions src/lib/api/packagephobia/packagephobia.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type TIMED_OUT from "../TimedOut";
import { resultOrTimedOut } from "../TimedOut";
import type PackagephobiaResponse from "./PackagephobiaResponse";
import type PackagephobiaResults from "./PackagephobiaResult";

async function getPackage(spec: string): Promise<PackagephobiaResponse | null> {
try {
const response = await fetch(
`https://packagephobia.com/v2/api.json?p=${spec}`,
{
signal: AbortSignal.timeout(7_500),
},
);

if (response.status !== 200) {
Expand Down Expand Up @@ -35,6 +36,6 @@ async function getPackages(
export default async function packagephobia([aSpec, bSpec]: [
string,
string,
]): Promise<PackagephobiaResults | null | TIMED_OUT> {
return resultOrTimedOut(getPackages(aSpec, bSpec));
]): Promise<PackagephobiaResults | null> {
return getPackages(aSpec, bSpec);
}

0 comments on commit 6dc4fc4

Please sign in to comment.