Skip to content

Commit

Permalink
util: Add ability to disconnect smart signal connections
Browse files Browse the repository at this point in the history
  • Loading branch information
3v1n0 committed Sep 29, 2022
1 parent 5f4baa4 commit 68c17c4
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

/* exported refreshPropertyOnProxy, getUniqueBusName, getBusNames,
introspectBusObject, dbusNodeImplementsInterfaces, waitForStartupCompletion,
connectSmart, versionCheck, getDefaultTheme, tryCleanupOldIndicators,
connectSmart, disconnectSmart, versionCheck, getDefaultTheme,
getProcessName, ensureProxyAsyncMethod, queueProxyPropertyUpdate,
getProxyProperty, indicatorId, BUS_ADDRESS_REGEX */
getProxyProperty, indicatorId, tryCleanupOldIndicators, BUS_ADDRESS_REGEX */

const ByteArray = imports.byteArray;
const Gio = imports.gi.Gio;
Expand Down Expand Up @@ -302,13 +302,16 @@ Signals.addSignalMethods(NameWatcher.prototype);

function connectSmart3A(src, signal, handler) {
let id = src.connect(signal, handler);
let destroyId = 0;

if (src.connect && (!(src instanceof GObject.Object) || GObject.signal_lookup('destroy', src))) {
let destroyId = src.connect('destroy', () => {
destroyId = src.connect('destroy', () => {
src.disconnect(id);
src.disconnect(destroyId);
});
}

return [id, destroyId];
}

function connectSmart4A(src, signal, target, method) {
Expand All @@ -331,6 +334,8 @@ function connectSmart4A(src, signal, target, method) {
GObject.signal_lookup('destroy', src)) ? src.connect('destroy', onDestroy) : 0;
const tgtDestroyId = target.connect && (!(target instanceof GObject.Object) ||
GObject.signal_lookup('destroy', target)) ? target.connect('destroy', onDestroy) : 0;

return [signalId, srcDestroyId, tgtDestroyId];
}

// eslint-disable-next-line valid-jsdoc
Expand All @@ -350,6 +355,32 @@ function connectSmart(...args) {
return connectSmart3A(...args);
}

function disconnectSmart3A(src, signalIds) {
const [id, destroyId] = signalIds;
src.disconnect(id);

if (destroyId)
src.disconnect(destroyId);
}

function disconnectSmart4A(src, tgt, signalIds) {
const [signalId, srcDestroyId, tgtDestroyId] = signalIds;

disconnectSmart3A(src, [signalId, srcDestroyId]);

if (tgtDestroyId)
tgt.disconnect(tgtDestroyId);
}

function disconnectSmart(...args) {
if (arguments.length === 2)
return disconnectSmart3A(...args);
else if (arguments.length === 3)
return disconnectSmart4A(...args);

throw new TypeError('Unexpected number of arguments');
}

function getDefaultTheme() {
if (Gdk.Screen.get_default()) {
const defaultTheme = Gtk.IconTheme.get_default();
Expand Down

0 comments on commit 68c17c4

Please sign in to comment.