mirror of
https://github.com/fluencelabs/registry.git
synced 2025-04-28 19:42:14 +00:00
Add initTopicAndSubscribeBlocking (#56)
This commit is contained in:
parent
c498f6c15d
commit
c19ee1293e
@ -40,6 +40,28 @@ func initTopicAndSubscribe(topic: string, value: string, relay_id: ?PeerId, serv
|
|||||||
AquaDHT.register_key(topic, t, false, 0)
|
AquaDHT.register_key(topic, t, false, 0)
|
||||||
AquaDHT.put_value(topic, value, t, relay_id, service_id, 0)
|
AquaDHT.put_value(topic, value, t, relay_id, service_id, 0)
|
||||||
|
|
||||||
|
-- Create a topic and subscribe to it
|
||||||
|
-- %init_peer_id% (current client) will become a subscriber
|
||||||
|
-- In contrast with non-blocking version, waits for at least a single write to succeed
|
||||||
|
func initTopicAndSubscribeBlocking(
|
||||||
|
topic: string, value: string,
|
||||||
|
relay_id: ?PeerId, service_id: ?string,
|
||||||
|
progress: string -> ()
|
||||||
|
) -> DhtResult:
|
||||||
|
results: *DhtResult
|
||||||
|
on HOST_PEER_ID:
|
||||||
|
nodes <- getNeighbours(topic)
|
||||||
|
for n <- nodes par:
|
||||||
|
on n:
|
||||||
|
try:
|
||||||
|
t <- Peer.timestamp_sec()
|
||||||
|
AquaDHT.register_key(topic, t, false, 0)
|
||||||
|
result <- AquaDHT.put_value(topic, value, t, relay_id, service_id, 0)
|
||||||
|
if result.success:
|
||||||
|
results <<- result
|
||||||
|
co progress(n)
|
||||||
|
<- results!0
|
||||||
|
|
||||||
-- Create a topic and make the given node a subscriber to it
|
-- Create a topic and make the given node a subscriber to it
|
||||||
func initTopicAndSubscribeNode(subscriber_node_id: PeerId, topic: string, value: string, service_id: ?string):
|
func initTopicAndSubscribeNode(subscriber_node_id: PeerId, topic: string, value: string, service_id: ?string):
|
||||||
on subscriber_node_id:
|
on subscriber_node_id:
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
import initTopicAndSubscribe, findSubscribers from "@fluencelabs/aqua-dht/pubsub.aqua"
|
import initTopicAndSubscribeBlocking, findSubscribers from "@fluencelabs/aqua-dht/pubsub.aqua"
|
||||||
|
|
||||||
export initTopicAndSubscribe, findSubscribers
|
export initTopicAndSubscribeBlocking, findSubscribers
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Fluence } from "@fluencelabs/fluence";
|
import { Fluence } from "@fluencelabs/fluence";
|
||||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||||
import { initTopicAndSubscribe, findSubscribers } from "./generated/export";
|
import { initTopicAndSubscribeBlocking, findSubscribers } from "./generated/export";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
// connect to the Fluence network
|
// connect to the Fluence network
|
||||||
@ -9,7 +9,10 @@ async function main() {
|
|||||||
let value = "myValue";
|
let value = "myValue";
|
||||||
// create topic (if not exists) and subscribe on it
|
// create topic (if not exists) and subscribe on it
|
||||||
let relay = Fluence.getStatus().relayPeerId;
|
let relay = Fluence.getStatus().relayPeerId;
|
||||||
await initTopicAndSubscribe(topic, value, relay, null);
|
await initTopicAndSubscribeBlocking(
|
||||||
|
topic, value, relay, null,
|
||||||
|
(s) => console.log(`node ${s} saved the record`)
|
||||||
|
);
|
||||||
// find other peers subscribed to that topic
|
// find other peers subscribed to that topic
|
||||||
let subscribers = await findSubscribers(topic);
|
let subscribers = await findSubscribers(topic);
|
||||||
console.log("found subscribers:", subscribers);
|
console.log("found subscribers:", subscribers);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user