Skip to content

Commit

Permalink
browser: reuse index of found services between responses
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Dec 29, 2015
1 parent a2038a1 commit af0c2fb
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Browser (mdns, opts, onup) {
this._name = serviceName.stringify(opts.type, opts.protocol || 'tcp') + TLD
this._mdns = mdns
this._onresponse = null
this._serviceMap = {}
this.services = []
this.start()
}
Expand All @@ -46,13 +47,8 @@ Browser.prototype.start = function () {
var matches = buildServicesFor(self._name, packet)
if (matches.length === 0) return

var exists = {}
self.services.forEach(function (service) {
exists[service.fqdn] = true
})

matches.forEach(function (service) {
if (exists[service.fqdn]) return
if (self._serviceMap[service.fqdn]) return
self._addService(service)
})
}
Expand All @@ -70,6 +66,7 @@ Browser.prototype.stop = function () {

Browser.prototype._addService = function (service) {
this.services.push(service)
this._serviceMap[service.fqdn] = true
this.emit('up', service)
}

Expand Down

0 comments on commit af0c2fb

Please sign in to comment.