maxwofford / bus-tracker

5 stars 6 forks source link

Use Map (es6) instead of array. #1

Open TheThirdOne opened 8 years ago

TheThirdOne commented 8 years ago

The code for storing would be better represented as a map. You make an effort to not make socket a key despite using for / if constructs to treat it as such. Also, given the use of const and fat arrows, the increased use of es6 shouldn't pose a compatability problem.

I propose that it be rewritten to something like: (warning not tested)

const busses = new Map()

socket.on('busConnection', () => {
    const bus = {
      lat: null,
      long: null
    }
    busses.set(socket, bus)
  })

  socket.on('update', data => {
    let bus = busses.get(socket)
    bus.lat = data.lat
    bus.long = data.long
  })

  socket.on('disconnect', () => {
    busses.delete(socket)
  }

setInterval(() => {
  const busLocations = [...busses.values()]
  io.emit('tick', busLocations)
  console.log(busLocations)
}, 5000)
eddiezane commented 8 years ago

@TheThirdOne great idea! Thanks a ton for the consideration.