Skip to content

Commit

Permalink
Adding missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimenta committed Nov 24, 2022
1 parent 3f7a5b5 commit 2c1eb44
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
78 changes: 78 additions & 0 deletions app/components/Views/Wallet/WebviewPostMessageStream.ts
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');
}
}
40 changes: 39 additions & 1 deletion app/components/Views/Wallet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import React, {
useEffect,
useRef,
Expand Down Expand Up @@ -35,7 +37,11 @@ import { shouldShowWhatsNewModal } from '../../../util/onboarding';
import Logger from '../../../util/Logger';
import Routes from '../../../constants/navigation/Routes';
import generateTestId from '../../../../wdio/utils/generateTestId';

import WebView from 'react-native-webview';
import { Button } from 'react-native-share';
import WebviewPostMessageStream from './WebviewPostMessageStream';
import snapsState from '../../../core/SnapsState';
let stream;
const createStyles = (colors: any) =>
StyleSheet.create({
wrapper: {
Expand Down Expand Up @@ -74,6 +80,8 @@ const Wallet = ({ navigation }: any) => {
const accountOverviewRef = useRef(null);
const { colors } = useTheme();
const styles = createStyles(colors);

const webviewRef = useRef();
/**
* Map of accounts to information objects including balances
*/
Expand Down Expand Up @@ -325,6 +333,29 @@ const Wallet = ({ navigation }: any) => {
[navigation, wizardStep],
);

const messageFromWebview = (data) => {
stream?._onMessage(data);
};

const setWebviewPostMessage = () => {
stream = new WebviewPostMessageStream({
name: 'rnside',
target: 'webview',
targetOrigin: '*',
targetWindow: webviewRef.current,
});

// eslint-disable-next-line no-console
stream.on('data', (data) => console.log('Message from webview ' + data));

snapsState.stream = stream;
snapsState.webview = webviewRef.current;
};

const sendMessageToWebview = () => {
stream.write('HELLO');
};

return (
<ErrorBoundary view="Wallet">
<View style={baseStyles.flexGrow} {...generateTestId('wallet-screen')}>
Expand All @@ -341,6 +372,13 @@ const Wallet = ({ navigation }: any) => {
>
{selectedAddress ? renderContent() : renderLoader()}
</ScrollView>
<Button onPress={sendMessageToWebview}>Send message to webview</Button>
<WebView
ref={webviewRef}
source={{ uri: 'http://localhost:3000/' }}
onMessage={messageFromWebview}
onLoadEnd={setWebviewPostMessage}
/>
{renderOnboardingWizard()}
</View>
</ErrorBoundary>
Expand Down
6 changes: 6 additions & 0 deletions app/core/SnapsState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const snapsState = {
stream: null,
webview: null,
};

export default snapsState;

0 comments on commit 2c1eb44

Please sign in to comment.