Skip to content

Commit

Permalink
Notify when using response async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 1, 2023
1 parent 3f2a268 commit 57996bc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,16 @@ describe('BaseSnapExecutor', () => {
method: 'OutboundResponse',
});

expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
});

expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
});

expect(await executor.readCommand()).toStrictEqual({
id: 2,
jsonrpc: '2.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Network endowments', () => {
expect(factoryOptions.notify).toHaveBeenCalledWith({
method: 'OutboundResponse',
});
expect(factoryOptions.notify).toHaveBeenCalledTimes(2);
expect(factoryOptions.notify).toHaveBeenCalledTimes(4);
});

it('can use AbortController normally', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ class ResponseWrapper implements Response {

#ogResponse: Response;

constructor(ogResponse: Response, teardownRef: { lastTeardown: number }) {
#onStart: () => void;

#onFinish: () => void;

constructor(
ogResponse: Response,
teardownRef: { lastTeardown: number },
onStart: () => void,
onFinish: () => void,
) {
this.#ogResponse = ogResponse;
this.#teardownRef = teardownRef;
this.#onStart = onStart;
this.#onFinish = onFinish;
}

get body(): ReadableStream<Uint8Array> | null {
Expand Down Expand Up @@ -54,31 +65,64 @@ class ResponseWrapper implements Response {
}

async text() {
return withTeardown<string>(this.#ogResponse.text(), this as any);
this.#onStart();
try {
return await withTeardown<string>(this.#ogResponse.text(), this as any);
} finally {
this.#onFinish();
}
}

async arrayBuffer(): Promise<ArrayBuffer> {
return withTeardown<ArrayBuffer>(
this.#ogResponse.arrayBuffer(),
this as any,
);
this.#onStart();
try {
return await withTeardown<ArrayBuffer>(
this.#ogResponse.arrayBuffer(),
this as any,
);
} finally {
this.#onFinish();
}
}

async blob(): Promise<Blob> {
return withTeardown<Blob>(this.#ogResponse.blob(), this as any);
this.#onStart();
try {
return await withTeardown<Blob>(this.#ogResponse.blob(), this as any);
} finally {
this.#onFinish();
}
}

clone(): Response {
const newResponse = this.#ogResponse.clone();
return new ResponseWrapper(newResponse, this.#teardownRef);
return new ResponseWrapper(
newResponse,
this.#teardownRef,
this.#onStart,
this.#onFinish,
);
}

async formData(): Promise<FormData> {
return withTeardown<FormData>(this.#ogResponse.formData(), this as any);
this.#onStart();
try {
return await withTeardown<FormData>(
this.#ogResponse.formData(),
this as any,
);
} finally {
this.#onFinish();
}
}

async json(): Promise<any> {
return withTeardown(this.#ogResponse.json(), this as any);
this.#onStart();
try {
return await withTeardown(this.#ogResponse.json(), this as any);
} finally {
this.#onFinish();
}
}
}

Expand Down Expand Up @@ -127,6 +171,22 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => {
);
}

let started = false;
const onStart = () => {
if (!started) {
started = true;
notify({ method: 'OutboundRequest' });
}
};

let finished = false;
const onFinish = () => {
if (!finished) {
finished = true;
notify({ method: 'OutboundResponse' });
}
};

let res: Response;
let openFetchConnection: { cancel: () => Promise<void> } | undefined;
try {
Expand All @@ -151,6 +211,8 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => {
res = new ResponseWrapper(
await withTeardown(fetchPromise, teardownRef),
teardownRef,
onStart,
onFinish,
);
} finally {
if (openFetchConnection !== undefined) {
Expand Down

0 comments on commit 57996bc

Please sign in to comment.