@gjsify/node-gi 0.13.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +818 -5
- package/binding.gyp +55 -19
- package/cairo.d.ts +187 -0
- package/cairo.js +570 -0
- package/gettext.d.ts +65 -0
- package/gettext.js +128 -0
- package/gi.d.ts +178 -0
- package/gi.js +2018 -23
- package/globals.d.ts +7 -33
- package/globals.js +40 -51
- package/gtk-runtime.d.ts +17 -0
- package/gtk-runtime.js +245 -0
- package/index.d.ts +306 -12
- package/index.js +447 -14
- package/overrides/_signals.js +167 -0
- package/overrides/gio-dbus.js +749 -0
- package/overrides/mainloop.js +60 -0
- package/package.json +45 -4
- package/src/addon.cc +98 -2074
- package/src/cairo.cc +926 -0
- package/src/calls.cc +1425 -0
- package/src/class.cc +1170 -0
- package/src/common.h +624 -0
- package/src/loop.cc +766 -0
- package/src/marshal.cc +1738 -0
- package/src/object.cc +814 -0
- package/src/private.cc +351 -0
- package/src/repo.cc +297 -0
- package/src/signals.cc +535 -0
- package/src/template.cc +116 -0
- package/src/toggle.cc +718 -0
- package/src/variant.cc +587 -0
- package/system.d.ts +49 -0
- package/system.js +116 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
|
|
2
|
+
// SPDX-FileCopyrightText: 2008 litl, LLC
|
|
3
|
+
// SPDX-FileCopyrightText: 2022 Canonical Ltd.
|
|
4
|
+
//
|
|
5
|
+
// Adapted from GJS (refs/gjs/modules/core/_signals.js). Copyright (c) 2008
|
|
6
|
+
// litl, LLC; 2022 Canonical Ltd. MIT OR LGPL-2.0-or-later.
|
|
7
|
+
// Modifications: ported near-verbatim as an ESM module for @gjsify/node-gi; the
|
|
8
|
+
// individual `_connect`/`_disconnect`/`_emit`/… functions are named-exported so
|
|
9
|
+
// both the DBus proxy surface (overrides/gio-dbus.js) and the legacy
|
|
10
|
+
// `imports.signals` (globals.js) reuse the SAME mixin, exactly as GJS does.
|
|
11
|
+
//
|
|
12
|
+
// A couple principals of this simple signal system:
|
|
13
|
+
// 1) should look just like our GObject signal binding
|
|
14
|
+
// 2) memory and safety matter more than speed of connect/disconnect/emit
|
|
15
|
+
// 3) the expectation is that a given object will have a very small number of
|
|
16
|
+
// connections, but they may be to different signal names
|
|
17
|
+
|
|
18
|
+
function _connectFull(name, callback, after) {
|
|
19
|
+
// be paranoid about callback arg since we'd start to throw from emit()
|
|
20
|
+
// if it was messed up
|
|
21
|
+
if (typeof callback !== 'function')
|
|
22
|
+
throw new Error('When connecting signal must give a callback that is a function');
|
|
23
|
+
|
|
24
|
+
// we instantiate the "signal machinery" only on-demand if anything
|
|
25
|
+
// gets connected.
|
|
26
|
+
if (this._signalConnections === undefined) {
|
|
27
|
+
this._signalConnections = Object.create(null);
|
|
28
|
+
this._signalConnectionsByName = Object.create(null);
|
|
29
|
+
this._nextConnectionId = 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const id = this._nextConnectionId;
|
|
33
|
+
this._nextConnectionId += 1;
|
|
34
|
+
|
|
35
|
+
this._signalConnections[id] = {
|
|
36
|
+
name,
|
|
37
|
+
callback,
|
|
38
|
+
after,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const connectionsByName = this._signalConnectionsByName[name] ?? [];
|
|
42
|
+
|
|
43
|
+
if (!connectionsByName.length)
|
|
44
|
+
this._signalConnectionsByName[name] = connectionsByName;
|
|
45
|
+
connectionsByName.push(id);
|
|
46
|
+
|
|
47
|
+
return id;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function _connect(name, callback) {
|
|
51
|
+
return _connectFull.call(this, name, callback, false);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function _connectAfter(name, callback) {
|
|
55
|
+
return _connectFull.call(this, name, callback, true);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function _disconnect(id) {
|
|
59
|
+
const connection = this._signalConnections?.[id];
|
|
60
|
+
|
|
61
|
+
if (!connection)
|
|
62
|
+
throw new Error(`No signal connection ${id} found`);
|
|
63
|
+
|
|
64
|
+
if (connection.disconnected)
|
|
65
|
+
throw new Error(`Signal handler id ${id} already disconnected`);
|
|
66
|
+
|
|
67
|
+
connection.disconnected = true;
|
|
68
|
+
delete this._signalConnections[id];
|
|
69
|
+
|
|
70
|
+
const ids = this._signalConnectionsByName[connection.name];
|
|
71
|
+
if (!ids)
|
|
72
|
+
return;
|
|
73
|
+
|
|
74
|
+
const indexOfId = ids.indexOf(id);
|
|
75
|
+
if (indexOfId !== -1)
|
|
76
|
+
ids.splice(indexOfId, 1);
|
|
77
|
+
|
|
78
|
+
if (ids.length === 0)
|
|
79
|
+
delete this._signalConnectionsByName[connection.name];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function _signalHandlerIsConnected(id) {
|
|
83
|
+
const connection = this._signalConnections?.[id];
|
|
84
|
+
return !!connection && !connection.disconnected;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function _disconnectAll() {
|
|
88
|
+
Object.values(this._signalConnections ?? {}).forEach(c => (c.disconnected = true));
|
|
89
|
+
delete this._signalConnections;
|
|
90
|
+
delete this._signalConnectionsByName;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function _emit(name, ...args) {
|
|
94
|
+
const connections = this._signalConnectionsByName?.[name];
|
|
95
|
+
|
|
96
|
+
// may not be any signal handlers at all, if not then return
|
|
97
|
+
if (!connections)
|
|
98
|
+
return;
|
|
99
|
+
|
|
100
|
+
// To deal with re-entrancy (removal/addition while
|
|
101
|
+
// emitting), we copy out a list of what was connected
|
|
102
|
+
// at emission start; and just before invoking each
|
|
103
|
+
// handler we check its disconnected flag.
|
|
104
|
+
const handlers = connections.map(id => this._signalConnections[id]);
|
|
105
|
+
|
|
106
|
+
// create arg array which is emitter + everything passed in except
|
|
107
|
+
// signal name. Would be more convenient not to pass emitter to
|
|
108
|
+
// the callback, but trying to be 100% consistent with GObject
|
|
109
|
+
// which does pass it in. Also if we pass in the emitter here,
|
|
110
|
+
// people don't create closures with the emitter in them,
|
|
111
|
+
// which would be a cycle.
|
|
112
|
+
const argArray = [this, ...args];
|
|
113
|
+
|
|
114
|
+
const afterHandlers = [];
|
|
115
|
+
const beforeHandlers = handlers.filter(c => {
|
|
116
|
+
if (!c.after)
|
|
117
|
+
return true;
|
|
118
|
+
|
|
119
|
+
afterHandlers.push(c);
|
|
120
|
+
return false;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if (!_callHandlers(beforeHandlers, argArray))
|
|
124
|
+
_callHandlers(afterHandlers, argArray);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function _callHandlers(handlers, argArray) {
|
|
128
|
+
for (const handler of handlers) {
|
|
129
|
+
if (handler.disconnected)
|
|
130
|
+
continue;
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
// since we pass "null" for this, the global object will be used.
|
|
134
|
+
const ret = handler.callback.apply(null, argArray);
|
|
135
|
+
|
|
136
|
+
// if the callback returns true, we don't call the next
|
|
137
|
+
// signal handlers
|
|
138
|
+
if (ret === true)
|
|
139
|
+
return true;
|
|
140
|
+
} catch (e) {
|
|
141
|
+
// just log any exceptions so that callbacks can't disrupt
|
|
142
|
+
// signal emission
|
|
143
|
+
logError(e, `Exception in callback for signal: ${handler.name}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function _addSignalMethod(proto, functionName, func) {
|
|
151
|
+
if (proto[functionName] && proto[functionName] !== func)
|
|
152
|
+
log(`WARNING: addSignalMethods is replacing existing ${proto} ${functionName} method`);
|
|
153
|
+
|
|
154
|
+
proto[functionName] = func;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function addSignalMethods(proto) {
|
|
158
|
+
_addSignalMethod(proto, 'connect', _connect);
|
|
159
|
+
_addSignalMethod(proto, 'connectAfter', _connectAfter);
|
|
160
|
+
_addSignalMethod(proto, 'disconnect', _disconnect);
|
|
161
|
+
_addSignalMethod(proto, 'emit', _emit);
|
|
162
|
+
_addSignalMethod(proto, 'signalHandlerIsConnected', _signalHandlerIsConnected);
|
|
163
|
+
// this one is not in GObject, but useful
|
|
164
|
+
_addSignalMethod(proto, 'disconnectAll', _disconnectAll);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export default { addSignalMethods, _connect, _connectAfter, _disconnect, _emit, _signalHandlerIsConnected, _disconnectAll };
|