feat: add functions to retrieve message history and check if node is started (#5)

This commit is contained in:
Richard Ramos 2022-08-01 19:43:49 -04:00 committed by GitHub
parent f00c3d267a
commit 99964ccf69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 87 additions and 12 deletions

View File

@ -64,6 +64,11 @@ class ReactNativeModule(reactContext: ReactApplicationContext) : ReactContextBas
promise.resolve(Gowaku.stop())
}
@ReactMethod
fun isStarted(promise: Promise) {
promise.resolve(Gowaku.isStarted())
}
@ReactMethod
fun peerID(promise: Promise) {
promise.resolve(Gowaku.peerID())

View File

@ -1,7 +1,7 @@
import * as React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { defaultPubsubTopic, newNode, start, stop, peerID, relayEnoughPeers, listenAddresses, connect, peerCnt, peers, relayPublish, relayUnsubscribe, relaySubscribe, WakuMessage, onMessage } from '@waku/react-native';
import { defaultPubsubTopic, newNode, start, isStarted, stop, peerID, relayEnoughPeers, listenAddresses, connect, peerCnt, peers, relayPublish, relayUnsubscribe, relaySubscribe, WakuMessage, onMessage, storeQuery, StoreQuery } from '@waku/react-native';
export default function App() {
const [result, setResult] = React.useState<string | undefined>();
@ -10,9 +10,12 @@ export default function App() {
React.useEffect(() => {
(async () => {
await newNode(null); // TODO: This must be called only once
await start(); // // TODO: This must be called only once
const nodeStarted = await isStarted();
if (!nodeStarted) {
await newNode(null);
await start();
}
console.log("The node ID:", await peerID())
await relaySubscribe()
@ -27,7 +30,7 @@ export default function App() {
await connect("/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm", 5000)
console.log("connected!")
console.log("connected!")
console.log("PeerCNT", await peerCnt())
console.log("Peers", await peers())
@ -42,6 +45,14 @@ export default function App() {
console.log("The messageID", messageID)
/*
console.log("Retrieving messages from store node")
const query = new StoreQuery();
//query.contentFilters.push("/toy-chat/2/luzhou/proto")
const queryResult = await storeQuery(query, "16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm")
console.log(queryResult)
*/
// await delay(5000) // Waiting 5s before unsubscribing
// console.log("Unsubscribing and stopping node...")

View File

@ -1 +1 @@
0.1.0
0.2.0

View File

@ -21,6 +21,10 @@ RCT_EXTERN_METHOD(stop:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(isStarted:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(peerID:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
@ -125,4 +129,10 @@ RCT_EXTERN_METHOD(decodeAsymmetric:(NSString *)msg
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(storeQuery:(NSString *)query
withPeerID:(NSString *)peerID
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
@end

View File

@ -62,6 +62,11 @@ class ReactNative: RCTEventEmitter {
resolve(GowakuStop())
}
@objc(isStarted:withRejecter:)
func isStarted(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuIsStarted())
}
@objc(peerID:withRejecter:)
func peerID(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuPeerID())
@ -157,4 +162,9 @@ class ReactNative: RCTEventEmitter {
resolve(GowakuDecodeAsymmetric(msg, privateKey))
}
@objc(storeQuery:withPeerID:withMs:withResolver:withRejecter:)
func storeQuery(queryJSON: String, peerID: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuStoreQuery(queryJSON, peerID, ms))
}
}

View File

@ -1,6 +1,6 @@
{
"name": "@waku/react-native",
"version": "0.0.2",
"version": "0.0.4",
"description": "Waku React Native",
"author": "Status Research & Development GMBH",
"authors": [

View File

@ -93,6 +93,17 @@ export function stop(): Promise<void> {
});
}
export function isStarted(): Promise<Boolean> {
return new Promise<Boolean>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.isStarted());
if(response.error){
reject(response.error);
} else {
resolve(response.result);
}
});
}
export function peerID(): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.peerID());
@ -295,11 +306,6 @@ export function relayUnsubscribe(topic: String = ""): Promise<void> {
});
}
export function lightpushPublish(msg: WakuMessage, topic: String = "", peerID: String = "", ms: Number = 0): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg)
@ -361,4 +367,37 @@ export function peers(): Promise<Array<Peer>> {
})
}
// TODO: storeQuery
export class Index {
digest: Uint8Array = new Uint8Array();
receiverTime: Number = 0
senderTime: Number = 0
pubsubTopic: String = ""
}
export class PagingOptions {
pageSize: Number = 0
cursor: Index | null = null
forward: Boolean = false
}
export class StoreQuery {
pubsubTopic: String = ""
contentFilters: Array<String> = Array()
startTime: Number = 0
endTime: Number = 0
pagingOptions: PagingOptions | null = null
}
export function storeQuery(query: StoreQuery, peerID: String = "", ms: Number = 0): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let queryJSON = JSON.stringify(query)
let response = JSON.parse(await ReactNative.storeQuery(queryJSON, peerID, ms));
console.log("STORE RESPONSE:")
console.log(response)
if(response.error){
reject(response.error);
} else {
resolve(response.result);
}
});
}