/* eslint-disable no-alert */ /** * Sample React Native App * https://github.com/facebook/react-native * * @format */ import React from 'react'; import type {PropsWithChildren} from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, useColorScheme, View, } from 'react-native'; import {Colors} from 'react-native/Libraries/NewAppScreen'; import { NativeEventEmitter, NativeModules, EmitterSubscription, Button, } from 'react-native'; type SectionProps = PropsWithChildren<{ title: string; }>; function Section({children, title}: SectionProps): React.JSX.Element { const isDarkMode = useColorScheme() === 'dark'; return ( {title} {children} ); } const WakuFactory = (() => { let isSetup = false; const eventEmitter = new NativeEventEmitter(NativeModules.WakuModule); class Waku { wakuPtr: Number; constructor(wakuPtr: Number) { this.wakuPtr = wakuPtr; } async destroy(): Promise { await NativeModules.WakuModule.destroy(this.wakuPtr); } async start(): Promise { return NativeModules.WakuModule.start(this.wakuPtr); } async stop(): Promise { return NativeModules.WakuModule.stop(this.wakuPtr); } async version(): Promise { return NativeModules.WakuModule.version(this.wakuPtr); } async listenAddresses(): Promise> { let addresses = await NativeModules.WakuModule.listenAddresses( this.wakuPtr, ); return addresses; } async connect(peerMultiaddr: String, timeoutMs: Number): Promise { return NativeModules.WakuModule.connect( this.wakuPtr, peerMultiaddr, timeoutMs, ); } async relaySubscribe(pubsubTopic: String): Promise { return NativeModules.WakuModule.relaySubscribe(this.wakuPtr, pubsubTopic); } async relayUnsubscribe(pubsubTopic: String): Promise { return NativeModules.WakuModule.relayUnsubscribe( this.wakuPtr, pubsubTopic, ); } // TODO: Use a type instead of `any` async relayPublish( pubsubTopic: string, msg: any, timeoutMs: Number, ): Promise { return NativeModules.WakuModule.relayPublish( this.wakuPtr, pubsubTopic, msg, timeoutMs, ); } onEvent(cb: (event: any) => void): EmitterSubscription { return eventEmitter.addListener('wakuEvent', evt => { if (evt.wakuPtr === this.wakuPtr) { cb(JSON.parse(evt.event)); } }); } } async function createInstance(config: any) { if (!isSetup) { console.debug('initializing waku library'); await NativeModules.WakuModule.setup(); isSetup = true; alert('waku instance created!'); } let wakuPtr = await NativeModules.WakuModule.new(config); return new Waku(wakuPtr); } // Expose the factory method return { createInstance, Waku, }; })(); function App(): React.JSX.Element { const isDarkMode = useColorScheme() === 'dark'; const backgroundStyle = { backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, }; var waku: Waku; const onClickNew = async () => { const config = { host: '0.0.0.0', port: 42342, key: '1122334455667788990011223344556677889900112233445566778899000022', relay: true, }; waku = await WakuFactory.createInstance(config); }; const onClickStart = async () => { await waku.start(); alert('start executed succesfully'); }; const onClickVersion = async () => { let version = await waku.version(); alert(version); }; const onClickListenAddresses = async () => { let addresses = await waku.listenAddresses(); alert(addresses[0]); }; const onClickStop = async () => { await waku.stop(); alert('stopped!'); }; const onClickDestroy = async () => { await waku.destroy(); alert('destroyed!'); }; const onClickConnect = async () => { let result = await waku.connect( '/ip4/127.0.0.1/tcp/48117/p2p/16Uiu2HAmVrsyU3y3pQYuSEyaqrBgevQeshp7YZsL8rY3nWb2yWD5', 0, ); alert( 'connected? (TODO: bindings function do not return connection attempt status)', ); }; const onClickSubscribe = async () => { await waku.relaySubscribe('test'); alert('subscribed to test'); }; const onClickUnsubscribe = async () => { await waku.relayUnsubscribe('test'); alert('unsubscribed from test'); }; const onClickSetEventCallback = async () => { const eventSubs = waku.onEvent((event: any) => { console.log(event); alert('received a message'); }); // TODO: eventSubs.remove() should be used to avoid a mem leak. alert("event callback set"); }; const onClickPublish = async () => { const pubsubTopic = 'test'; const msg = { payload: 'aGVsbG8', contentTopic: 'test', timestamp: 0, version: 0, }; let hash = await waku.relayPublish(pubsubTopic, msg, 0); alert('published - msgHash: ' + hash); }; return (
); } const styles = StyleSheet.create({ sectionContainer: { marginTop: 32, paddingHorizontal: 24, }, sectionTitle: { fontSize: 24, fontWeight: '600', }, sectionDescription: { marginTop: 8, fontSize: 18, fontWeight: '400', }, highlight: { fontWeight: '700', }, }); export default App;