mirror of https://github.com/waku-org/js-waku.git
fix: invoke peer exchange query immediately (#1115)
when the query is set in an interval, it first gets invoked when the interval time is reached. refactor: invoke it immmediately, and then set an interval
This commit is contained in:
parent
0b083201c6
commit
fcd500e045
|
@ -55,8 +55,62 @@ export class PeerExchangeDiscovery
|
|||
if (!protocols.includes(PeerExchangeCodec) || this.intervals.get(peerId))
|
||||
return;
|
||||
|
||||
await this.query(peerId);
|
||||
const interval = setInterval(async () => {
|
||||
await this.peerExchange.query(
|
||||
await this.query(peerId);
|
||||
}, PEER_EXCHANGE_QUERY_INTERVAL);
|
||||
|
||||
this.intervals.set(peerId, interval);
|
||||
};
|
||||
|
||||
constructor(components: PeerExchangeComponents, options: Options = {}) {
|
||||
super();
|
||||
this.components = components;
|
||||
this.peerExchange = new WakuPeerExchange(components);
|
||||
this.options = options;
|
||||
this.isStarted = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start emitting events
|
||||
*/
|
||||
start(): void {
|
||||
if (this.isStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
log("Starting peer exchange node discovery, discovering peers");
|
||||
|
||||
this.components.peerStore.addEventListener(
|
||||
"change:protocols",
|
||||
this.eventHandler
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove event listener
|
||||
*/
|
||||
stop(): void {
|
||||
if (!this.isStarted) return;
|
||||
log("Stopping peer exchange node discovery");
|
||||
this.isStarted = false;
|
||||
this.intervals.forEach((interval) => clearInterval(interval));
|
||||
this.components.peerStore.removeEventListener(
|
||||
"change:protocols",
|
||||
this.eventHandler
|
||||
);
|
||||
}
|
||||
|
||||
get [symbol](): true {
|
||||
return true;
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag](): string {
|
||||
return "@waku/peer-exchange";
|
||||
}
|
||||
|
||||
private query(peerId: PeerId): Promise<void> {
|
||||
return this.peerExchange.query(
|
||||
{
|
||||
numPeers: DEFAULT_PEER_EXCHANGE_REQUEST_NODES,
|
||||
peerId,
|
||||
|
@ -110,55 +164,6 @@ export class PeerExchangeDiscovery
|
|||
}
|
||||
}
|
||||
);
|
||||
}, PEER_EXCHANGE_QUERY_INTERVAL);
|
||||
|
||||
this.intervals.set(peerId, interval);
|
||||
};
|
||||
|
||||
constructor(components: PeerExchangeComponents, options: Options = {}) {
|
||||
super();
|
||||
this.components = components;
|
||||
this.peerExchange = new WakuPeerExchange(components);
|
||||
this.options = options;
|
||||
this.isStarted = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start emitting events
|
||||
*/
|
||||
start(): void {
|
||||
if (this.isStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
log("Starting peer exchange node discovery, discovering peers");
|
||||
|
||||
this.components.peerStore.addEventListener(
|
||||
"change:protocols",
|
||||
this.eventHandler
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove event listener
|
||||
*/
|
||||
stop(): void {
|
||||
if (!this.isStarted) return;
|
||||
log("Stopping peer exchange node discovery");
|
||||
this.isStarted = false;
|
||||
this.intervals.forEach((interval) => clearInterval(interval));
|
||||
this.components.peerStore.removeEventListener(
|
||||
"change:protocols",
|
||||
this.eventHandler
|
||||
);
|
||||
}
|
||||
|
||||
get [symbol](): true {
|
||||
return true;
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag](): string {
|
||||
return "@waku/peer-exchange";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue