Adopt EventEmitter class instead of injecting Signal methods

Introduce a new class, EventEmitter, which implements signal
handling for pure JavaScript classes. EventEmitter still
utilizes GJS' addSignalMethods internally.

EventEmitter allows static typechecking to understand the
structure of event-emitting JS classes and makes creating
child classes simpler.

The name 'EventEmitter' mirrors a common name for this pattern
in Node and in JS libraries.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2043>
This commit is contained in:
Evan Welsh
2022-07-04 18:30:44 -04:00
parent 9e30afe678
commit a88e59c1a8
39 changed files with 204 additions and 169 deletions

View File

@ -1,4 +1,4 @@
/* exported TransientSignalHolder, addObjectSignalMethods */
/* exported TransientSignalHolder, connectObject, disconnectObject */
const { GObject } = imports.gi;
const destroyableTypes = [];
@ -214,25 +214,6 @@ function disconnectObject(thisObj, obj) {
SignalManager.getDefault().getSignalTracker(thisObj).untrack(obj);
}
/**
* Add connectObject()/disconnectObject() methods
* to prototype. The prototype must have the connect()
* and disconnect() signal methods.
*
* @param {prototype} proto - a prototype
*/
function addObjectSignalMethods(proto) {
proto['connectObject'] = function (...args) {
connectObject(this, ...args);
};
proto['connect_object'] = proto['connectObject'];
proto['disconnectObject'] = function (obj) {
disconnectObject(this, obj);
};
proto['disconnect_object'] = proto['disconnectObject'];
}
/**
* Register a GObject type as having a 'destroy' signal
* that should disconnect all handlers