futomi / node-dns-sd

The node-dns-sd is a Node.js module which is a pure javascript implementation of mDNS/DNS-SD (Apple Bonjour) browser and packet parser. It allows you to discover IPv4 addresses in the local network specifying a service name such as _http._tcp.local. Besides, it allows you to monitor mDNS/DNS-SD packets.
MIT License
44 stars 11 forks source link

Feature Request: Progress-Callback #12

Open tiptronic opened 1 year ago

tiptronic commented 1 year ago

Hi @futomi thanks for this nice library!

Here's a quick feature request, because I am missing a quick callback to allow for monitoring the progress of a discovery:

Since there is a delay until the discovery is parsed and the promise resolved, I added a callback function (progress), which is called whenever a new device is found. This allows updating the UI (or take another action) as soon as a device is dicovered (instead of waiting until the promise resolved).

Would you be open to add something like that into the library? (If you like, I can send a pull-request).

The addition would be very simple to implement (see below) and called like this:

mDnsSd.discover({
    name:  '_airplay._tcp.local',
    progress: (device) => {  // add a callback-function <---
        console.log('progress called with device', device?.address);
    },
}).then((device_list) => {
   // ...
}).catch((error) => {
    console.error(error);
});

Changes:

in dns-sd.js:: ~ line 247:

if('progress' in params) {
    const v = params['progress'];
    if(typeof (v) !== 'function') {
        return { error: new Error('`progress` must be a function.') };
    }
    p['progress'] = v;
}

in dns-sd.js:308:

const progress = params['progress'];

in dns-sd.js:320:

if (progress) {
    progress(device);
}