Skip to content

Commit

Permalink
Reject send requests when the transport is stopped (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
robintown authored Aug 15, 2022
1 parent 3efadab commit 8100b5f
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/transport/PostmessageTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ interface IOutboundRequest {
request: IWidgetApiRequest;
resolve: (response: IWidgetApiResponse) => void;
reject: (err: Error) => void;
timerId: number;
}

/**
Expand All @@ -47,7 +46,7 @@ export class PostmessageTransport extends EventEmitter implements ITransport {
private _ready = false;
private _widgetId = null;
private outboundRequests = new Map<string, IOutboundRequest>();
private isStopped = false;
private stopController = new AbortController();

public get ready(): boolean {
return this._ready;
Expand Down Expand Up @@ -118,15 +117,31 @@ export class PostmessageTransport extends EventEmitter implements ITransport {
// TODO: Fix scalar
request['visible'] = data['visible'];
}
return new Promise<R>((prResolve, reject) => {
const timerId = setTimeout(() => {
const req = this.outboundRequests.get(request.requestId);
if (!req) return; // it finished!
return new Promise<R>((prResolve, prReject) => {
const resolve = (response: IWidgetApiResponse) => {
cleanUp();
prResolve(<R>response);
};
const reject = (err: Error) => {
cleanUp();
prReject(err);
};

const timerId = setTimeout(
() => reject(new Error("Request timed out")),
(this.timeoutSeconds || 1) * 1000,
);

const onStop = () => reject(new Error("Transport stopped"));
this.stopController.signal.addEventListener("abort", onStop);

const cleanUp = () => {
this.outboundRequests.delete(request.requestId);
req.reject(new Error("Request timed out"));
}, (this.timeoutSeconds || 1) * 1000);
const resolve = (r: IWidgetApiResponse) => prResolve(<R>r);
this.outboundRequests.set(request.requestId, {request, resolve, reject, timerId});
clearTimeout(timerId);
this.stopController.signal.removeEventListener("abort", onStop);
};

this.outboundRequests.set(request.requestId, { request, resolve, reject });
this.sendInternal(request);
});
}
Expand All @@ -140,11 +155,11 @@ export class PostmessageTransport extends EventEmitter implements ITransport {

public stop() {
this._ready = false;
this.isStopped = true;
this.stopController.abort();
}

private handleMessage(ev: MessageEvent) {
if (this.isStopped) return;
if (this.stopController.signal.aborted) return;
if (!ev.data) return; // invalid event

if (this.strictOriginCheck && ev.origin !== window.origin) return; // bad origin
Expand Down Expand Up @@ -181,8 +196,6 @@ export class PostmessageTransport extends EventEmitter implements ITransport {
const req = this.outboundRequests.get(response.requestId);
if (!req) return; // response to an unknown request

this.outboundRequests.delete(response.requestId);
clearTimeout(req.timerId);
if (isErrorResponse(response.response)) {
const err = <IWidgetApiErrorResponseData>response.response;
req.reject(new Error(err.error.message));
Expand Down

0 comments on commit 8100b5f

Please sign in to comment.