Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
fix: dont use 'this' in root anon function
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
jacobheun committed Nov 29, 2018
1 parent 091f65c commit c6a833e
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = (config, http) => {

log('create new server', config)

this._peers = {}
const _peers = {}
const nonces = {}

const peersMetric = config.metrics ? new client.Gauge({ name: 'rendezvous_peers', help: 'peers online now' }) : fake.gauge
Expand All @@ -42,13 +42,10 @@ module.exports = (config, http) => {
const joinsFailureTotal = config.metrics ? new client.Counter({ name: 'rendezvous_joins_total_failure', help: 'failed joins since server started' }) : fake.counter
const joinsTotal = config.metrics ? new client.Counter({ name: 'rendezvous_joins_total', help: 'all joins since server started' }) : fake.counter

const getPeers = () => this._peers // it's a function because, and I'm not kidding, the value of that var is different for every peer that has joined
const refreshMetrics = () => peersMetric.set(Object.keys(getPeers()).length)

this.peers = () => getPeers()
const refreshMetrics = () => peersMetric.set(Object.keys(_peers).length)

function safeEmit (addr, event, arg) {
const peer = getPeers()[addr]
const peer = _peers[addr]
if (!peer) {
log('trying to emit %s but peer is gone', event)
return
Expand Down Expand Up @@ -130,7 +127,7 @@ module.exports = (config, http) => {

function joinFinalize (socket, multiaddr, cb) {
const log = getConfig().log.bind(getConfig().log, '[' + socket.id + ']')
getPeers()[multiaddr] = socket
_peers[multiaddr] = socket
if (!socket.stopSendingPeersIntv) socket.stopSendingPeersIntv = {}
joinsSuccessTotal.inc()
refreshMetrics()
Expand All @@ -146,7 +143,7 @@ module.exports = (config, http) => {
sendPeers()

function sendPeers () {
const list = Object.keys(getPeers())
const list = Object.keys(_peers)
log(multiaddr, 'sending', (list.length - 1).toString(), 'peer(s)')
list.forEach((mh) => {
if (mh === multiaddr) {
Expand All @@ -167,14 +164,14 @@ module.exports = (config, http) => {

socket.stopSendingPeersIntv[multiaddr] = stopSendingPeers

const otherPeers = Object.keys(getPeers()).filter(mh => mh !== multiaddr)
const otherPeers = Object.keys(_peers).filter(mh => mh !== multiaddr)
cb(null, null, otherPeers)
}

function leave (socket, multiaddr) {
if (getPeers()[multiaddr] && getPeers()[multiaddr].id === socket.id) {
if (_peers[multiaddr] && _peers[multiaddr].id === socket.id) {
socket.log('leaving', multiaddr)
delete getPeers()[multiaddr]
delete _peers[multiaddr]
socket.addrs = socket.addrs.filter(m => m !== multiaddr)
if (socket.stopSendingPeersIntv[multiaddr]) {
socket.stopSendingPeersIntv[multiaddr]()
Expand All @@ -186,8 +183,8 @@ module.exports = (config, http) => {

function disconnect (socket) {
socket.log('disconnected')
Object.keys(getPeers()).forEach((mh) => {
if (getPeers()[mh].id === socket.id) {
Object.keys(_peers).forEach((mh) => {
if (_peers[mh].id === socket.id) {
leave(socket, mh)
}
})
Expand All @@ -205,7 +202,7 @@ module.exports = (config, http) => {
}

log(from, 'is dialing', to)
const peer = getPeers()[to]
const peer = _peers[to]

if (!peer) {
dialsFailureTotal.inc()
Expand All @@ -226,5 +223,7 @@ module.exports = (config, http) => {
})
}

return this
return {
peers: () => _peers
}
}

0 comments on commit c6a833e

Please sign in to comment.