-
-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathRemoteCommunicationPostMessageStream.ts
More file actions
82 lines (74 loc) · 2.19 KB
/
Copy pathRemoteCommunicationPostMessageStream.ts
File metadata and controls
82 lines (74 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {
CommunicationLayerMessage,
EventType,
RemoteCommunication,
} from '@metamask/sdk-communication-layer';
import { Duplex } from 'readable-stream';
import { PlatformManager } from '../Platform/PlatfformManager';
import { ProviderConstants } from '../constants';
import { onMessage } from '../services/RemoteCommunicationPostMessageStream/onMessage';
import { write } from '../services/RemoteCommunicationPostMessageStream/write';
import { PostMessageStream } from './PostMessageStream';
interface RemoteCommunicationPostMessageStreamState {
_name: any;
remote: RemoteCommunication | null;
deeplinkProtocol: boolean;
hideReturnToAppNotification?: boolean;
platformManager: PlatformManager | null;
}
export class RemoteCommunicationPostMessageStream
extends Duplex
implements PostMessageStream
{
public state: RemoteCommunicationPostMessageStreamState = {
_name: null,
remote: null,
deeplinkProtocol: false,
hideReturnToAppNotification: false,
platformManager: null,
};
constructor({
name,
remote,
deeplinkProtocol,
hideReturnToAppNotification,
platformManager,
}: {
name: ProviderConstants;
deeplinkProtocol: boolean;
hideReturnToAppNotification?: boolean;
remote: RemoteCommunication;
platformManager: PlatformManager;
}) {
super({
objectMode: true,
});
this.state._name = name;
this.state.remote = remote;
this.state.deeplinkProtocol = deeplinkProtocol;
this.state.hideReturnToAppNotification =
hideReturnToAppNotification ?? this.state.hideReturnToAppNotification;
this.state.platformManager = platformManager;
this._onMessage = this._onMessage.bind(this);
this.state.remote.on(EventType.MESSAGE, this._onMessage);
}
/**
* Called when querying the sdk provider with ethereum.request
*/
async _write(
chunk: any,
_encoding: BufferEncoding,
callback: (error?: Error | null) => void,
) {
return write(this, chunk, _encoding, callback);
}
_read() {
return undefined;
}
_onMessage(message: CommunicationLayerMessage) {
return onMessage(this, message);
}
start() {
// Ethereum.ethereum.isConnected = () => RemoteConnection.isConnected();
}
}