signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
// Test cases for version comparison
|
|
|
|
|
2023-08-10 15:50:08 +00:00
|
|
|
import 'resource:///org/gnome/shell/ui/environment.js';
|
2023-07-10 05:19:40 +00:00
|
|
|
import GObject from 'gi://GObject';
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
|
|
|
|
const JsUnit = imports.jsUnit;
|
2023-08-10 15:50:08 +00:00
|
|
|
import * as Signals from 'resource:///org/gnome/shell/misc/signals.js';
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
|
2023-08-10 15:50:08 +00:00
|
|
|
import {TransientSignalHolder, registerDestroyableType} from 'resource:///org/gnome/shell/misc/signalTracker.js';
|
2022-03-04 22:32:24 +00:00
|
|
|
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
const Destroyable = GObject.registerClass({
|
2023-07-10 05:59:46 +00:00
|
|
|
Signals: {'destroy': {}},
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
}, class Destroyable extends GObject.Object {});
|
2022-03-05 23:30:16 +00:00
|
|
|
registerDestroyableType(Destroyable);
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
|
|
|
|
const GObjectEmitter = GObject.registerClass({
|
2023-07-10 05:59:46 +00:00
|
|
|
Signals: {'signal': {}},
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
}, class GObjectEmitter extends Destroyable {});
|
|
|
|
|
2022-07-04 22:30:44 +00:00
|
|
|
const emitter1 = new Signals.EventEmitter();
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
const emitter2 = new GObjectEmitter();
|
|
|
|
|
|
|
|
const tracked1 = new Destroyable();
|
|
|
|
const tracked2 = {};
|
|
|
|
|
2023-07-10 05:59:46 +00:00
|
|
|
let transientHolder;
|
signalTracker: Provide monkey-patching for (dis)connectObject()
The module exports a `addObjectSignalMethods()` method that extends
the provided prototype with `connectObject()` and `disconnectObject()`
methods.
In its simplest form, `connectObject()` looks like the regular
`connect()` method, except for an additional parameter:
```js
this._button.connectObject('clicked',
() => this._onButtonClicked(), this);
```
The additional object can be used to disconnect all handlers on the
instance that were connected with that object, similar to
`g_signal_handlers_disconnect_by_data()` (which cannot be used
from introspection).
For objects that are subclasses of Clutter.Actor, that will happen
automatically when the actor is destroyed, similar to
`g_signal_connect_object()`.
Finally, `connectObject()` allows to conveniently connect multiple
signals at once, similar to `g_object_connect()`:
```js
this._toggleButton.connect(
'clicked', () => this._onClicked(),
'notify::checked', () => this._onChecked(), this);
```
Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1953>
2021-08-15 22:01:40 +00:00
|
|
|
let count = 0;
|
|
|
|
const handler = () => count++;
|
|
|
|
|
|
|
|
emitter1.connectObject('signal', handler, tracked1);
|
|
|
|
emitter2.connectObject('signal', handler, tracked1);
|
|
|
|
|
|
|
|
emitter1.connectObject('signal', handler, tracked2);
|
|
|
|
emitter2.connectObject('signal', handler, tracked2);
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 0);
|
|
|
|
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 4);
|
|
|
|
|
|
|
|
tracked1.emit('destroy');
|
|
|
|
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 6);
|
|
|
|
|
|
|
|
emitter1.disconnectObject(tracked2);
|
|
|
|
emitter2.emit('destroy');
|
|
|
|
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 6);
|
|
|
|
|
|
|
|
emitter1.connectObject(
|
|
|
|
'signal', handler,
|
|
|
|
'signal', handler, GObject.ConnectFlags.AFTER,
|
|
|
|
tracked1);
|
|
|
|
emitter2.connectObject(
|
|
|
|
'signal', handler,
|
|
|
|
'signal', handler, GObject.ConnectFlags.AFTER,
|
|
|
|
tracked1);
|
|
|
|
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 10);
|
|
|
|
|
|
|
|
tracked1.emit('destroy');
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 10);
|
2022-03-04 22:32:24 +00:00
|
|
|
|
|
|
|
emitter1.connectObject('signal', handler, tracked1);
|
|
|
|
emitter2.connectObject('signal', handler, tracked1);
|
|
|
|
|
|
|
|
transientHolder = new TransientSignalHolder(tracked1);
|
|
|
|
|
|
|
|
emitter1.connectObject('signal', handler, transientHolder);
|
|
|
|
emitter2.connectObject('signal', handler, transientHolder);
|
|
|
|
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 14);
|
|
|
|
|
|
|
|
transientHolder.destroy();
|
|
|
|
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 16);
|
|
|
|
|
|
|
|
transientHolder = new TransientSignalHolder(tracked1);
|
|
|
|
|
|
|
|
emitter1.connectObject('signal', handler, transientHolder);
|
|
|
|
emitter2.connectObject('signal', handler, transientHolder);
|
|
|
|
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 20);
|
|
|
|
|
|
|
|
tracked1.emit('destroy');
|
|
|
|
emitter1.emit('signal');
|
|
|
|
emitter2.emit('signal');
|
|
|
|
|
|
|
|
JsUnit.assertEquals(count, 20);
|