From f8a6c1ad88c553143bd66b06b41d46714f4fdc91 Mon Sep 17 00:00:00 2001 From: nuzulul Date: Mon, 15 Jul 2024 23:48:45 +0700 Subject: [PATCH] fix : fix bug double on connect event --- src/webpeerjs.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/webpeerjs.js b/src/webpeerjs.js index ea68710..0802767 100644 --- a/src/webpeerjs.js +++ b/src/webpeerjs.js @@ -99,6 +99,9 @@ class webpeerjs{ //track last connect to webpeer network #lastTimeConnectToNetwork + //arr to track on connect event + #onConnectQueue + id status IPFS @@ -129,6 +132,7 @@ class webpeerjs{ this.#msgIdtracker = [] this.#peerexchangedata = new Map() this.#lastTimeConnectToNetwork = new Date().getTime() + this.#onConnectQueue = [] this.peers = (function(f) { return f @@ -207,7 +211,7 @@ class webpeerjs{ } else{ //add to connected webpeers - this.#onConnectFn(id) + this.#onConnectFnUpdate(id) const now = new Date().getTime() const metadata = {addrs:address,last:now} this.#connectedPeers.set(id,metadata) @@ -260,7 +264,7 @@ class webpeerjs{ } else{ //add to connected webpeers - this.#onConnectFn(senderPeerId) + this.#onConnectFnUpdate(senderPeerId) const address = this.#webPeersAddrs.get(senderPeerId) const now = new Date().getTime() const metadata = {addrs:address,last:now} @@ -339,7 +343,7 @@ class webpeerjs{ //add to connected webpeers if(!this.#connectedPeers.has(id)){ - this.#onConnectFn(id) + this.#onConnectFnUpdate(id) address = [] const now = new Date().getTime() const metadata = {addrs:address,last:now} @@ -779,6 +783,20 @@ class webpeerjs{ /* PRIVATE FUNCTION */ + + //prevent double on connect event + #onConnectFnUpdate(id){ + if(!this.#onConnectQueue.includes(id)){ + this.#onConnectQueue.push(id) + this.#onConnectFn(id) + setTimeout(()=>{ + const index = this.#onConnectQueue.indexOf(id) + if (index > -1) { + this.#onConnectQueue.splice(index, 1) + } + },5000) + } + } #updatePeers(){ this.#connectedPeersArr.length = 0 @@ -836,7 +854,7 @@ class webpeerjs{ if(!this.#webPeersId.includes(id))this.#webPeersId.push(id) //add to connected webpeers - this.#onConnectFn(id) + this.#onConnectFnUpdate(id) const now = new Date().getTime() const metadata = {addrs:address,last:now} this.#connectedPeers.set(id,metadata) @@ -963,7 +981,7 @@ class webpeerjs{ if(!this.#webPeersId.includes(id))this.#webPeersId.push(id) //add to connected webpeers - this.#onConnectFn(id) + this.#onConnectFnUpdate(id) const now = new Date().getTime() const metadata = {addrs:address,last:now} this.#connectedPeers.set(id,metadata)