mirror of
https://github.com/RoboSats/robosats.git
synced 2025-09-13 00:56:22 +00:00
* Android Clipboard and Tor Status Icon * working clipboard and lintern * Fix * Add style for Tor connection component * Fix Freeze and Internet out * Fix Typo Co-authored-by: Reckless_Satoshi <reckless.satoshi@protonmail.com>
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import { NativeRobosatsPromise, NativeWebViewMessage, NativeWebViewMessageSystem } from './index.d';
|
|
|
|
class NativeRobosats {
|
|
constructor() {
|
|
this.messageCounter = 0;
|
|
}
|
|
|
|
public torDaemonStatus = 'NOTINIT';
|
|
|
|
private messageCounter: number;
|
|
|
|
private pendingMessages: { [id: number]: NativeRobosatsPromise } = {};
|
|
|
|
public onMessageResolve: (messageId: number, response?: object) => void = (
|
|
messageId,
|
|
response = {},
|
|
) => {
|
|
if (this.pendingMessages[messageId]) {
|
|
this.pendingMessages[messageId].resolve(response);
|
|
delete this.pendingMessages[messageId];
|
|
}
|
|
};
|
|
|
|
public onMessageReject: (messageId: number, response?: object) => void = (
|
|
messageId,
|
|
response = {},
|
|
) => {
|
|
if (this.pendingMessages[messageId]) {
|
|
this.pendingMessages[messageId].reject(response);
|
|
delete this.pendingMessages[messageId];
|
|
}
|
|
};
|
|
|
|
public onMessage: (message: NativeWebViewMessageSystem) => void = (message) => {
|
|
if (message.type === 'torStatus') {
|
|
this.torDaemonStatus = message.detail;
|
|
window.dispatchEvent(new CustomEvent('torStatus', { detail: this.torDaemonStatus }));
|
|
}
|
|
};
|
|
|
|
public postMessage: (message: NativeWebViewMessage) => Promise<{ [key: string]: any }> = async (
|
|
message,
|
|
) => {
|
|
this.messageCounter += 1;
|
|
message.id = this.messageCounter;
|
|
const json = JSON.stringify(message);
|
|
window.ReactNativeWebView?.postMessage(json);
|
|
|
|
return await new Promise<object>(async (resolve, reject) => {
|
|
if (message.id) {
|
|
this.pendingMessages[message.id] = {
|
|
resolve,
|
|
reject,
|
|
};
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
export default NativeRobosats;
|