KoalaSat 9bda934ee5 Android Tor icon and copy to clipboard (#269)
* 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>
2022-10-07 14:10:21 +00:00

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;