Skip to content

Commit fb2bd6d

Browse files
committed
util/promise: add null/undefined/0 checks
Gio.Cancellable.connect() will return 0 if already cancelled. Exception can be thrown at any point, leaving one or both handlers undefined.
1 parent c80be37 commit fb2bd6d

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

ddterm/util/promise.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ export async function wait_timeout(message, timeout_ms, cancellable = null) {
1212
await new Promise(resolve => {
1313
source = GLib.timeout_add(GLib.PRIORITY_DEFAULT, timeout_ms, () => {
1414
resolve();
15-
return GLib.SOURCE_CONTINUE;
15+
source = null;
16+
return GLib.SOURCE_REMOVE;
1617
});
1718

1819
cancel_handler = cancellable?.connect(() => {
1920
resolve();
2021
});
2122
});
2223
} finally {
23-
GLib.Source.remove(source);
24+
if (source)
25+
GLib.Source.remove(source);
26+
27+
if (cancel_handler)
28+
cancellable.disconnect(cancel_handler);
2429
}
2530

26-
cancellable?.disconnect(cancel_handler);
2731
cancellable?.set_error_if_cancelled();
2832

2933
throw GLib.Error.new_literal(Gio.io_error_quark(), Gio.IOErrorEnum.TIMED_OUT, message);
@@ -55,8 +59,11 @@ export async function wait_property(object, property, predicate, cancellable = n
5559
});
5660
});
5761
} finally {
58-
object.disconnect(handler);
59-
cancellable?.disconnect(cancel_handler);
62+
if (handler)
63+
object.disconnect(handler);
64+
65+
if (cancel_handler)
66+
cancellable.disconnect(cancel_handler);
6067
}
6168

6269
cancellable?.set_error_if_cancelled();

0 commit comments

Comments
 (0)