Skip to content

Commit 7c730eb

Browse files
authored
RAI-10177 add abort signal pollTransaction (#83)
* RAI-10177 add abort signal to poll transaction api * add abort signal to all get apis * add abort signal to all apis * fix tests * revert * refactor * refactor * export AbortError in index
1 parent 3e290ef commit 7c730eb

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

index.web.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ export * from './src/api/transaction/types';
2323
export * from './src/api/user/types';
2424
export * from './src/credentials';
2525
export type { SdkError } from './src/errors';
26-
export { ApiError, MaxRelationSizeError, TransactionError } from './src/errors';
26+
export {
27+
AbortError,
28+
ApiError,
29+
MaxRelationSizeError,
30+
TransactionError,
31+
} from './src/errors';
2732
export * from './src/proto/generated/message';
2833
export * from './src/proto/generated/schema';
2934
export * from './src/results/resultTable';

src/errors.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,20 @@ export class MaxRelationSizeError extends Error {
8282
}
8383
}
8484

85+
export class AbortError extends Error {
86+
constructor(message?: string) {
87+
super(message);
88+
89+
this.name = 'AbortError';
90+
}
91+
92+
toString() {
93+
return this.message ?? this.name;
94+
}
95+
}
96+
8597
export type SdkError =
98+
| AbortError
8699
| ApiError
87100
| TransactionError
88101
| MaxRelationSizeError

src/rest.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { stringify } from 'query-string';
1818

19-
import { makeError } from './errors';
19+
import { AbortError, makeError } from './errors';
2020
import { getFetch, Response } from './fetch.node';
2121
import { ApiResponse, VERSION } from './types';
2222

@@ -37,6 +37,7 @@ export type PollOptions = {
3737
overheadRate?: number;
3838
maxInterval?: number;
3939
timeout?: number;
40+
signal?: AbortSignal;
4041
};
4142

4243
export type PollingResult<T> = {
@@ -173,6 +174,11 @@ export async function pollWithOverhead<T = void>(
173174
return new Promise<T>((resolve, reject) => {
174175
const poll = (delay: number) => {
175176
setTimeout(async () => {
177+
if (options?.signal?.aborted) {
178+
reject(new AbortError());
179+
return;
180+
}
181+
176182
try {
177183
const pollingResult = await callback();
178184
if (pollingResult.done && pollingResult.result) {

0 commit comments

Comments
 (0)