Skip to content

Commit 7cffbee

Browse files
perf: Change request notification order (#3381)
Moved some of the ideas from #3356 to this PR which will be a simple to merge right away. The other referenced PR needs some more work, but is eventually what we should do. This PR let's Snaps start their outbound request before waiting for the notification to land, this should be a small perf improvement to all requests made from the Snap.
1 parent 93a31e2 commit 7cffbee

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

packages/snaps-controllers/src/snaps/SnapController.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ describe('SnapController', () => {
18021802
});
18031803

18041804
const { rootMessenger } = options;
1805-
const [snapController, service] = getSnapControllerWithEES(options);
1805+
const [snapController] = getSnapControllerWithEES(options);
18061806
const snap = snapController.getExpect(MOCK_SNAP_ID);
18071807

18081808
rootMessenger.registerActionHandler(
@@ -1845,7 +1845,6 @@ describe('SnapController', () => {
18451845
);
18461846

18471847
snapController.destroy();
1848-
await service.terminateAllSnaps();
18491848
});
18501849

18511850
// This isn't stable in CI unfortunately
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"branches": 90,
33
"functions": 94.57,
4-
"lines": 90.13,
5-
"statements": 89.5
4+
"lines": 90.15,
5+
"statements": 89.52
66
}

packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,15 @@ export class BaseSnapExecutor {
531531
assertSnapOutboundRequest(sanitizedArgs);
532532
return await withTeardown(
533533
(async () => {
534-
await this.#notify({
535-
method: 'OutboundRequest',
536-
params: { source: 'snap.request' },
537-
});
538534
try {
539-
return await originalRequest(sanitizedArgs);
535+
const promise = originalRequest(sanitizedArgs);
536+
537+
await this.#notify({
538+
method: 'OutboundRequest',
539+
params: { source: 'snap.request' },
540+
});
541+
542+
return await promise;
540543
} finally {
541544
await this.#notify({
542545
method: 'OutboundResponse',
@@ -573,12 +576,15 @@ export class BaseSnapExecutor {
573576
assertEthereumOutboundRequest(sanitizedArgs);
574577
return await withTeardown(
575578
(async () => {
576-
await this.#notify({
577-
method: 'OutboundRequest',
578-
params: { source: 'ethereum.request' },
579-
});
580579
try {
581-
return await originalRequest(sanitizedArgs);
580+
const promise = originalRequest(sanitizedArgs);
581+
582+
await this.#notify({
583+
method: 'OutboundRequest',
584+
params: { source: 'ethereum.request' },
585+
});
586+
587+
return await promise;
582588
} finally {
583589
await this.#notify({
584590
method: 'OutboundResponse',

packages/snaps-execution-environments/src/common/endowments/network.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,16 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => {
226226
return await withTeardown(
227227
(async () => {
228228
try {
229-
await notify({
230-
method: 'OutboundRequest',
231-
params: { source: 'fetch' },
232-
});
233229
const fetchPromise = fetch(input, {
234230
...init,
235231
signal: abortController.signal,
236232
});
237233

234+
await notify({
235+
method: 'OutboundRequest',
236+
params: { source: 'fetch' },
237+
});
238+
238239
openFetchConnection = {
239240
cancel: async () => {
240241
abortController.abort();

0 commit comments

Comments
 (0)