Skip to content

Commit 22437fd

Browse files
committed
Added missing abort signals for some ElementEndpoint methods
1 parent ff99969 commit 22437fd

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

endpoints/generic/ElementEndpoint.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ export class ElementEndpoint<TEntity> extends ETagEndpointBase {
4040

4141
/**
4242
* Determines whether the element currently exists.
43+
* @param signal Used to cancel the request.
4344
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
4445
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
4546
* @throws {@link HttpError}: Other non-success status code
4647
*/
47-
async exists() {
48-
const response = await this.httpClient.send(this.uri, HttpMethod.Head);
48+
async exists(signal?: AbortSignal) {
49+
const response = await this.httpClient.send(this.uri, HttpMethod.Head, signal);
4950
if (response.ok) return true;
5051
if (response.status === HttpStatusCode.NotFound || response.status === HttpStatusCode.Gone) return false;
5152

@@ -63,6 +64,7 @@ export class ElementEndpoint<TEntity> extends ETagEndpointBase {
6364
/**
6465
* Sets/replaces the `TEntity`.
6566
* @param entity The new `TEntity`.
67+
* @param signal Used to cancel the request.
6668
* @returns The `TEntity` as returned by the server, possibly with additional fields set. undefined if the server does not respond with a result entity.
6769
* @throws {@link ConcurrencyError}: The entity has changed since it was last retrieved with {@link read}. Your changes were rejected to prevent a lost update.
6870
* @throws {@link BadRequestError}: {@link HttpStatusCode.BadRequest}
@@ -71,8 +73,8 @@ export class ElementEndpoint<TEntity> extends ETagEndpointBase {
7173
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
7274
* @throws {@link HttpError}: Other non-success status code
7375
*/
74-
async set(entity: TEntity): Promise<(TEntity | undefined)> {
75-
const response = await this.putContent(entity);
76+
async set(entity: TEntity, signal?: AbortSignal): Promise<(TEntity | undefined)> {
77+
const response = await this.putContent(entity, signal);
7678
const text = await response.text();
7779
if (text) {
7880
return this.serializer.deserialize<TEntity>(text);
@@ -114,6 +116,7 @@ export class ElementEndpoint<TEntity> extends ETagEndpointBase {
114116
* Reads the current state of the entity, applies a change to it and stores the result. Applies optimistic concurrency using automatic retries.
115117
* @param updateAction A callback that takes the current state of the entity and applies the desired modifications.
116118
* @param maxRetries The maximum number of retries to perform for optimistic concurrency before giving up.
119+
* @param signal Used to cancel the request.
117120
* @returns The `TEntity` as returned by the server, possibly with additional fields set. undefined if the server does not respond with a result entity.
118121
* @throws {@link ConcurrencyError}: The maximum number of retries to perform for optimistic concurrency before giving up.
119122
* @throws {@link BadRequestError}: {@link HttpStatusCode.BadRequest}
@@ -122,13 +125,13 @@ export class ElementEndpoint<TEntity> extends ETagEndpointBase {
122125
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
123126
* @throws {@link HttpError}: Other non-success status code
124127
*/
125-
async update(updateAction: (entity: TEntity) => void, maxRetries: number = 3): Promise<(TEntity | undefined)> {
128+
async update(updateAction: (entity: TEntity) => void, maxRetries: number = 3, signal?: AbortSignal): Promise<(TEntity | undefined)> {
126129
let retryCounter = 0;
127130
while (true) {
128-
const entity = await this.read();
131+
const entity = await this.read(signal);
129132
updateAction(entity);
130133
try {
131-
return await this.set(entity);
134+
return await this.set(entity, signal);
132135
} catch (err) {
133136
if (retryCounter++ >= maxRetries || !(err instanceof ConcurrencyError))
134137
throw err;
@@ -145,11 +148,14 @@ export class ElementEndpoint<TEntity> extends ETagEndpointBase {
145148

146149
/**
147150
* Deletes the element.
151+
* @param signal Used to cancel the request.
148152
* @throws {@link ConcurrencyError}: The entity has changed since it was last retrieved with {@link read}. Your changes were rejected to prevent a lost update.
149153
* @throws {@link AuthenticationError}: {@link HttpStatusCode.Unauthorized}
150154
* @throws {@link AuthorizationError}: {@link HttpStatusCode.Forbidden}
151155
* @throws {@link NotFoundError}: {@link HttpStatusCode.NotFound} or {@link HttpStatusCode.Gone}
152156
* @throws {@link HttpError}: Other non-success status code
153157
*/
154-
async delete() { await this.deleteContent(); }
158+
async delete(signal?: AbortSignal) {
159+
await this.deleteContent(signal);
160+
}
155161
}

0 commit comments

Comments
 (0)