-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f7a5b5
commit 2c1eb44
Showing
3 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-nocheck | ||
import { | ||
BasePostMessageStream, | ||
PostMessageEvent, | ||
} from '@metamask/post-message-stream'; | ||
|
||
interface WindowPostMessageStreamArgs { | ||
name: string; | ||
target: string; | ||
targetOrigin?: string; | ||
targetWindow?: Window; | ||
} | ||
|
||
/** | ||
* A {@link Window.postMessage} stream. | ||
*/ | ||
export default class WebviewPostMessageStream extends BasePostMessageStream { | ||
private _name: string; | ||
|
||
private _target: string; | ||
|
||
private _targetOrigin: string; | ||
|
||
private _targetWindow: Window; | ||
|
||
/** | ||
* Creates a stream for communicating with other streams across the same or | ||
* different `window` objects. | ||
* | ||
* @param args - Options bag. | ||
* @param args.name - The name of the stream. Used to differentiate between | ||
* multiple streams sharing the same window object. | ||
* @param args.target - The name of the stream to exchange messages with. | ||
* @param args.targetOrigin - The origin of the target. Defaults to | ||
* `location.origin`, '*' is permitted. | ||
* @param args.targetWindow - The window object of the target stream. Defaults | ||
* to `window`. | ||
*/ | ||
constructor({ | ||
name, | ||
target, | ||
targetOrigin, | ||
targetWindow, | ||
}: WindowPostMessageStreamArgs) { | ||
super(); | ||
|
||
this._name = name; | ||
this._target = target; | ||
this._targetOrigin = targetOrigin; | ||
this._targetWindow = targetWindow; | ||
this._onMessage = this._onMessage.bind(this); | ||
|
||
//this._targetWindow.onMessage = this._onMessage; | ||
|
||
setTimeout(() => this._handshake(), 0); | ||
} | ||
|
||
protected _postMessage(data: unknown): void { | ||
const message = { | ||
target: this._target, | ||
data, | ||
}; | ||
this._targetWindow.postMessage(JSON.stringify(message), this._targetOrigin); | ||
} | ||
|
||
private _onMessage(event: PostMessageEvent): void { | ||
const message = event.nativeEvent; | ||
const data = JSON.parse(message.data); | ||
|
||
this._onData(data.data); | ||
} | ||
|
||
_destroy(): void { | ||
// eslint-disable-next-line no-console | ||
console.log('destroy'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const snapsState = { | ||
stream: null, | ||
webview: null, | ||
}; | ||
|
||
export default snapsState; |