Skip to content

Commit 076e53d

Browse files
Simplify WebViewMessageStream
1 parent e69a2fc commit 076e53d

File tree

3 files changed

+12
-26
lines changed

3 files changed

+12
-26
lines changed

packages/snaps-controllers/src/services/webview/WebViewExecutionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class WebViewExecutionService extends AbstractExecutionService<string> {
4545
const stream = new WebViewMessageStream({
4646
name: 'parent',
4747
target: 'child',
48-
getWebView: async () => webView,
48+
webView,
4949
});
5050

5151
return { worker: jobId, stream };

packages/snaps-controllers/src/services/webview/WebViewMessageStream.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { PostMessageEvent } from '@metamask/post-message-stream';
22
import { BasePostMessageStream } from '@metamask/post-message-stream';
33
import { isValidStreamMessage } from '@metamask/post-message-stream/dist/utils';
4-
import { logError } from '@metamask/snaps-utils';
54
import { assert, stringToBytes } from '@metamask/utils';
65

76
export type WebViewInterface = {
@@ -13,7 +12,7 @@ export type WebViewInterface = {
1312
export type WebViewStreamArgs = {
1413
name: string;
1514
target: string;
16-
getWebView: () => Promise<WebViewInterface>;
15+
webView: WebViewInterface;
1716
};
1817

1918
/**
@@ -33,29 +32,21 @@ export class WebViewMessageStream extends BasePostMessageStream {
3332
* @param args.name - The name of the stream. Used to differentiate between
3433
* multiple streams sharing the same window object.
3534
* @param args.target - The name of the stream to exchange messages with.
36-
* @param args.getWebView - A asynchronous getter for the webview.
35+
* @param args.webView - A reference to the WebView.
3736
*/
38-
constructor({ name, target, getWebView }: WebViewStreamArgs) {
37+
constructor({ name, target, webView }: WebViewStreamArgs) {
3938
super();
4039

4140
this.#name = name;
4241
this.#target = target;
4342

4443
this._onMessage = this._onMessage.bind(this);
4544

46-
// This is a bit atypical from other post-message streams.
47-
// We have to wait for the WebView to fully load before we can continue using the stream.
48-
getWebView()
49-
.then((webView) => {
50-
this.#webView = webView;
51-
// This method is already bound.
52-
// eslint-disable-next-line @typescript-eslint/unbound-method
53-
webView.registerMessageListener(this._onMessage);
54-
this._handshake();
55-
})
56-
.catch((error) => {
57-
logError(error);
58-
});
45+
this.#webView = webView;
46+
// This method is already bound.
47+
// eslint-disable-next-line @typescript-eslint/unbound-method
48+
this.#webView.registerMessageListener(this._onMessage);
49+
this._handshake();
5950
}
6051

6152
protected _postMessage(data: unknown): void {

packages/snaps-controllers/src/test-utils/webview.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function parseInjectedJS(js: string) {
1717
/**
1818
* Takes no param and return mocks necessary for testing WebViewMessageStream.
1919
*
20-
* @returns The mockWebView, mockGetWebView, and mockStream.
20+
* @returns The mockWebView, and mockStream.
2121
*/
2222
export function createWebViewObjects() {
2323
const registerMessageListenerA = jest.fn();
@@ -45,26 +45,21 @@ export function createWebViewObjects() {
4545
}),
4646
};
4747

48-
const mockGetWebViewA = jest.fn().mockResolvedValue(mockWebViewA);
49-
const mockGetWebViewB = jest.fn().mockResolvedValue(mockWebViewB);
50-
5148
const streamA = new WebViewMessageStream({
5249
name: 'a',
5350
target: 'b',
54-
getWebView: mockGetWebViewA,
51+
webView: mockWebViewA,
5552
});
5653

5754
const streamB = new WebViewMessageStream({
5855
name: 'b',
5956
target: 'a',
60-
getWebView: mockGetWebViewB,
57+
webView: mockWebViewB,
6158
});
6259

6360
return {
6461
mockWebViewA,
6562
mockWebViewB,
66-
mockGetWebViewA,
67-
mockGetWebViewB,
6863
streamA,
6964
streamB,
7065
};

0 commit comments

Comments
 (0)