-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fc2338
commit 25cf88b
Showing
6 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { APIMethodReturn, APIMethods } from "@gramio/types"; | ||
import { TelegramError } from "./errors.ts"; | ||
import type { | ||
MaybeSuppressedReturn, | ||
SuppressedAPIMethodReturn, | ||
} from "./types.ts"; | ||
|
||
// cant use node:timers/promises because possible browser usage... | ||
export const sleep = (ms: number) => | ||
new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
// TODO: improve types. suppress should be required | ||
export async function withRetries< | ||
Result extends Promise<unknown>, | ||
Mode extends "suppress" | "rethrow" = "suppress", | ||
>( | ||
resultPromise: () => Result, | ||
mode: Mode = "suppress" as Mode, | ||
): Promise<Result> { | ||
let result = await resultPromise().catch((error) => error); | ||
|
||
while (result instanceof TelegramError) { | ||
const retryAfter = result.payload?.retry_after; | ||
console.log(result.method, retryAfter); | ||
|
||
if (retryAfter) { | ||
await sleep(retryAfter * 1000); | ||
result = await resultPromise(); | ||
} else { | ||
if (mode === "rethrow") throw result; | ||
|
||
// TODO: hard conditional typings. fix later | ||
// @ts-expect-error | ||
return result; | ||
} | ||
} | ||
|
||
// TODO: find why it miss types. but it works as expected | ||
// @ts-expect-error this is fine | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters