Skip to content

Update Zeroconf class and add methods to remove listeners #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 125 additions & 64 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,131 @@
import { NativeModules, DeviceEventEmitter } from 'react-native'
import { EventEmitter } from 'events'
import {NativeModules, DeviceEventEmitter} from 'react-native'
import {EventEmitter} from 'events'

const RNZeroconf = NativeModules.RNZeroconf

export default class Zeroconf extends EventEmitter {

constructor (props) {
super(props)

this._services = {}

DeviceEventEmitter.addListener('RNZeroconfStart', () => this.emit('start'))
DeviceEventEmitter.addListener('RNZeroconfStop', () => this.emit('stop'))
DeviceEventEmitter.addListener('RNZeroconfError', err => this.emit('error', err))

DeviceEventEmitter.addListener('RNZeroconfFound', service => {
if (!service || !service.name) { return }
const { name } = service

this._services[name] = service
this.emit('found', name)
this.emit('update')
})

DeviceEventEmitter.addListener('RNZeroconfRemove', service => {
if (!service || !service.name) { return }
const { name } = service

delete this._services[name]

this.emit('remove', name)
this.emit('update')
})

DeviceEventEmitter.addListener('RNZeroconfResolved', service => {
if (!service || !service.name) { return }

this._services[service.name] = service
this.emit('resolved', service)
this.emit('update')
})

}

/**
* Get all the services already resolved
*/
getServices () {
return this._services
}

/**
* Scan for Zeroconf services,
* Defaults to _http._tcp. on local domain
*/
scan (type = 'http', protocol = 'tcp', domain = 'local.') {
this._services = {}
this.emit('update')
RNZeroconf.scan(type, protocol, domain)
}

/**
* Stop current scan if any
*/
stop () {
RNZeroconf.stop()
}

constructor(props) {
super(props)
this._services = {}
this._deviceListeners = {}
this._onStart = this._onStart.bind(this)
this._onStop = this._onStop.bind(this)
this._onError = this._onError.bind(this)
this._onFound = this._onFound.bind(this)
this._onResolved = this._onResolved.bind(this)
this._onRemove = this._onRemove.bind(this)
}

/**
* Remove all listeners
*/
removeDeviceListener(event) {
this._deviceListeners[event].remove()
}

removeAllDeviceListeners() {
Object.keys(this._deviceListeners).map(e => this._deviceListeners[e].remove())
}

/**
* Scan for Zeroconf services,
* Defaults to _http._tcp. on local domain
*/
scan(type = 'http', protocol = 'tcp', domain = 'local.') {
this._deviceListeners.start = DeviceEventEmitter.addListener('RNZeroconfStart', this._onStart)
this._deviceListeners.stop = DeviceEventEmitter.addListener('RNZeroconfStop', this._onStop)
this._deviceListeners.remove = DeviceEventEmitter.addListener('RNZeroconfStop', this._onRemove)
this._deviceListeners.found = DeviceEventEmitter.addListener('RNZeroconfFound', this._onFound)
this._deviceListeners.resolve = DeviceEventEmitter.addListener('RNZeroconfResolved', this._onResolved)
this._deviceListeners.error = DeviceEventEmitter.addListener('RNZeroconfError', this._onError)

this._services = {}
this.emit('update')
RNZeroconf.scan(type, protocol, domain)
}

/**
* Stop current scan if any
*/
stop() {
RNZeroconf.stop()
this.removeAllDeviceListeners()
}

/**
* Get all the services already resolved
*/
getServices() {
return this._services
}


/**
* On start
* @private
*/
_onStart() {
this.emit('start')
}

/**
* On stop
* @private
*/
_onStop() {
this.emit('stop')
}

/**
* On service found
* @private
*/
_onFound(service) {
if (!service || !service.name) {
return
}
const {name} = service

this._services[name] = service
this.emit('found', name)
this.emit('update')
}

/**
* On service resolved
* @private
*/
_onResolved(service) {
if (!service || !service.name) {
return
}
this._services[service.name] = service
this.emit('resolved', service)
this.emit('update')
}

/**
* On service removed
* @private
*/
_onRemove(service) {
if (!service || !service.name) {
return
}
const {name} = service

delete this._services[name]

this.emit('remove', name)
this.emit('update')
}

/**
* On error
* @private
*/
_onError(err) {
this.emit('error', err)
}
}