Skip to content

Commit 5863856

Browse files
committed
avahi: better connection timeout handling
This doesn't rely on private members
1 parent 319e6e7 commit 5863856

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/dnssd/avahi.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as dbus from 'dbus-next';
55
import * as events from 'events';
66

77
import * as dnssd from '../dnssd';
8+
import { time } from 'console';
89

910
const PROTO_INET = 0;
1011
const PROTO_INET6 = 1;
@@ -56,13 +57,15 @@ async function getServer(): Promise<Server> {
5657
// dbus-next will queue messages and wait forever for a connection
5758
// so we have to hack in a timeout, otherwise we end up with a deadlock
5859
// on systems without D-Bus.
59-
await new Promise((resolve, reject) => setTimeout(() => {
60-
if ((bus as any)._connection.state === 'connected') {
61-
resolve();
62-
} else {
60+
await new Promise((resolve, reject) => {
61+
const timeout = setTimeout(() => {
6362
reject(Error("Timeout while connecting to D-Bus"));
64-
}
65-
}, 100));
63+
}, 100);
64+
(bus as any).on('connect', () => {
65+
clearTimeout(timeout);
66+
resolve();
67+
});
68+
});
6669
const proxy = await bus.getProxyObject('org.freedesktop.Avahi', '/');
6770
const server = proxy.getInterface<Server>('org.freedesktop.Avahi.Server');
6871
const version = await server.GetAPIVersion();

0 commit comments

Comments
 (0)