@gtkx/runtime 1.0.0-rc.3 → 1.0.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 +8 -9
- package/dist/application-class.d.ts +32 -0
- package/dist/application-class.d.ts.map +1 -0
- package/dist/application-class.js +68 -0
- package/dist/application-class.js.map +1 -0
- package/dist/arg.d.ts +9 -3
- package/dist/arg.d.ts.map +1 -1
- package/dist/arg.js +4 -3
- package/dist/arg.js.map +1 -1
- package/dist/bind.d.ts +11 -0
- package/dist/bind.d.ts.map +1 -1
- package/dist/bind.js +11 -0
- package/dist/bind.js.map +1 -1
- package/dist/callback.d.ts +2 -1
- package/dist/callback.d.ts.map +1 -1
- package/dist/callback.js +119 -21
- package/dist/callback.js.map +1 -1
- package/dist/closure.d.ts +26 -0
- package/dist/closure.d.ts.map +1 -0
- package/dist/closure.js +99 -0
- package/dist/closure.js.map +1 -0
- package/dist/descriptors.d.ts +105 -6
- package/dist/descriptors.d.ts.map +1 -1
- package/dist/descriptors.js +56 -12
- package/dist/descriptors.js.map +1 -1
- package/dist/error.d.ts +7 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +1 -1
- package/dist/error.js.map +1 -1
- package/dist/fn.d.ts +24 -3
- package/dist/fn.d.ts.map +1 -1
- package/dist/fn.js +70 -33
- package/dist/fn.js.map +1 -1
- package/dist/index.d.ts +29 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -4
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +1 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +1 -0
- package/dist/internal.js.map +1 -1
- package/dist/library.d.ts +2 -1
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +2 -1
- package/dist/library.js.map +1 -1
- package/dist/lifecycle.d.ts +52 -10
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +62 -18
- package/dist/lifecycle.js.map +1 -1
- package/dist/listeners.d.ts +6 -0
- package/dist/listeners.d.ts.map +1 -1
- package/dist/listeners.js +3 -1
- package/dist/listeners.js.map +1 -1
- package/dist/mixin.d.ts +16 -2
- package/dist/mixin.d.ts.map +1 -1
- package/dist/mixin.js +18 -6
- package/dist/mixin.js.map +1 -1
- package/dist/native-value.d.ts +8 -0
- package/dist/native-value.d.ts.map +1 -1
- package/dist/native-value.js +12 -3
- package/dist/native-value.js.map +1 -1
- package/dist/object.d.ts +34 -6
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +89 -25
- package/dist/object.js.map +1 -1
- package/dist/param-spec.d.ts +11 -0
- package/dist/param-spec.d.ts.map +1 -0
- package/dist/param-spec.js +149 -0
- package/dist/param-spec.js.map +1 -0
- package/dist/promisify.d.ts +3 -5
- package/dist/promisify.d.ts.map +1 -1
- package/dist/promisify.js +4 -3
- package/dist/promisify.js.map +1 -1
- package/dist/properties.d.ts +39 -7
- package/dist/properties.d.ts.map +1 -1
- package/dist/properties.js +305 -42
- package/dist/properties.js.map +1 -1
- package/dist/register-class.d.ts +119 -14
- package/dist/register-class.d.ts.map +1 -1
- package/dist/register-class.js +109 -80
- package/dist/register-class.js.map +1 -1
- package/dist/registry.d.ts +81 -17
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +72 -26
- package/dist/registry.js.map +1 -1
- package/dist/signal.d.ts +22 -3
- package/dist/signal.d.ts.map +1 -1
- package/dist/signal.js +64 -8
- package/dist/signal.js.map +1 -1
- package/dist/t.d.ts +44 -0
- package/dist/t.d.ts.map +1 -1
- package/dist/t.js.map +1 -1
- package/dist/type.d.ts +2 -2
- package/dist/type.d.ts.map +1 -1
- package/dist/type.js +2 -0
- package/dist/type.js.map +1 -1
- package/dist/value.d.ts +10 -10
- package/dist/value.d.ts.map +1 -1
- package/dist/value.js +44 -22
- package/dist/value.js.map +1 -1
- package/dist/vfunc-call.d.ts +37 -0
- package/dist/vfunc-call.d.ts.map +1 -0
- package/dist/vfunc-call.js +168 -0
- package/dist/vfunc-call.js.map +1 -0
- package/dist/vfunc-seeds.d.ts +12 -0
- package/dist/vfunc-seeds.d.ts.map +1 -0
- package/dist/vfunc-seeds.js +19 -0
- package/dist/vfunc-seeds.js.map +1 -0
- package/dist/vfunc.d.ts +8 -0
- package/dist/vfunc.d.ts.map +1 -0
- package/dist/vfunc.js +23 -0
- package/dist/vfunc.js.map +1 -0
- package/package.json +4 -4
- package/src/application-class.ts +116 -0
- package/src/arg.ts +11 -5
- package/src/bind.ts +11 -0
- package/src/callback.ts +163 -23
- package/src/closure.ts +136 -0
- package/src/descriptors.ts +137 -41
- package/src/error.ts +8 -1
- package/src/fn.ts +104 -39
- package/src/index.ts +36 -7
- package/src/internal.ts +1 -0
- package/src/library.ts +2 -1
- package/src/lifecycle.ts +99 -22
- package/src/listeners.ts +9 -1
- package/src/mixin.ts +27 -8
- package/src/native-value.ts +13 -3
- package/src/object.ts +128 -34
- package/src/param-spec.ts +235 -0
- package/src/promisify.ts +8 -8
- package/src/properties.ts +459 -45
- package/src/register-class.ts +329 -114
- package/src/registry.ts +150 -38
- package/src/signal.ts +101 -17
- package/src/t.ts +44 -0
- package/src/type.ts +3 -2
- package/src/value.ts +57 -19
- package/src/vfunc-call.ts +243 -0
- package/src/vfunc-seeds.ts +36 -0
- package/src/vfunc.ts +31 -0
package/dist/registry.js
CHANGED
|
@@ -1,21 +1,38 @@
|
|
|
1
1
|
import { getType, getWrapper, setWrapper, } from "@gtkx/native";
|
|
2
|
+
import { walkClassChain } from "@gtkx/utils";
|
|
2
3
|
import { TYPE_INVALID, typeInterfaces, typeIsA, typeName, typeParent } from "./type.js";
|
|
3
4
|
const classRegistry = new Map();
|
|
4
5
|
const interfaceMixinRegistry = new Map();
|
|
5
6
|
const composedClassRegistry = new Map();
|
|
6
7
|
const handleMap = new WeakMap();
|
|
7
8
|
const vfuncRegistry = new WeakMap();
|
|
8
|
-
const
|
|
9
|
+
const interfaceLayoutRegistry = new Map();
|
|
10
|
+
const wrapperClasses = new WeakSet();
|
|
11
|
+
const derivedClasses = new WeakSet();
|
|
9
12
|
function setClassType(cls, type) {
|
|
10
13
|
cls.prototype.__type__ = type;
|
|
11
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Returns the GType a class was registered under, or the invalid type when it carries none. The tag is
|
|
17
|
+
* read off the class's own prototype, so a subclass that never went through `registerClass` reports the
|
|
18
|
+
* invalid type rather than inheriting the one its parent was registered with.
|
|
19
|
+
*/
|
|
12
20
|
function getClassType(cls) {
|
|
13
|
-
const proto = cls
|
|
14
|
-
|
|
21
|
+
const proto = cls?.prototype;
|
|
22
|
+
if (proto === undefined || !Object.hasOwn(proto, "__type__")) {
|
|
23
|
+
return TYPE_INVALID;
|
|
24
|
+
}
|
|
25
|
+
return proto.__type__;
|
|
15
26
|
}
|
|
16
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Returns the GType the given instance's handle carries, or the invalid type when it has no handle.
|
|
29
|
+
* This is the object's own type rather than the type of the class it was wrapped as, and the two differ
|
|
30
|
+
* for an object GTK created itself, such as the row widget inside a `Gtk.ListView`, which GTKX wraps as
|
|
31
|
+
* the nearest registered ancestor.
|
|
32
|
+
*/
|
|
17
33
|
function getInstanceType(instance) {
|
|
18
|
-
|
|
34
|
+
const handle = handleMap.get(instance);
|
|
35
|
+
return handle === undefined ? TYPE_INVALID : getType(handle);
|
|
19
36
|
}
|
|
20
37
|
function registerClassType(cls, type) {
|
|
21
38
|
if (type === TYPE_INVALID) {
|
|
@@ -29,30 +46,51 @@ function registerClassType(cls, type) {
|
|
|
29
46
|
* installing a registry of virtual functions.
|
|
30
47
|
* @param cls Wrapper class to associate with the type.
|
|
31
48
|
* @param type GType the class wraps.
|
|
32
|
-
* @param vfuncs
|
|
49
|
+
* @param vfuncs Vtable slots the class exposes, so `registerClass` can bind the ones a subclass
|
|
50
|
+
* overrides.
|
|
33
51
|
*/
|
|
34
52
|
function registerWrapperClass(cls, type, vfuncs) {
|
|
35
53
|
registerClassType(cls, type);
|
|
54
|
+
if (type !== TYPE_INVALID) {
|
|
55
|
+
wrapperClasses.add(cls);
|
|
56
|
+
}
|
|
36
57
|
if (vfuncs) {
|
|
37
58
|
registerVfuncRegistry(cls, vfuncs);
|
|
38
59
|
}
|
|
39
60
|
}
|
|
61
|
+
function markDerivedClass(cls) {
|
|
62
|
+
derivedClasses.add(cls);
|
|
63
|
+
}
|
|
64
|
+
function resolveAncestorType(ancestor) {
|
|
65
|
+
if (derivedClasses.has(ancestor) || !wrapperClasses.has(ancestor)) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
return getClassType(ancestor);
|
|
69
|
+
}
|
|
70
|
+
function resolveWrapperType(instance) {
|
|
71
|
+
const cls = instance.constructor;
|
|
72
|
+
if (cls === undefined) {
|
|
73
|
+
return TYPE_INVALID;
|
|
74
|
+
}
|
|
75
|
+
return walkClassChain(cls, (ancestor) => resolveAncestorType(ancestor)) ?? TYPE_INVALID;
|
|
76
|
+
}
|
|
40
77
|
/**
|
|
41
78
|
* Registers a GInterface, associating its GType with a mixin used to compose the
|
|
42
|
-
* interface onto wrapper classes and
|
|
79
|
+
* interface onto wrapper classes and, when introspection describes its vtable, that layout.
|
|
43
80
|
* @param cls Class carrying the interface's GType tag.
|
|
44
81
|
* @param type GType of the interface.
|
|
45
82
|
* @param mixin Mixin that applies the interface to a wrapper class.
|
|
46
|
-
* @param
|
|
83
|
+
* @param layout The interface's vtable struct, so `registerClass` can bind the slots an
|
|
84
|
+
* implementing class overrides and take over the ones it leaves alone.
|
|
47
85
|
*/
|
|
48
|
-
function registerInterface(cls, type, mixin,
|
|
86
|
+
function registerInterface(cls, type, mixin, layout) {
|
|
49
87
|
if (type === TYPE_INVALID) {
|
|
50
88
|
return;
|
|
51
89
|
}
|
|
52
90
|
setClassType(cls, type);
|
|
53
91
|
interfaceMixinRegistry.set(type, mixin);
|
|
54
|
-
if (
|
|
55
|
-
|
|
92
|
+
if (layout) {
|
|
93
|
+
interfaceLayoutRegistry.set(type, layout);
|
|
56
94
|
}
|
|
57
95
|
}
|
|
58
96
|
function wrapHandle(handle, cls) {
|
|
@@ -88,11 +126,14 @@ function resolveWrapperClass(type) {
|
|
|
88
126
|
}
|
|
89
127
|
return null;
|
|
90
128
|
}
|
|
129
|
+
function getInterfaceMixin(type) {
|
|
130
|
+
return interfaceMixinRegistry.get(type);
|
|
131
|
+
}
|
|
91
132
|
function applyInterfaceMixin(cls, type, baseType, applied) {
|
|
92
133
|
if (applied.has(type) || typeIsA(baseType, type)) {
|
|
93
134
|
return cls;
|
|
94
135
|
}
|
|
95
|
-
const mixin =
|
|
136
|
+
const mixin = getInterfaceMixin(type);
|
|
96
137
|
if (mixin === undefined) {
|
|
97
138
|
return cls;
|
|
98
139
|
}
|
|
@@ -126,10 +167,20 @@ function resolveComposedClass(runtimeType) {
|
|
|
126
167
|
return base;
|
|
127
168
|
}
|
|
128
169
|
setClassType(composed, runtimeType);
|
|
170
|
+
wrapperClasses.add(composed);
|
|
129
171
|
composedClassRegistry.set(runtimeType, composed);
|
|
130
172
|
return composed;
|
|
131
173
|
}
|
|
174
|
+
function wrapObject(value) {
|
|
175
|
+
if (value == null) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
return getOrCreateWrapper(value);
|
|
179
|
+
}
|
|
132
180
|
function getOrCreateWrapper(handle) {
|
|
181
|
+
if (handleMap.has(handle)) {
|
|
182
|
+
return handle;
|
|
183
|
+
}
|
|
133
184
|
const existing = getWrapper(handle);
|
|
134
185
|
if (existing) {
|
|
135
186
|
return existing;
|
|
@@ -146,19 +197,17 @@ function getOrCreateWrapper(handle) {
|
|
|
146
197
|
registerWrapper(handle, instance);
|
|
147
198
|
return instance;
|
|
148
199
|
}
|
|
200
|
+
function instanceClassName(instance) {
|
|
201
|
+
return instance.constructor?.name ?? "object";
|
|
202
|
+
}
|
|
149
203
|
/** Returns the native handle bound to a wrapper instance, throwing if none is set. */
|
|
150
204
|
function getHandle(instance) {
|
|
151
205
|
const handle = handleMap.get(instance);
|
|
152
206
|
if (handle === undefined) {
|
|
153
|
-
|
|
154
|
-
throw new Error(`No native handle associated with ${name}`);
|
|
207
|
+
throw new Error(`No native handle associated with ${instanceClassName(instance)}`);
|
|
155
208
|
}
|
|
156
209
|
return handle;
|
|
157
210
|
}
|
|
158
|
-
/** Returns the native handle bound to an instance, or undefined when there is none or the instance is null. */
|
|
159
|
-
function tryGetHandle(instance) {
|
|
160
|
-
return instance == null ? undefined : handleMap.get(instance);
|
|
161
|
-
}
|
|
162
211
|
/** Associates a native handle with a wrapper instance. */
|
|
163
212
|
function setHandle(instance, handle) {
|
|
164
213
|
handleMap.set(instance, handle);
|
|
@@ -173,14 +222,11 @@ function registerVfuncRegistry(cls, registry) {
|
|
|
173
222
|
function getVfuncRegistry(cls) {
|
|
174
223
|
return vfuncRegistry.get(cls);
|
|
175
224
|
}
|
|
176
|
-
function registerInterfaceVfuncRegistry(type, registry) {
|
|
177
|
-
if (type === TYPE_INVALID) {
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
interfaceVfuncRegistry.set(type, registry);
|
|
181
|
-
}
|
|
182
225
|
function getInterfaceVfuncRegistry(type) {
|
|
183
|
-
return
|
|
226
|
+
return interfaceLayoutRegistry.get(type)?.vfuncs;
|
|
227
|
+
}
|
|
228
|
+
function getInterfaceProperties(type) {
|
|
229
|
+
return interfaceLayoutRegistry.get(type)?.properties;
|
|
184
230
|
}
|
|
185
|
-
export { getClassType, getInstanceType, registerClassType, registerWrapperClass, registerInterface, wrapHandle, getWrapperClass, resolveWrapperClass, getHandle,
|
|
231
|
+
export { getClassType, getInstanceType, markDerivedClass, registerClassType, registerWrapperClass, registerInterface, wrapHandle, getWrapperClass, resolveWrapperClass, getHandle, setHandle, getVfuncRegistry, getInterfaceMixin, getInterfaceProperties, getInterfaceVfuncRegistry, instanceClassName, registerWrapper, resolveWrapperType, wrapObject, };
|
|
186
232
|
//# sourceMappingURL=registry.js.map
|
package/dist/registry.js.map
CHANGED
|
@@ -1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Aa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAClC,CAAC;AAED,SAAS,8BAA8B,CAAC,IAAY,EAAE,QAAuB;IACzE,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;QACxB,OAAO;IACX,CAAC;IAED,sBAAsB,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,yBAAyB,CAAC,IAAY;IAC3C,OAAO,sBAAsB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC5C,CAAC;AAED,OAAO,EACH,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,EACjB,UAAU,EACV,eAAe,EACf,mBAAmB,EACnB,SAAS,EACT,YAAY,EACZ,SAAS,EACT,gBAAgB,EAChB,yBAAyB,GAI5B,CAAC","sourcesContent":["import type { AnyClass } from \"@gtkx/utils\";\nimport {\n type ExternalObject,\n getType,\n getWrapper,\n type Handle,\n type RegisterClassVfunc as NativeRegisterClassVfunc,\n setWrapper,\n} from \"@gtkx/native\";\nimport type { Mixin, MixinReceiver } from \"./mixin.js\";\nimport { TYPE_INVALID, type TypedClass, typeInterfaces, typeIsA, typeName, typeParent } from \"./type.js\";\n\n/**\n * Static side of class `C` with its construct signature preserved but the member\n * named `K` (default `\"new\"`) removed.\n */\ntype StaticBase<C, K extends PropertyKey = \"new\"> = Omit<C, K> &\n (C extends new (...args: infer A) => infer R ? new (...args: A) => R : never);\n\ntype VfuncDescriptor<K extends \"class\" | \"interface\"> = {\n kind: K;\n className: string;\n vfuncName: string;\n byteOffset: number;\n vtableSize: number;\n argDescriptors: NativeRegisterClassVfunc[\"argDescriptors\"];\n returnDescriptor: NativeRegisterClassVfunc[\"returnDescriptor\"];\n};\n\ntype VfuncRegistry = Record<string, VfuncDescriptor<\"class\"> | VfuncDescriptor<\"interface\">>;\n\nconst classRegistry: Map<bigint, AnyClass> = new Map();\nconst interfaceMixinRegistry: Map<bigint, Mixin> = new Map();\nconst composedClassRegistry: Map<bigint, AnyClass> = new Map();\nconst handleMap: WeakMap<object, ExternalObject<Handle>> = new WeakMap();\nconst vfuncRegistry: WeakMap<object, VfuncRegistry> = new WeakMap();\nconst interfaceVfuncRegistry: Map<bigint, VfuncRegistry> = new Map();\n\nfunction setClassType(cls: AnyClass, type: bigint): void {\n (cls.prototype as { [K in keyof TypedClass]: TypedClass[K] }).__type__ = type;\n}\n\nfunction getClassType(cls: AnyClass): bigint {\n const proto: object = cls.prototype;\n\n return Object.hasOwn(proto, \"__type__\") ? (proto as TypedClass).__type__ : TYPE_INVALID;\n}\n\n/** Returns the GType tag of the given wrapper instance. */\nfunction getInstanceType(instance: object): bigint {\n return getClassType(instance.constructor as AnyClass);\n}\n\nfunction registerClassType(cls: AnyClass, type: bigint): void {\n if (type === TYPE_INVALID) {\n return;\n }\n\n classRegistry.set(type, cls);\n setClassType(cls, type);\n}\n\n/**\n * Registers a wrapper class as the JS representation of a GType, optionally\n * installing a registry of virtual functions.\n * @param cls Wrapper class to associate with the type.\n * @param type GType the class wraps.\n * @param vfuncs Virtual functions the class overrides.\n */\nfunction registerWrapperClass(cls: AnyClass, type: bigint, vfuncs?: VfuncRegistry): void {\n registerClassType(cls, type);\n\n if (vfuncs) {\n registerVfuncRegistry(cls, vfuncs);\n }\n}\n\n/**\n * Registers a GInterface, associating its GType with a mixin used to compose the\n * interface onto wrapper classes and an optional virtual function registry.\n * @param cls Class carrying the interface's GType tag.\n * @param type GType of the interface.\n * @param mixin Mixin that applies the interface to a wrapper class.\n * @param vfuncs Virtual functions the interface exposes.\n */\nfunction registerInterface(cls: AnyClass, type: bigint, mixin: Mixin, vfuncs?: VfuncRegistry): void {\n if (type === TYPE_INVALID) {\n return;\n }\n\n setClassType(cls, type);\n interfaceMixinRegistry.set(type, mixin);\n\n if (vfuncs) {\n registerInterfaceVfuncRegistry(type, vfuncs);\n }\n}\n\n/**\n * Wraps a native handle in a JS wrapper instance. With no class, resolves and\n * reuses the wrapper for the handle's runtime GType (composing interface mixins);\n * with an explicit class, creates a bare instance backed by the handle. Returns\n * null for a null or undefined handle.\n * @param handle Native handle to wrap.\n * @param cls Wrapper class to instantiate, or omitted to resolve it from the runtime type.\n */\nfunction wrapHandle(handle: null | undefined, cls?: AnyClass): null;\nfunction wrapHandle<T extends object>(handle: ExternalObject<Handle>, cls: AnyClass<T>): T;\n\nfunction wrapHandle<T extends object>(\n handle: ExternalObject<Handle> | null | undefined,\n cls: AnyClass<T>,\n): T | null;\n\nfunction wrapHandle(handle: ExternalObject<Handle>, cls?: AnyClass): TypedClass;\nfunction wrapHandle(handle: ExternalObject<Handle> | null | undefined, cls?: AnyClass): TypedClass | null;\n\nfunction wrapHandle(handle: ExternalObject<Handle> | null | undefined, cls?: AnyClass): object | null {\n if (handle === null || handle === undefined) {\n return null;\n }\n\n if (cls === undefined) {\n return getOrCreateWrapper(handle);\n }\n\n const instance: object = Object.create(cls.prototype) as object;\n setHandle(instance, handle);\n\n return instance;\n}\n\n/**\n * Returns the wrapper class registered for a GType, walking up to ancestor types,\n * and throws if none is registered.\n */\nfunction getWrapperClass(type: bigint): AnyClass {\n const cls = resolveWrapperClass(type);\n\n if (!cls) {\n throw new Error(`No registered wrapper class for type '${typeName(type) ?? String(type)}'`);\n }\n\n return cls;\n}\n\nfunction resolveWrapperClass(type: bigint): AnyClass | null {\n let currentType = type;\n\n while (currentType !== TYPE_INVALID) {\n const cls = classRegistry.get(currentType);\n\n if (cls) {\n return cls;\n }\n\n currentType = typeParent(currentType);\n }\n\n return null;\n}\n\nfunction applyInterfaceMixin(cls: AnyClass, type: bigint, baseType: bigint, applied: Set<bigint>): AnyClass {\n if (applied.has(type) || typeIsA(baseType, type)) {\n return cls;\n }\n\n const mixin = interfaceMixinRegistry.get(type);\n\n if (mixin === undefined) {\n return cls;\n }\n\n applied.add(type);\n\n return mixin(cls as AnyClass<MixinReceiver>);\n}\n\nfunction createComposedClass(base: AnyClass, runtimeType: bigint): AnyClass {\n const baseType = getClassType(base);\n const applied: Set<bigint> = new Set();\n let cls: AnyClass = base;\n\n for (const type of typeInterfaces(runtimeType)) {\n cls = applyInterfaceMixin(cls, type, baseType, applied);\n }\n\n return applied.size === 0 ? base : cls;\n}\n\nfunction resolveComposedClass(runtimeType: bigint): AnyClass | null {\n const exact = classRegistry.get(runtimeType);\n\n if (exact) {\n return exact;\n }\n\n const cached = composedClassRegistry.get(runtimeType);\n\n if (cached) {\n return cached;\n }\n\n const base = resolveWrapperClass(runtimeType);\n\n if (base === null) {\n return null;\n }\n\n const composed = createComposedClass(base, runtimeType);\n\n if (composed === base) {\n return base;\n }\n\n setClassType(composed, runtimeType);\n composedClassRegistry.set(runtimeType, composed);\n\n return composed;\n}\n\nfunction getOrCreateWrapper(handle: ExternalObject<Handle>): object {\n const existing = getWrapper(handle);\n\n if (existing) {\n return existing;\n }\n\n const runtimeType: bigint = getType(handle);\n\n if (runtimeType === TYPE_INVALID) {\n throw new Error(\"Cannot resolve runtime GLib type from handle\");\n }\n\n const cls = resolveComposedClass(runtimeType);\n\n if (!cls) {\n throw new Error(`Expected registered GLib type, got type ${String(runtimeType)}`);\n }\n\n const instance: object = Object.create(cls.prototype) as object;\n registerWrapper(handle, instance);\n\n return instance;\n}\n\n/** Returns the native handle bound to a wrapper instance, throwing if none is set. */\nfunction getHandle(instance: object): ExternalObject<Handle> {\n const handle = handleMap.get(instance);\n\n if (handle === undefined) {\n const name = (instance as { constructor?: { name?: string } }).constructor?.name ?? \"object\";\n throw new Error(`No native handle associated with ${name}`);\n }\n\n return handle;\n}\n\n/** Returns the native handle bound to an instance, or undefined when there is none or the instance is null. */\nfunction tryGetHandle(instance: object | null | undefined): ExternalObject<Handle> | undefined {\n return instance == null ? undefined : handleMap.get(instance);\n}\n\n/** Associates a native handle with a wrapper instance. */\nfunction setHandle(instance: object, handle: ExternalObject<Handle>): void {\n handleMap.set(instance, handle);\n}\n\nfunction registerWrapper(handle: ExternalObject<Handle>, instance: object): void {\n setHandle(instance, handle);\n setWrapper(handle, instance);\n}\n\nfunction registerVfuncRegistry(cls: object, registry: VfuncRegistry): void {\n vfuncRegistry.set(cls, registry);\n}\n\nfunction getVfuncRegistry(cls: object): VfuncRegistry | undefined {\n return vfuncRegistry.get(cls);\n}\n\nfunction registerInterfaceVfuncRegistry(type: bigint, registry: VfuncRegistry): void {\n if (type === TYPE_INVALID) {\n return;\n }\n\n interfaceVfuncRegistry.set(type, registry);\n}\n\nfunction getInterfaceVfuncRegistry(type: bigint): VfuncRegistry | undefined {\n return interfaceVfuncRegistry.get(type);\n}\n\nexport {\n getClassType,\n getInstanceType,\n registerClassType,\n registerWrapperClass,\n registerInterface,\n wrapHandle,\n getWrapperClass,\n resolveWrapperClass,\n getHandle,\n tryGetHandle,\n setHandle,\n getVfuncRegistry,\n getInterfaceVfuncRegistry,\n type StaticBase,\n type VfuncDescriptor,\n type VfuncRegistry,\n};\n"]}
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,OAAO,EACP,UAAU,EAGV,UAAU,GACb,MAAM,cAAc,CAAC;AACtB,OAAO,EAAiB,cAAc,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,YAAY,EAAmB,cAAc,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAqEzG,MAAM,aAAa,GAA0B,IAAI,GAAG,EAAE,CAAC;AACvD,MAAM,sBAAsB,GAAuB,IAAI,GAAG,EAAE,CAAC;AAC7D,MAAM,qBAAqB,GAA0B,IAAI,GAAG,EAAE,CAAC;AAC/D,MAAM,SAAS,GAA4C,IAAI,OAAO,EAAE,CAAC;AACzE,MAAM,aAAa,GAAmC,IAAI,OAAO,EAAE,CAAC;AACpE,MAAM,uBAAuB,GAAiC,IAAI,GAAG,EAAE,CAAC;AACxE,MAAM,cAAc,GAAsB,IAAI,OAAO,EAAE,CAAC;AACxD,MAAM,cAAc,G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n type ExternalObject,\n getType,\n getWrapper,\n type Handle,\n type RegisterClassVfunc as NativeRegisterClassVfunc,\n setWrapper,\n} from \"@gtkx/native\";\nimport { type AnyClass, walkClassChain } from \"@gtkx/utils\";\nimport type { Mixin, MixinReceiver } from \"./mixin.js\";\nimport { TYPE_INVALID, type TypedClass, typeInterfaces, typeIsA, typeName, typeParent } from \"./type.js\";\n\n/**\n * Static side of class `C` with its construct signature preserved but the member\n * named `K` (default `\"new\"`) removed.\n */\ntype StaticBase<C, K extends PropertyKey = \"new\"> = Omit<C, K> &\n (C extends new (...args: infer A) => infer R ? new (...args: A) => R : never);\n\n/** One overridable vtable slot: where it sits in the vtable struct and how it is marshalled. */\ntype VfuncDescriptor = {\n /** GIR name of the type struct holding the slot, without its namespace, such as `WidgetClass`. */\n className: string;\n /** Name of the slot's field in that struct. */\n vfuncName: string;\n /** Byte offset of the slot within the struct. */\n byteOffset: number;\n /**\n * Byte size of an interface's vtable struct, used to bounds-check `VfuncDescriptor.byteOffset`.\n * A class struct is bounds-checked against the size `g_type_query` reports for it, so a slot in\n * one carries no size of its own.\n */\n vtableSize?: number;\n /** Descriptor for each argument the slot receives, starting with the instance. */\n argDescriptors: NativeRegisterClassVfunc[\"argDescriptors\"];\n /** Descriptor for the value the slot returns. */\n returnDescriptor: NativeRegisterClassVfunc[\"returnDescriptor\"];\n /**\n * The slot takes a trailing `GError**` that `VfuncDescriptor.argDescriptors` leaves out, the\n * way GIR leaves it out of a callable's parameters. A call through the slot has to append it\n * or it passes one argument fewer than the implementation reads.\n */\n canThrow?: boolean;\n};\n\n/**\n * The vtable slots a wrapper class or interface exposes, keyed by the JavaScript method name that\n * overrides each one.\n */\ntype VfuncRegistry = Record<string, VfuncDescriptor>;\n\n/**\n * How one property an interface declares reaches the vtable, given as the interface's own accessor\n * members. A direction introspection does not route through a vtable slot is left out, and the\n * property owns that direction's state itself.\n */\ntype InterfaceProperty = {\n /** Member reading the slot the property's value comes from, such as `getEnabled`. */\n getter?: string;\n /** Member writing the slot the property's value goes to, such as `setActionName`. */\n setter?: string;\n};\n\n/**\n * What an interface's vtable struct looks like, for the classes that adopt the interface.\n * `g_type_query` reports no size for an interface, so each slot's generated metadata carries the\n * struct's byte size to bounds-check the slot's offset; an interface introspection describes no\n * vtable for simply contributes no slots.\n */\ntype InterfaceLayout = {\n /** The slots the struct declares, keyed by the JavaScript method name that fills each one. */\n vfuncs?: VfuncRegistry;\n /**\n * The properties a vtable slot backs, keyed by canonical property name, so a class adopting the\n * interface answers `g_object_get` and `g_object_set` with what the slot holds.\n */\n properties?: Record<string, InterfaceProperty>;\n};\n\nconst classRegistry: Map<bigint, AnyClass> = new Map();\nconst interfaceMixinRegistry: Map<bigint, Mixin> = new Map();\nconst composedClassRegistry: Map<bigint, AnyClass> = new Map();\nconst handleMap: WeakMap<object, ExternalObject<Handle>> = new WeakMap();\nconst vfuncRegistry: WeakMap<object, VfuncRegistry> = new WeakMap();\nconst interfaceLayoutRegistry: Map<bigint, InterfaceLayout> = new Map();\nconst wrapperClasses: WeakSet<AnyClass> = new WeakSet();\nconst derivedClasses: WeakSet<AnyClass> = new WeakSet();\n\nfunction setClassType(cls: AnyClass, type: bigint): void {\n (cls.prototype as { [K in keyof TypedClass]: TypedClass[K] }).__type__ = type;\n}\n\n/**\n * Returns the GType a class was registered under, or the invalid type when it carries none. The tag is\n * read off the class's own prototype, so a subclass that never went through `registerClass` reports the\n * invalid type rather than inheriting the one its parent was registered with.\n */\nfunction getClassType(cls: AnyClass | undefined): bigint {\n const proto: object | undefined = cls?.prototype;\n\n if (proto === undefined || !Object.hasOwn(proto, \"__type__\")) {\n return TYPE_INVALID;\n }\n\n return (proto as TypedClass).__type__;\n}\n\n/**\n * Returns the GType the given instance's handle carries, or the invalid type when it has no handle.\n * This is the object's own type rather than the type of the class it was wrapped as, and the two differ\n * for an object GTK created itself, such as the row widget inside a `Gtk.ListView`, which GTKX wraps as\n * the nearest registered ancestor.\n */\nfunction getInstanceType(instance: object): bigint {\n const handle = handleMap.get(instance);\n\n return handle === undefined ? TYPE_INVALID : getType(handle);\n}\n\nfunction registerClassType(cls: AnyClass, type: bigint): void {\n if (type === TYPE_INVALID) {\n return;\n }\n\n classRegistry.set(type, cls);\n setClassType(cls, type);\n}\n\n/**\n * Registers a wrapper class as the JS representation of a GType, optionally\n * installing a registry of virtual functions.\n * @param cls Wrapper class to associate with the type.\n * @param type GType the class wraps.\n * @param vfuncs Vtable slots the class exposes, so `registerClass` can bind the ones a subclass\n * overrides.\n */\nfunction registerWrapperClass(cls: AnyClass, type: bigint, vfuncs?: VfuncRegistry): void {\n registerClassType(cls, type);\n\n if (type !== TYPE_INVALID) {\n wrapperClasses.add(cls);\n }\n\n if (vfuncs) {\n registerVfuncRegistry(cls, vfuncs);\n }\n}\n\nfunction markDerivedClass(cls: AnyClass): void {\n derivedClasses.add(cls);\n}\n\nfunction resolveAncestorType(ancestor: AnyClass): bigint | undefined {\n if (derivedClasses.has(ancestor) || !wrapperClasses.has(ancestor)) {\n return undefined;\n }\n\n return getClassType(ancestor);\n}\n\nfunction resolveWrapperType(instance: object): bigint {\n const cls = instance.constructor as AnyClass | undefined;\n\n if (cls === undefined) {\n return TYPE_INVALID;\n }\n\n return walkClassChain(cls, (ancestor) => resolveAncestorType(ancestor)) ?? TYPE_INVALID;\n}\n\n/**\n * Registers a GInterface, associating its GType with a mixin used to compose the\n * interface onto wrapper classes and, when introspection describes its vtable, that layout.\n * @param cls Class carrying the interface's GType tag.\n * @param type GType of the interface.\n * @param mixin Mixin that applies the interface to a wrapper class.\n * @param layout The interface's vtable struct, so `registerClass` can bind the slots an\n * implementing class overrides and take over the ones it leaves alone.\n */\nfunction registerInterface(cls: AnyClass, type: bigint, mixin: Mixin, layout?: InterfaceLayout): void {\n if (type === TYPE_INVALID) {\n return;\n }\n\n setClassType(cls, type);\n interfaceMixinRegistry.set(type, mixin);\n\n if (layout) {\n interfaceLayoutRegistry.set(type, layout);\n }\n}\n\n/**\n * Wraps a native handle in a JS wrapper instance. With no class, resolves and\n * reuses the wrapper for the handle's runtime GType (composing interface mixins),\n * and hands back an instance that already carries a handle unchanged; with an\n * explicit class, creates a bare instance backed by the handle. Returns null for\n * a null or undefined handle.\n * @param handle Native handle to wrap.\n * @param cls Wrapper class to instantiate, or omitted to resolve it from the runtime type.\n */\nfunction wrapHandle(handle: null | undefined, cls?: AnyClass): null;\nfunction wrapHandle<T extends object>(handle: ExternalObject<Handle>, cls: AnyClass<T>): T;\n\nfunction wrapHandle<T extends object>(\n handle: ExternalObject<Handle> | null | undefined,\n cls: AnyClass<T>,\n): T | null;\n\nfunction wrapHandle(handle: ExternalObject<Handle>, cls?: AnyClass): TypedClass;\nfunction wrapHandle(handle: ExternalObject<Handle> | null | undefined, cls?: AnyClass): TypedClass | null;\n\nfunction wrapHandle(handle: ExternalObject<Handle> | null | undefined, cls?: AnyClass): object | null {\n if (handle === null || handle === undefined) {\n return null;\n }\n\n if (cls === undefined) {\n return getOrCreateWrapper(handle);\n }\n\n const instance: object = Object.create(cls.prototype) as object;\n setHandle(instance, handle);\n\n return instance;\n}\n\n/**\n * Returns the wrapper class registered for a GType, walking up to ancestor types,\n * and throws if none is registered.\n */\nfunction getWrapperClass(type: bigint): AnyClass {\n const cls = resolveWrapperClass(type);\n\n if (!cls) {\n throw new Error(`No registered wrapper class for type '${typeName(type) ?? String(type)}'`);\n }\n\n return cls;\n}\n\nfunction resolveWrapperClass(type: bigint): AnyClass | null {\n let currentType = type;\n\n while (currentType !== TYPE_INVALID) {\n const cls = classRegistry.get(currentType);\n\n if (cls) {\n return cls;\n }\n\n currentType = typeParent(currentType);\n }\n\n return null;\n}\n\nfunction getInterfaceMixin(type: bigint): Mixin | undefined {\n return interfaceMixinRegistry.get(type);\n}\n\nfunction applyInterfaceMixin(cls: AnyClass, type: bigint, baseType: bigint, applied: Set<bigint>): AnyClass {\n if (applied.has(type) || typeIsA(baseType, type)) {\n return cls;\n }\n\n const mixin = getInterfaceMixin(type);\n\n if (mixin === undefined) {\n return cls;\n }\n\n applied.add(type);\n\n return mixin(cls as AnyClass<MixinReceiver>);\n}\n\nfunction createComposedClass(base: AnyClass, runtimeType: bigint): AnyClass {\n const baseType = getClassType(base);\n const applied: Set<bigint> = new Set();\n let cls: AnyClass = base;\n\n for (const type of typeInterfaces(runtimeType)) {\n cls = applyInterfaceMixin(cls, type, baseType, applied);\n }\n\n return applied.size === 0 ? base : cls;\n}\n\nfunction resolveComposedClass(runtimeType: bigint): AnyClass | null {\n const exact = classRegistry.get(runtimeType);\n\n if (exact) {\n return exact;\n }\n\n const cached = composedClassRegistry.get(runtimeType);\n\n if (cached) {\n return cached;\n }\n\n const base = resolveWrapperClass(runtimeType);\n\n if (base === null) {\n return null;\n }\n\n const composed = createComposedClass(base, runtimeType);\n\n if (composed === base) {\n return base;\n }\n\n setClassType(composed, runtimeType);\n wrapperClasses.add(composed);\n composedClassRegistry.set(runtimeType, composed);\n\n return composed;\n}\n\nfunction wrapObject(value: unknown): object | null {\n if (value == null) {\n return null;\n }\n\n return getOrCreateWrapper(value as ExternalObject<Handle>);\n}\n\nfunction getOrCreateWrapper(handle: ExternalObject<Handle>): object {\n if (handleMap.has(handle)) {\n return handle;\n }\n\n const existing = getWrapper(handle);\n\n if (existing) {\n return existing;\n }\n\n const runtimeType: bigint = getType(handle);\n\n if (runtimeType === TYPE_INVALID) {\n throw new Error(\"Cannot resolve runtime GLib type from handle\");\n }\n\n const cls = resolveComposedClass(runtimeType);\n\n if (!cls) {\n throw new Error(`Expected registered GLib type, got type ${String(runtimeType)}`);\n }\n\n const instance: object = Object.create(cls.prototype) as object;\n registerWrapper(handle, instance);\n\n return instance;\n}\n\nfunction instanceClassName(instance: object): string {\n return (instance as { constructor?: { name?: string } }).constructor?.name ?? \"object\";\n}\n\n/** Returns the native handle bound to a wrapper instance, throwing if none is set. */\nfunction getHandle(instance: object): ExternalObject<Handle> {\n const handle = handleMap.get(instance);\n\n if (handle === undefined) {\n throw new Error(`No native handle associated with ${instanceClassName(instance)}`);\n }\n\n return handle;\n}\n\n/** Associates a native handle with a wrapper instance. */\nfunction setHandle(instance: object, handle: ExternalObject<Handle>): void {\n handleMap.set(instance, handle);\n}\n\nfunction registerWrapper(handle: ExternalObject<Handle>, instance: object): void {\n setHandle(instance, handle);\n setWrapper(handle, instance);\n}\n\nfunction registerVfuncRegistry(cls: object, registry: VfuncRegistry): void {\n vfuncRegistry.set(cls, registry);\n}\n\nfunction getVfuncRegistry(cls: object): VfuncRegistry | undefined {\n return vfuncRegistry.get(cls);\n}\n\nfunction getInterfaceVfuncRegistry(type: bigint): VfuncRegistry | undefined {\n return interfaceLayoutRegistry.get(type)?.vfuncs;\n}\n\nfunction getInterfaceProperties(type: bigint): Record<string, InterfaceProperty> | undefined {\n return interfaceLayoutRegistry.get(type)?.properties;\n}\n\nexport {\n getClassType,\n getInstanceType,\n markDerivedClass,\n registerClassType,\n registerWrapperClass,\n registerInterface,\n wrapHandle,\n getWrapperClass,\n resolveWrapperClass,\n getHandle,\n setHandle,\n getVfuncRegistry,\n getInterfaceMixin,\n getInterfaceProperties,\n getInterfaceVfuncRegistry,\n instanceClassName,\n registerWrapper,\n resolveWrapperType,\n wrapObject,\n type InterfaceProperty,\n type StaticBase,\n type VfuncDescriptor,\n};\n"]}
|
package/dist/signal.d.ts
CHANGED
|
@@ -3,17 +3,37 @@ import { type Arg } from "./arg.js";
|
|
|
3
3
|
import { type CallbackDescriptor } from "./descriptors.js";
|
|
4
4
|
/** Function invoked when a connected GObject signal is emitted. */
|
|
5
5
|
type SignalHandler = (...args: unknown[]) => unknown;
|
|
6
|
+
/** The marshalling and handler that make up a single signal connection. */
|
|
6
7
|
type SignalConnectSpec = {
|
|
8
|
+
/**
|
|
9
|
+
* Marshalling for the emission, whose `argDescriptors` lead with the emitter and include the
|
|
10
|
+
* closure's user data slot.
|
|
11
|
+
*/
|
|
7
12
|
callback: CallbackDescriptor;
|
|
13
|
+
/** Called on each emission with the signal's own arguments, without the leading emitter. */
|
|
8
14
|
handler: SignalHandler;
|
|
9
|
-
after
|
|
15
|
+
/** When true, run the handler after the class's default handler instead of before it. */
|
|
16
|
+
isAfter: boolean;
|
|
10
17
|
};
|
|
18
|
+
/** One argument of a signal emission: how to marshal it, plus the value to marshal. */
|
|
11
19
|
type EmitArg = Arg & {
|
|
20
|
+
/**
|
|
21
|
+
* The value to pass for an input or inout argument, or the caller-allocated storage to fill for
|
|
22
|
+
* a caller-allocated output argument; omitted for a plain output argument.
|
|
23
|
+
*/
|
|
12
24
|
value?: unknown;
|
|
13
25
|
};
|
|
14
26
|
/** Returns the signal name without its detail suffix (the part after `::`). */
|
|
15
27
|
declare const getSignalBaseName: (signal: string) => string;
|
|
16
28
|
declare const isSignalHandlerConnected: (instance: object, handlerId: number) => boolean;
|
|
29
|
+
declare const untrackConnection: (instance: object, handlerId: number) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Disconnects the handler an instance connected under the given id, and forgets the connection.
|
|
32
|
+
* @param instance Emitter the handler was connected to.
|
|
33
|
+
* @param handlerId Id {@link connectSignal} returned for the handler.
|
|
34
|
+
*/
|
|
35
|
+
declare const disconnectSignal: (instance: object, handlerId: number) => void;
|
|
36
|
+
declare function hasSignalListener(instance: object, signals?: string[]): boolean;
|
|
17
37
|
/**
|
|
18
38
|
* Connects a handler to a GObject signal on an instance and returns the handler id.
|
|
19
39
|
* @param instance Emitter to connect to.
|
|
@@ -21,7 +41,6 @@ declare const isSignalHandlerConnected: (instance: object, handlerId: number) =>
|
|
|
21
41
|
* @param spec Callback descriptor, handler function, and whether to run after the default handler.
|
|
22
42
|
*/
|
|
23
43
|
declare function connectSignal(instance: object, signal: string, spec: SignalConnectSpec): number;
|
|
24
|
-
declare function blockMatchedSignalHandlers(instance: object, signal: string): void;
|
|
25
44
|
/**
|
|
26
45
|
* Emits a signal on an instance with the given arguments and returns its result
|
|
27
46
|
* combined with any output-argument values.
|
|
@@ -31,5 +50,5 @@ declare function blockMatchedSignalHandlers(instance: object, signal: string): v
|
|
|
31
50
|
* @param returnDescriptor Descriptor for the signal's return value, omitted when it returns void.
|
|
32
51
|
*/
|
|
33
52
|
declare function emitSignal(instance: object, signal: string, args: EmitArg[], returnDescriptor?: Descriptor): unknown;
|
|
34
|
-
export { getSignalBaseName, connectSignal,
|
|
53
|
+
export { getSignalBaseName, connectSignal, disconnectSignal, emitSignal, hasSignalListener, isSignalHandlerConnected, untrackConnection, type SignalHandler, };
|
|
35
54
|
//# sourceMappingURL=signal.d.ts.map
|
package/dist/signal.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../src/signal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAA0B,MAAM,cAAc,CAAC;AAEvE,OAAO,EAAE,KAAK,GAAG,EAAiD,MAAM,UAAU,CAAC;AAGnF,OAAO,EAIH,KAAK,kBAAkB,EAM1B,MAAM,kBAAkB,CAAC;AAc1B,mEAAmE;AACnE,KAAK,aAAa,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC;AAErD,KAAK,iBAAiB,GAAG;IACrB,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,OAAO,EAAE,aAAa,CAAC;IACvB,
|
|
1
|
+
{"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../src/signal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAA0B,MAAM,cAAc,CAAC;AAEvE,OAAO,EAAE,KAAK,GAAG,EAAiD,MAAM,UAAU,CAAC;AAGnF,OAAO,EAIH,KAAK,kBAAkB,EAM1B,MAAM,kBAAkB,CAAC;AAc1B,mEAAmE;AACnE,KAAK,aAAa,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC;AAErD,2EAA2E;AAC3E,KAAK,iBAAiB,GAAG;IACrB;;;OAGG;IACH,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,4FAA4F;IAC5F,OAAO,EAAE,aAAa,CAAC;IACvB,yFAAyF;IACzF,OAAO,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,uFAAuF;AACvF,KAAK,OAAO,GAAG,GAAG,GAAG;IACjB;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAuBF,+EAA+E;AAC/E,QAAA,MAAM,iBAAiB,GAAI,QAAQ,MAAM,KAAG,MAI3C,CAAC;AAYF,QAAA,MAAM,wBAAwB,GAAI,UAAU,MAAM,EAAE,WAAW,MAAM,KAAG,OACA,CAAC;AAoBzE,QAAA,MAAM,iBAAiB,GAAI,UAAU,MAAM,EAAE,WAAW,MAAM,KAAG,IAchE,CAAC;AAEF;;;;GAIG;AACH,QAAA,MAAM,gBAAgB,GAAI,UAAU,MAAM,EAAE,WAAW,MAAM,KAAG,IAG/D,CAAC;AAgBF,iBAAS,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAexE;AAoBD;;;;;GAKG;AACH,iBAAS,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,GAAG,MAAM,CASxF;AAoBD;;;;;;;GAOG;AACH,iBAAS,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,gBAAgB,CAAC,EAAE,UAAU,GAAG,OAAO,CAiC7G;AAED,OAAO,EACH,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,wBAAwB,EACxB,iBAAiB,EACjB,KAAK,aAAa,GACrB,CAAC"}
|
package/dist/signal.js
CHANGED
|
@@ -7,11 +7,12 @@ import { getHandle } from "./registry.js";
|
|
|
7
7
|
import { packTupleResult } from "./tuple.js";
|
|
8
8
|
import { fromValue, getBoxedValue, inoutValueForBoxedDescriptor, newValueForDescriptor, outValueForBoxedDescriptor, outValueForDescriptor, toValue, } from "./value.js";
|
|
9
9
|
const connectCache = createBindCache();
|
|
10
|
+
const connectionTable = new WeakMap();
|
|
10
11
|
const gQuarkFromString = bind(LIB, "g_quark_from_string", [stringT("borrowed")], uint32T);
|
|
11
12
|
const gSignalLookup = bind(LIB, "g_signal_lookup", [stringT("borrowed"), biguint64T], uint32T);
|
|
12
13
|
const gSignalEmitv = bind(LIB, "g_signal_emitv", [arrayT(VALUE_T, "array", "borrowed", { elementSize: VALUE_SIZE }), uint32T, uint32T, VALUE_T], voidT);
|
|
13
|
-
const gSignalHandlersBlockMatched = bind(LIB, "g_signal_handlers_block_matched", [objectT("borrowed"), uint32T, uint32T, uint32T, objectT("borrowed"), objectT("borrowed"), objectT("borrowed")], uint32T);
|
|
14
14
|
const gSignalHandlerIsConnected = bind(LIB, "g_signal_handler_is_connected", [objectT("borrowed"), uint64T], booleanT);
|
|
15
|
+
const gSignalHandlerDisconnect = bind(LIB, "g_signal_handler_disconnect", [objectT("borrowed"), uint64T], voidT);
|
|
15
16
|
/** Returns the signal name without its detail suffix (the part after `::`). */
|
|
16
17
|
const getSignalBaseName = (signal) => {
|
|
17
18
|
const detailIndex = signal.indexOf("::");
|
|
@@ -25,6 +26,63 @@ function getSignalDetailQuark(signal) {
|
|
|
25
26
|
return gQuarkFromString(signal.slice(detailIndex + 2));
|
|
26
27
|
}
|
|
27
28
|
const isSignalHandlerConnected = (instance, handlerId) => gSignalHandlerIsConnected(getHandle(instance), handlerId);
|
|
29
|
+
const trackConnection = (instance, signal, handlerId) => {
|
|
30
|
+
let bySignal = connectionTable.get(instance);
|
|
31
|
+
if (!bySignal) {
|
|
32
|
+
bySignal = new Map();
|
|
33
|
+
connectionTable.set(instance, bySignal);
|
|
34
|
+
}
|
|
35
|
+
let handlerIds = bySignal.get(signal);
|
|
36
|
+
if (!handlerIds) {
|
|
37
|
+
handlerIds = new Set();
|
|
38
|
+
bySignal.set(signal, handlerIds);
|
|
39
|
+
}
|
|
40
|
+
handlerIds.add(handlerId);
|
|
41
|
+
};
|
|
42
|
+
const untrackConnection = (instance, handlerId) => {
|
|
43
|
+
const bySignal = connectionTable.get(instance);
|
|
44
|
+
if (bySignal === undefined) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
for (const [name, handlerIds] of bySignal) {
|
|
48
|
+
handlerIds.delete(handlerId);
|
|
49
|
+
if (handlerIds.size === 0) {
|
|
50
|
+
bySignal.delete(name);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Disconnects the handler an instance connected under the given id, and forgets the connection.
|
|
56
|
+
* @param instance Emitter the handler was connected to.
|
|
57
|
+
* @param handlerId Id {@link connectSignal} returned for the handler.
|
|
58
|
+
*/
|
|
59
|
+
const disconnectSignal = (instance, handlerId) => {
|
|
60
|
+
untrackConnection(instance, handlerId);
|
|
61
|
+
gSignalHandlerDisconnect(getHandle(instance), handlerId);
|
|
62
|
+
};
|
|
63
|
+
const hasLiveConnection = (instance, handlerIds) => {
|
|
64
|
+
let isLive = false;
|
|
65
|
+
for (const handlerId of handlerIds) {
|
|
66
|
+
if (isSignalHandlerConnected(instance, handlerId)) {
|
|
67
|
+
isLive = true;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
handlerIds.delete(handlerId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return isLive;
|
|
74
|
+
};
|
|
75
|
+
function hasSignalListener(instance, signals) {
|
|
76
|
+
const bySignal = connectionTable.get(instance);
|
|
77
|
+
if (!bySignal) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const names = signals === undefined ? bySignal.keys().toArray() : signals.map((signal) => getSignalBaseName(signal));
|
|
81
|
+
return names.some((name) => {
|
|
82
|
+
const handlerIds = bySignal.get(name);
|
|
83
|
+
return handlerIds !== undefined && hasLiveConnection(instance, handlerIds);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
28
86
|
const getSignalId = (instance, signal) => {
|
|
29
87
|
const type = instance.__type__;
|
|
30
88
|
return gSignalLookup(getSignalBaseName(signal), type);
|
|
@@ -40,15 +98,13 @@ function connectBind(type, signal, callback) {
|
|
|
40
98
|
* @param spec Callback descriptor, handler function, and whether to run after the default handler.
|
|
41
99
|
*/
|
|
42
100
|
function connectSignal(instance, signal, spec) {
|
|
43
|
-
const { callback, handler,
|
|
101
|
+
const { callback, handler, isAfter } = spec;
|
|
44
102
|
const wrapped = wrapCallback(handler, callback, "emitter");
|
|
45
103
|
const type = instance.__type__;
|
|
46
104
|
const connect = connectBind(type, signal, callback);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const signalId = getSignalId(instance, signal);
|
|
51
|
-
gSignalHandlersBlockMatched(getHandle(instance), 1, signalId, 0, undefined, undefined, undefined);
|
|
105
|
+
const handlerId = connect(getHandle(instance), signal, wrapped, isAfter ? 1 : 0);
|
|
106
|
+
trackConnection(instance, getSignalBaseName(signal), handlerId);
|
|
107
|
+
return handlerId;
|
|
52
108
|
}
|
|
53
109
|
const createEmitValue = (arg) => {
|
|
54
110
|
if (!isOutputArg(arg)) {
|
|
@@ -91,5 +147,5 @@ function emitSignal(instance, signal, args, returnDescriptor) {
|
|
|
91
147
|
gSignalEmitv(values, signalId, detail, undefined);
|
|
92
148
|
return packTupleResult(reads.map((read) => read()), undefined, false);
|
|
93
149
|
}
|
|
94
|
-
export { getSignalBaseName, connectSignal,
|
|
150
|
+
export { getSignalBaseName, connectSignal, disconnectSignal, emitSignal, hasSignalListener, isSignalHandlerConnected, untrackConnection, };
|
|
95
151
|
//# sourceMappingURL=signal.js.map
|
package/dist/signal.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signal.js","sourceRoot":"","sources":["../src/signal.ts"],"names":[],"mappings":"AAEA,OAAO,EAAY,oBAAoB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;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IAED,IAAI,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,WAAW,GAAG,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;QAC5D,YAAY,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QAEpD,OAAO,eAAe,CAClB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAC3B,SAAS,CAAC,WAAW,CAAC,EACtB,IAAI,CACP,CAAC;IACN,CAAC;IAED,YAAY,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;IAElD,OAAO,eAAe,CAClB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAC3B,SAAS,EACT,KAAK,CACR,CAAC;AACN,CAAC;AAED,OAAO,EACH,iBAAiB,EACjB,aAAa,EACb,0BAA0B,EAC1B,UAAU,EACV,wBAAwB,GAE3B,CAAC","sourcesContent":["import type { Descriptor, ExternalObject, Handle } from \"@gtkx/native\";\nimport type { TypedClass } from \"./type.js\";\nimport { type Arg, isCallerAllocatedArg, isInoutArg, isOutputArg } from \"./arg.js\";\nimport { bind, createBindCache } from \"./bind.js\";\nimport { wrapCallback } from \"./callback.js\";\nimport {\n arrayT,\n biguint64T,\n booleanT,\n type CallbackDescriptor,\n objectT,\n stringT,\n uint32T,\n uint64T,\n voidT,\n} from \"./descriptors.js\";\nimport { LIB, VALUE_SIZE, VALUE_T } from \"./library.js\";\nimport { getHandle } from \"./registry.js\";\nimport { packTupleResult } from \"./tuple.js\";\nimport {\n fromValue,\n getBoxedValue,\n inoutValueForBoxedDescriptor,\n newValueForDescriptor,\n outValueForBoxedDescriptor,\n outValueForDescriptor,\n toValue,\n} from \"./value.js\";\n\n/** Function invoked when a connected GObject signal is emitted. */\ntype SignalHandler = (...args: unknown[]) => unknown;\n\ntype SignalConnectSpec = {\n callback: CallbackDescriptor;\n handler: SignalHandler;\n after: boolean;\n};\n\ntype EmitArg = Arg & { value?: unknown };\n\nconst connectCache = createBindCache();\nconst gQuarkFromString = bind(LIB, \"g_quark_from_string\", [stringT(\"borrowed\")], uint32T);\nconst gSignalLookup = bind(LIB, \"g_signal_lookup\", [stringT(\"borrowed\"), biguint64T], uint32T);\n\nconst gSignalEmitv = bind(\n LIB,\n \"g_signal_emitv\",\n [arrayT(VALUE_T, \"array\", \"borrowed\", { elementSize: VALUE_SIZE }), uint32T, uint32T, VALUE_T],\n voidT,\n);\n\nconst gSignalHandlersBlockMatched = bind(\n LIB,\n \"g_signal_handlers_block_matched\",\n [objectT(\"borrowed\"), uint32T, uint32T, uint32T, objectT(\"borrowed\"), objectT(\"borrowed\"), objectT(\"borrowed\")],\n uint32T,\n);\n\nconst gSignalHandlerIsConnected = bind(\n LIB,\n \"g_signal_handler_is_connected\",\n [objectT(\"borrowed\"), uint64T],\n booleanT,\n);\n\n/** Returns the signal name without its detail suffix (the part after `::`). */\nconst getSignalBaseName = (signal: string): string => {\n const detailIndex = signal.indexOf(\"::\");\n\n return detailIndex === -1 ? signal : signal.slice(0, detailIndex);\n};\n\nfunction getSignalDetailQuark(signal: string): number {\n const detailIndex = signal.indexOf(\"::\");\n\n if (detailIndex === -1) {\n return 0;\n }\n\n return gQuarkFromString(signal.slice(detailIndex + 2)) as number;\n}\n\nconst isSignalHandlerConnected = (instance: object, handlerId: number): boolean =>\n gSignalHandlerIsConnected(getHandle(instance), handlerId) as boolean;\n\nconst getSignalId = (instance: object, signal: string): number => {\n const type: bigint = (instance as TypedClass).__type__;\n\n return gSignalLookup(getSignalBaseName(signal), type) as number;\n};\n\nfunction connectBind(type: bigint, signal: string, callback: CallbackDescriptor): (...values: unknown[]) => unknown {\n const key = `${String(type)}\\0${getSignalBaseName(signal)}`;\n\n return connectCache(\n key,\n LIB,\n \"g_signal_connect_data\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), callback, uint32T],\n uint64T,\n );\n}\n\n/**\n * Connects a handler to a GObject signal on an instance and returns the handler id.\n * @param instance Emitter to connect to.\n * @param signal Signal name, optionally including a `::detail` suffix.\n * @param spec Callback descriptor, handler function, and whether to run after the default handler.\n */\nfunction connectSignal(instance: object, signal: string, spec: SignalConnectSpec): number {\n const { callback, handler, after } = spec;\n const wrapped = wrapCallback(handler, callback, \"emitter\");\n const type: bigint = (instance as TypedClass).__type__;\n const connect = connectBind(type, signal, callback);\n\n return connect(getHandle(instance), signal, wrapped, after ? 1 : 0) as number;\n}\n\nfunction blockMatchedSignalHandlers(instance: object, signal: string): void {\n const signalId = getSignalId(instance, signal);\n gSignalHandlersBlockMatched(getHandle(instance), 1, signalId, 0, undefined, undefined, undefined);\n}\n\nconst createEmitValue = (arg: EmitArg): { value: ExternalObject<Handle>; read?: () => unknown } => {\n if (!isOutputArg(arg)) {\n return { value: toValue(arg.type, arg.value) };\n }\n\n if (isCallerAllocatedArg(arg)) {\n if (isInoutArg(arg)) {\n return { value: inoutValueForBoxedDescriptor(arg.type, arg.value as object) };\n }\n\n const value = outValueForBoxedDescriptor(arg.type, arg.value as object);\n\n return { value, read: () => getBoxedValue(value) };\n }\n\n return isInoutArg(arg) ? outValueForDescriptor(arg.type, arg.value) : outValueForDescriptor(arg.type);\n};\n\n/**\n * Emits a signal on an instance with the given arguments and returns its result\n * combined with any output-argument values.\n * @param instance Emitter to emit the signal on.\n * @param signal Signal name, optionally including a `::detail` suffix.\n * @param args Arguments to pass, including output and inout arguments.\n * @param returnDescriptor Descriptor for the signal's return value, omitted when it returns void.\n */\nfunction emitSignal(instance: object, signal: string, args: EmitArg[], returnDescriptor?: Descriptor): unknown {\n const signalId = getSignalId(instance, signal);\n const detail = getSignalDetailQuark(signal);\n const values: ExternalObject<Handle>[] = [toValue(objectT(\"full\"), instance)];\n const reads: (() => unknown)[] = [];\n\n for (const arg of args) {\n const { value, read } = createEmitValue(arg);\n values.push(value);\n\n if (read) {\n reads.push(read);\n }\n }\n\n if (returnDescriptor !== undefined) {\n const returnValue = newValueForDescriptor(returnDescriptor);\n gSignalEmitv(values, signalId, detail, returnValue);\n\n return packTupleResult(\n reads.map((read) => read()),\n fromValue(returnValue),\n true,\n );\n }\n\n gSignalEmitv(values, signalId, detail, undefined);\n\n return packTupleResult(\n reads.map((read) => read()),\n undefined,\n false,\n );\n}\n\nexport {\n getSignalBaseName,\n connectSignal,\n blockMatchedSignalHandlers,\n emitSignal,\n isSignalHandlerConnected,\n type SignalHandler,\n};\n"]}
|
|
1
|
+
{"version":3,"file":"signal.js","sourceRoot":"","sources":["../src/signal.ts"],"names":[],"mappings":"AAEA,OAAO,EAAY,oBAAoB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACnF,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EACH,MAAM,EACN,UAAU,EACV,QAAQ,EAER,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,KAAK,GACR,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EACH,SAAS,EACT,aAAa,EACb,4BAA4B,EAC5B,qBAAqB,EACrB,0BAA0B,EAC1B,qBAAqB,EACrB,OAAO,GACV,MAAM,YAAY,CAAC;AA2BpB,MAAM,YAAY,GAAG,eAAe,EAAE,CAAC;AACvC,MAAM,eAAe,GAA8C,IAAI,OAAO,EAAE,CAAC;AACjF,MAAM,gBAAgB,GAAG,IAAI,CAAC,GAAG,EAAE,qBAAqB,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;AAC1F,MAAM,aA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CAAC;QAEnB,IAAI,IAAI,EAAE,CAAC;YACP,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;IACL,CAAC;IAED,IAAI,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,WAAW,GAAG,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;QAC5D,YAAY,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QAEpD,OAAO,eAAe,CAClB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAC3B,SAAS,CAAC,WAAW,CAAC,EACtB,IAAI,CACP,CAAC;IACN,CAAC;IAED,YAAY,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;IAElD,OAAO,eAAe,CAClB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAC3B,SAAS,EACT,KAAK,CACR,CAAC;AACN,CAAC;AAED,OAAO,EACH,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,wBAAwB,EACxB,iBAAiB,GAEpB,CAAC","sourcesContent":["import type { Descriptor, ExternalObject, Handle } from \"@gtkx/native\";\nimport type { TypedClass } from \"./type.js\";\nimport { type Arg, isCallerAllocatedArg, isInoutArg, isOutputArg } from \"./arg.js\";\nimport { bind, createBindCache } from \"./bind.js\";\nimport { wrapCallback } from \"./callback.js\";\nimport {\n arrayT,\n biguint64T,\n booleanT,\n type CallbackDescriptor,\n objectT,\n stringT,\n uint32T,\n uint64T,\n voidT,\n} from \"./descriptors.js\";\nimport { LIB, VALUE_SIZE, VALUE_T } from \"./library.js\";\nimport { getHandle } from \"./registry.js\";\nimport { packTupleResult } from \"./tuple.js\";\nimport {\n fromValue,\n getBoxedValue,\n inoutValueForBoxedDescriptor,\n newValueForDescriptor,\n outValueForBoxedDescriptor,\n outValueForDescriptor,\n toValue,\n} from \"./value.js\";\n\n/** Function invoked when a connected GObject signal is emitted. */\ntype SignalHandler = (...args: unknown[]) => unknown;\n\n/** The marshalling and handler that make up a single signal connection. */\ntype SignalConnectSpec = {\n /**\n * Marshalling for the emission, whose `argDescriptors` lead with the emitter and include the\n * closure's user data slot.\n */\n callback: CallbackDescriptor;\n /** Called on each emission with the signal's own arguments, without the leading emitter. */\n handler: SignalHandler;\n /** When true, run the handler after the class's default handler instead of before it. */\n isAfter: boolean;\n};\n\n/** One argument of a signal emission: how to marshal it, plus the value to marshal. */\ntype EmitArg = Arg & {\n /**\n * The value to pass for an input or inout argument, or the caller-allocated storage to fill for\n * a caller-allocated output argument; omitted for a plain output argument.\n */\n value?: unknown;\n};\n\nconst connectCache = createBindCache();\nconst connectionTable: WeakMap<object, Map<string, Set<number>>> = new WeakMap();\nconst gQuarkFromString = bind(LIB, \"g_quark_from_string\", [stringT(\"borrowed\")], uint32T);\nconst gSignalLookup = bind(LIB, \"g_signal_lookup\", [stringT(\"borrowed\"), biguint64T], uint32T);\n\nconst gSignalEmitv = bind(\n LIB,\n \"g_signal_emitv\",\n [arrayT(VALUE_T, \"array\", \"borrowed\", { elementSize: VALUE_SIZE }), uint32T, uint32T, VALUE_T],\n voidT,\n);\n\nconst gSignalHandlerIsConnected = bind(\n LIB,\n \"g_signal_handler_is_connected\",\n [objectT(\"borrowed\"), uint64T],\n booleanT,\n);\n\nconst gSignalHandlerDisconnect = bind(LIB, \"g_signal_handler_disconnect\", [objectT(\"borrowed\"), uint64T], voidT);\n\n/** Returns the signal name without its detail suffix (the part after `::`). */\nconst getSignalBaseName = (signal: string): string => {\n const detailIndex = signal.indexOf(\"::\");\n\n return detailIndex === -1 ? signal : signal.slice(0, detailIndex);\n};\n\nfunction getSignalDetailQuark(signal: string): number {\n const detailIndex = signal.indexOf(\"::\");\n\n if (detailIndex === -1) {\n return 0;\n }\n\n return gQuarkFromString(signal.slice(detailIndex + 2)) as number;\n}\n\nconst isSignalHandlerConnected = (instance: object, handlerId: number): boolean =>\n gSignalHandlerIsConnected(getHandle(instance), handlerId) as boolean;\n\nconst trackConnection = (instance: object, signal: string, handlerId: number): void => {\n let bySignal = connectionTable.get(instance);\n\n if (!bySignal) {\n bySignal = new Map();\n connectionTable.set(instance, bySignal);\n }\n\n let handlerIds = bySignal.get(signal);\n\n if (!handlerIds) {\n handlerIds = new Set();\n bySignal.set(signal, handlerIds);\n }\n\n handlerIds.add(handlerId);\n};\n\nconst untrackConnection = (instance: object, handlerId: number): void => {\n const bySignal = connectionTable.get(instance);\n\n if (bySignal === undefined) {\n return;\n }\n\n for (const [name, handlerIds] of bySignal) {\n handlerIds.delete(handlerId);\n\n if (handlerIds.size === 0) {\n bySignal.delete(name);\n }\n }\n};\n\n/**\n * Disconnects the handler an instance connected under the given id, and forgets the connection.\n * @param instance Emitter the handler was connected to.\n * @param handlerId Id {@link connectSignal} returned for the handler.\n */\nconst disconnectSignal = (instance: object, handlerId: number): void => {\n untrackConnection(instance, handlerId);\n gSignalHandlerDisconnect(getHandle(instance), handlerId);\n};\n\nconst hasLiveConnection = (instance: object, handlerIds: Set<number>): boolean => {\n let isLive = false;\n\n for (const handlerId of handlerIds) {\n if (isSignalHandlerConnected(instance, handlerId)) {\n isLive = true;\n } else {\n handlerIds.delete(handlerId);\n }\n }\n\n return isLive;\n};\n\nfunction hasSignalListener(instance: object, signals?: string[]): boolean {\n const bySignal = connectionTable.get(instance);\n\n if (!bySignal) {\n return false;\n }\n\n const names =\n signals === undefined ? bySignal.keys().toArray() : signals.map((signal) => getSignalBaseName(signal));\n\n return names.some((name) => {\n const handlerIds = bySignal.get(name);\n\n return handlerIds !== undefined && hasLiveConnection(instance, handlerIds);\n });\n}\n\nconst getSignalId = (instance: object, signal: string): number => {\n const type: bigint = (instance as TypedClass).__type__;\n\n return gSignalLookup(getSignalBaseName(signal), type) as number;\n};\n\nfunction connectBind(type: bigint, signal: string, callback: CallbackDescriptor): (...values: unknown[]) => unknown {\n const key = `${String(type)}\\0${getSignalBaseName(signal)}`;\n\n return connectCache(\n key,\n LIB,\n \"g_signal_connect_data\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), callback, uint32T],\n uint64T,\n );\n}\n\n/**\n * Connects a handler to a GObject signal on an instance and returns the handler id.\n * @param instance Emitter to connect to.\n * @param signal Signal name, optionally including a `::detail` suffix.\n * @param spec Callback descriptor, handler function, and whether to run after the default handler.\n */\nfunction connectSignal(instance: object, signal: string, spec: SignalConnectSpec): number {\n const { callback, handler, isAfter } = spec;\n const wrapped = wrapCallback(handler, callback, \"emitter\");\n const type: bigint = (instance as TypedClass).__type__;\n const connect = connectBind(type, signal, callback);\n const handlerId = connect(getHandle(instance), signal, wrapped, isAfter ? 1 : 0) as number;\n trackConnection(instance, getSignalBaseName(signal), handlerId);\n\n return handlerId;\n}\n\nconst createEmitValue = (arg: EmitArg): { value: ExternalObject<Handle>; read?: () => unknown } => {\n if (!isOutputArg(arg)) {\n return { value: toValue(arg.type, arg.value) };\n }\n\n if (isCallerAllocatedArg(arg)) {\n if (isInoutArg(arg)) {\n return { value: inoutValueForBoxedDescriptor(arg.type, arg.value as object) };\n }\n\n const value = outValueForBoxedDescriptor(arg.type, arg.value as object);\n\n return { value, read: () => getBoxedValue(value) };\n }\n\n return isInoutArg(arg) ? outValueForDescriptor(arg.type, arg.value) : outValueForDescriptor(arg.type);\n};\n\n/**\n * Emits a signal on an instance with the given arguments and returns its result\n * combined with any output-argument values.\n * @param instance Emitter to emit the signal on.\n * @param signal Signal name, optionally including a `::detail` suffix.\n * @param args Arguments to pass, including output and inout arguments.\n * @param returnDescriptor Descriptor for the signal's return value, omitted when it returns void.\n */\nfunction emitSignal(instance: object, signal: string, args: EmitArg[], returnDescriptor?: Descriptor): unknown {\n const signalId = getSignalId(instance, signal);\n const detail = getSignalDetailQuark(signal);\n const values: ExternalObject<Handle>[] = [toValue(objectT(\"full\"), instance)];\n const reads: (() => unknown)[] = [];\n\n for (const arg of args) {\n const { value, read } = createEmitValue(arg);\n values.push(value);\n\n if (read) {\n reads.push(read);\n }\n }\n\n if (returnDescriptor !== undefined) {\n const returnValue = newValueForDescriptor(returnDescriptor);\n gSignalEmitv(values, signalId, detail, returnValue);\n\n return packTupleResult(\n reads.map((read) => read()),\n fromValue(returnValue),\n true,\n );\n }\n\n gSignalEmitv(values, signalId, detail, undefined);\n\n return packTupleResult(\n reads.map((read) => read()),\n undefined,\n false,\n );\n}\n\nexport {\n getSignalBaseName,\n connectSignal,\n disconnectSignal,\n emitSignal,\n hasSignalListener,\n isSignalHandlerConnected,\n untrackConnection,\n type SignalHandler,\n};\n"]}
|
package/dist/t.d.ts
CHANGED
|
@@ -1,43 +1,87 @@
|
|
|
1
1
|
import { bind } from "./bind.js";
|
|
2
2
|
import * as helpers from "./descriptors.js";
|
|
3
3
|
import { fn } from "./fn.js";
|
|
4
|
+
/** The descriptor factories and function binders exposed as {@link t}. */
|
|
4
5
|
type T = {
|
|
6
|
+
/** Binds a symbol in a shared library to a callable that marshals its arguments and return value. */
|
|
5
7
|
bind: typeof bind;
|
|
8
|
+
/** Descriptor for a `gint8`, marshalled as a number. */
|
|
6
9
|
int8: typeof helpers.int8T;
|
|
10
|
+
/** Descriptor for a `guint8`, marshalled as a number. */
|
|
7
11
|
uint8: typeof helpers.uint8T;
|
|
12
|
+
/** Descriptor for a `gint16`, marshalled as a number. */
|
|
8
13
|
int16: typeof helpers.int16T;
|
|
14
|
+
/** Descriptor for a `guint16`, marshalled as a number. */
|
|
9
15
|
uint16: typeof helpers.uint16T;
|
|
16
|
+
/** Descriptor for a `gint32`, marshalled as a number. */
|
|
10
17
|
int32: typeof helpers.int32T;
|
|
18
|
+
/** Descriptor for a `guint32`, marshalled as a number. */
|
|
11
19
|
uint32: typeof helpers.uint32T;
|
|
20
|
+
/** Descriptor for a `gint64`, marshalled as a number and rejected outside the 2^53 safe range. */
|
|
12
21
|
int64: typeof helpers.int64T;
|
|
22
|
+
/** Descriptor for a `guint64`, marshalled as a number and rejected outside the 2^53 safe range. */
|
|
13
23
|
uint64: typeof helpers.uint64T;
|
|
24
|
+
/** Descriptor for a `gint64`, marshalled as a bigint so the full 64-bit range survives. */
|
|
14
25
|
bigint64: typeof helpers.bigint64T;
|
|
26
|
+
/** Descriptor for a `guint64`, marshalled as a bigint so the full 64-bit range survives. */
|
|
15
27
|
biguint64: typeof helpers.biguint64T;
|
|
28
|
+
/** Descriptor for a `GType`, marshalled as a bigint and recognized as a GType by GValue conversion. */
|
|
16
29
|
gtype: typeof helpers.gtypeT;
|
|
30
|
+
/** Descriptor for a `gfloat`. */
|
|
17
31
|
float32: typeof helpers.float32T;
|
|
32
|
+
/** Descriptor for a `gdouble`. */
|
|
18
33
|
float64: typeof helpers.float64T;
|
|
34
|
+
/** Descriptor for a `gboolean`, marshalled as a JavaScript boolean. */
|
|
19
35
|
boolean: typeof helpers.booleanT;
|
|
36
|
+
/** Descriptor for the absence of a value, used as the return descriptor of a `void` function. */
|
|
20
37
|
void: typeof helpers.voidT;
|
|
38
|
+
/** Descriptor for a `gunichar`, marshalled as a single-character string or a codepoint number. */
|
|
21
39
|
unichar: typeof helpers.unicharT;
|
|
40
|
+
/** Descriptor for an opaque `gpointer` argument, taken from a typed array's memory or a numeric address. */
|
|
22
41
|
buffer: typeof helpers.bufferT;
|
|
42
|
+
/**
|
|
43
|
+
* Builds a descriptor for a C string, whose optional length sizes the caller-allocated buffer
|
|
44
|
+
* used when the string is passed by reference.
|
|
45
|
+
*/
|
|
23
46
|
string: typeof helpers.stringT;
|
|
47
|
+
/** Builds a descriptor for a `GObject`, wrapped in the class registered for its runtime GType. */
|
|
24
48
|
object: typeof helpers.objectT;
|
|
49
|
+
/** Builds a descriptor for a `GBoxed` value of the named type. */
|
|
25
50
|
boxed: typeof helpers.boxedT;
|
|
51
|
+
/** Builds a descriptor for a plain C struct. */
|
|
26
52
|
struct: typeof helpers.structT;
|
|
53
|
+
/** Builds a descriptor for a fundamental type whose lifetime is managed by named ref and unref functions. */
|
|
27
54
|
fundamental: typeof helpers.fundamentalT;
|
|
55
|
+
/** Wraps a descriptor in a pointer to it, for an output or inout argument. */
|
|
28
56
|
ref: typeof helpers.refT;
|
|
57
|
+
/** Builds a descriptor for a `GHashTable`, marshalled as an array of key/value pairs. */
|
|
29
58
|
hashTable: typeof helpers.hashTableT;
|
|
59
|
+
/** Builds a descriptor for an enumeration, resolving its GType from the named `get_type` function. */
|
|
30
60
|
enum: typeof helpers.enumT;
|
|
61
|
+
/** Builds a descriptor for a flags type, resolving its GType from the named `get_type` function. */
|
|
31
62
|
flags: typeof helpers.flagsT;
|
|
63
|
+
/** Builds a descriptor for an array of items in one of the supported container layouts. */
|
|
32
64
|
array: typeof helpers.arrayT;
|
|
65
|
+
/** Builds a descriptor for a `GList` of items. */
|
|
33
66
|
list: typeof helpers.listT;
|
|
67
|
+
/** Builds a descriptor for a `GSList` of items. */
|
|
34
68
|
slist: typeof helpers.slistT;
|
|
69
|
+
/** Builds a descriptor for a `GPtrArray` of items. */
|
|
35
70
|
ptrArray: typeof helpers.ptrArrayT;
|
|
71
|
+
/** Builds a descriptor for a `GArray` of items, optionally with an explicit element size. */
|
|
36
72
|
gArray: typeof helpers.gArrayT;
|
|
73
|
+
/** Builds a descriptor for a `GByteArray`. */
|
|
37
74
|
byteArray: typeof helpers.byteArrayT;
|
|
75
|
+
/** Builds a descriptor for a C array whose length is carried by another argument. */
|
|
38
76
|
sizedArray: typeof helpers.sizedArrayT;
|
|
77
|
+
/** Builds a descriptor for a C array of a fixed length. */
|
|
39
78
|
fixedArray: typeof helpers.fixedArrayT;
|
|
79
|
+
/** Builds a descriptor for a function pointer, marshalling a JavaScript function into a native closure. */
|
|
40
80
|
callback: typeof helpers.callbackT;
|
|
81
|
+
/**
|
|
82
|
+
* Binds a native function, wiring up argument directions, `GError` checking, and packing output
|
|
83
|
+
* arguments into the result.
|
|
84
|
+
*/
|
|
41
85
|
fn: typeof fn;
|
|
42
86
|
};
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