@gtkx/runtime 0.0.1 → 1.0.0-rc.1
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/LICENSE +373 -0
- package/README.md +171 -3
- package/dist/arg.d.ts +12 -0
- package/dist/arg.d.ts.map +1 -0
- package/dist/arg.js +5 -0
- package/dist/arg.js.map +1 -0
- package/dist/bind.d.ts +4 -0
- package/dist/bind.d.ts.map +1 -0
- package/dist/bind.js +17 -0
- package/dist/bind.js.map +1 -0
- package/dist/callback.d.ts +13 -0
- package/dist/callback.d.ts.map +1 -0
- package/dist/callback.js +68 -0
- package/dist/callback.js.map +1 -0
- package/dist/descriptors.d.ts +155 -0
- package/dist/descriptors.d.ts.map +1 -0
- package/dist/descriptors.js +103 -0
- package/dist/descriptors.js.map +1 -0
- package/dist/error.d.ts +25 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +29 -0
- package/dist/error.js.map +1 -0
- package/dist/fn.d.ts +10 -0
- package/dist/fn.d.ts.map +1 -0
- package/dist/fn.js +79 -0
- package/dist/fn.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +5 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +5 -0
- package/dist/internal.js.map +1 -0
- package/dist/library.d.ts +7 -0
- package/dist/library.d.ts.map +1 -0
- package/dist/library.js +13 -0
- package/dist/library.js.map +1 -0
- package/dist/lifecycle.d.ts +41 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +55 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/listeners.d.ts +36 -0
- package/dist/listeners.d.ts.map +1 -0
- package/dist/listeners.js +72 -0
- package/dist/listeners.js.map +1 -0
- package/dist/mixin.d.ts +19 -0
- package/dist/mixin.d.ts.map +1 -0
- package/dist/mixin.js +39 -0
- package/dist/mixin.js.map +1 -0
- package/dist/native-value.d.ts +12 -0
- package/dist/native-value.d.ts.map +1 -0
- package/dist/native-value.js +72 -0
- package/dist/native-value.js.map +1 -0
- package/dist/object.d.ts +31 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +63 -0
- package/dist/object.js.map +1 -0
- package/dist/promisify.d.ts +13 -0
- package/dist/promisify.d.ts.map +1 -0
- package/dist/promisify.js +36 -0
- package/dist/promisify.js.map +1 -0
- package/dist/register-class.d.ts +17 -0
- package/dist/register-class.d.ts.map +1 -0
- package/dist/register-class.js +124 -0
- package/dist/register-class.js.map +1 -0
- package/dist/registry.d.ts +67 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +167 -0
- package/dist/registry.js.map +1 -0
- package/dist/signal.d.ts +34 -0
- package/dist/signal.d.ts.map +1 -0
- package/dist/signal.js +88 -0
- package/dist/signal.js.map +1 -0
- package/dist/t.d.ts +50 -0
- package/dist/t.d.ts.map +1 -0
- package/dist/t.js +48 -0
- package/dist/t.js.map +1 -0
- package/dist/tuple.d.ts +6 -0
- package/dist/tuple.d.ts.map +1 -0
- package/dist/tuple.js +23 -0
- package/dist/tuple.js.map +1 -0
- package/dist/type.d.ts +81 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js +174 -0
- package/dist/type.js.map +1 -0
- package/dist/value.d.ts +20 -0
- package/dist/value.d.ts.map +1 -0
- package/dist/value.js +227 -0
- package/dist/value.js.map +1 -0
- package/package.json +56 -13
- package/src/arg.ts +13 -0
- package/src/bind.ts +22 -0
- package/src/callback.ts +87 -0
- package/src/descriptors.ts +208 -0
- package/src/error.ts +49 -0
- package/src/fn.ts +108 -0
- package/src/index.ts +57 -0
- package/src/internal.ts +12 -0
- package/src/library.ts +15 -0
- package/src/lifecycle.ts +73 -0
- package/src/listeners.ts +84 -0
- package/src/mixin.ts +48 -0
- package/src/native-value.ts +78 -0
- package/src/object.ts +83 -0
- package/src/promisify.ts +40 -0
- package/src/register-class.ts +178 -0
- package/src/registry.ts +221 -0
- package/src/signal.ts +152 -0
- package/src/t.ts +88 -0
- package/src/tuple.ts +25 -0
- package/src/type.ts +200 -0
- package/src/value.ts +307 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACnF,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EACH,SAAS,EACT,aAAa,EACb,YAAY,EACZ,4BAA4B,EAC5B,qBAAqB,EACrB,0BAA0B,EAC1B,OAAO,GACV,MAAM,YAAY,CAAC"}
|
package/dist/internal.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { checkError, createErrorDomain } from "./error.js";
|
|
2
|
+
export { registerClassType, resolveWrapperClass, wrapHandle } from "./registry.js";
|
|
3
|
+
export { resolveType } from "./type.js";
|
|
4
|
+
export { fromValue, getBoxedValue, getValueType, inoutValueForBoxedDescriptor, newValueForDescriptor, outValueForBoxedDescriptor, toValue, } from "./value.js";
|
|
5
|
+
//# sourceMappingURL=internal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal.js","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACnF,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EACH,SAAS,EACT,aAAa,EACb,YAAY,EACZ,4BAA4B,EAC5B,qBAAqB,EACrB,0BAA0B,EAC1B,OAAO,GACV,MAAM,YAAY,CAAC","sourcesContent":["export { checkError, createErrorDomain } from \"./error.js\";\nexport { registerClassType, resolveWrapperClass, wrapHandle } from \"./registry.js\";\nexport { resolveType } from \"./type.js\";\nexport {\n fromValue,\n getBoxedValue,\n getValueType,\n inoutValueForBoxedDescriptor,\n newValueForDescriptor,\n outValueForBoxedDescriptor,\n toValue,\n} from \"./value.js\";\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type BoxedDescriptor, type FundamentalDescriptor } from "./descriptors.js";
|
|
2
|
+
export declare const LIB = "libgobject-2.0.so.0,libglib-2.0.so.0";
|
|
3
|
+
export declare const VALUE_SIZE = 24;
|
|
4
|
+
export declare const VALUE_T: BoxedDescriptor;
|
|
5
|
+
export declare const PARAM_T: FundamentalDescriptor;
|
|
6
|
+
export declare const VARIANT_T: FundamentalDescriptor;
|
|
7
|
+
//# sourceMappingURL=library.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"library.d.ts","sourceRoot":"","sources":["../src/library.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAU,KAAK,qBAAqB,EAAgB,MAAM,kBAAkB,CAAC;AAE1G,eAAO,MAAM,GAAG,yCAAyC,CAAC;AAC1D,eAAO,MAAM,UAAU,KAAK,CAAC;AAC7B,eAAO,MAAM,OAAO,EAAE,eAA6F,CAAC;AAEpH,eAAO,MAAM,OAAO,EAAE,qBAGpB,CAAC;AAEH,eAAO,MAAM,SAAS,EAAE,qBAGtB,CAAC"}
|
package/dist/library.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { boxedT, fundamentalT } from "./descriptors.js";
|
|
2
|
+
export const LIB = "libgobject-2.0.so.0,libglib-2.0.so.0";
|
|
3
|
+
export const VALUE_SIZE = 24;
|
|
4
|
+
export const VALUE_T = boxedT("GValue", { sharedLibrary: LIB, getTypeFnName: "g_value_get_type" });
|
|
5
|
+
export const PARAM_T = fundamentalT(LIB, "g_param_spec_ref", "g_param_spec_unref", {
|
|
6
|
+
ownership: "borrowed",
|
|
7
|
+
typeName: "GParam",
|
|
8
|
+
});
|
|
9
|
+
export const VARIANT_T = fundamentalT(LIB, "g_variant_ref", "g_variant_unref", {
|
|
10
|
+
ownership: "borrowed",
|
|
11
|
+
typeName: "GVariant",
|
|
12
|
+
});
|
|
13
|
+
//# sourceMappingURL=library.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"library.js","sourceRoot":"","sources":["../src/library.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,MAAM,EAA8B,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAE1G,MAAM,CAAC,MAAM,GAAG,GAAG,sCAAsC,CAAC;AAC1D,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,CAAC;AAC7B,MAAM,CAAC,MAAM,OAAO,GAAoB,MAAM,CAAC,QAAQ,EAAE,EAAE,aAAa,EAAE,GAAG,EAAE,aAAa,EAAE,kBAAkB,EAAE,CAAC,CAAC;AAEpH,MAAM,CAAC,MAAM,OAAO,GAA0B,YAAY,CAAC,GAAG,EAAE,kBAAkB,EAAE,oBAAoB,EAAE;IACtG,SAAS,EAAE,UAAU;IACrB,QAAQ,EAAE,QAAQ;CACrB,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,SAAS,GAA0B,YAAY,CAAC,GAAG,EAAE,eAAe,EAAE,iBAAiB,EAAE;IAClG,SAAS,EAAE,UAAU;IACrB,QAAQ,EAAE,UAAU;CACvB,CAAC,CAAC","sourcesContent":["import { type BoxedDescriptor, boxedT, type FundamentalDescriptor, fundamentalT } from \"./descriptors.js\";\n\nexport const LIB = \"libgobject-2.0.so.0,libglib-2.0.so.0\";\nexport const VALUE_SIZE = 24;\nexport const VALUE_T: BoxedDescriptor = boxedT(\"GValue\", { sharedLibrary: LIB, getTypeFnName: \"g_value_get_type\" });\n\nexport const PARAM_T: FundamentalDescriptor = fundamentalT(LIB, \"g_param_spec_ref\", \"g_param_spec_unref\", {\n ownership: \"borrowed\",\n typeName: \"GParam\",\n});\n\nexport const VARIANT_T: FundamentalDescriptor = fundamentalT(LIB, \"g_variant_ref\", \"g_variant_unref\", {\n ownership: \"borrowed\",\n typeName: \"GVariant\",\n});\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal structural interface of a GTK4/GIO application needed to register, run,
|
|
3
|
+
* and shut it down, plus the signals its lifecycle helpers connect to.
|
|
4
|
+
*/
|
|
5
|
+
export type ApplicationLike = {
|
|
6
|
+
getIsRegistered(): boolean;
|
|
7
|
+
register(cancellable: null): boolean;
|
|
8
|
+
activate(): void;
|
|
9
|
+
quit(): void;
|
|
10
|
+
run(argv: string[]): number;
|
|
11
|
+
getWindows?(): object[];
|
|
12
|
+
removeWindow?(window: object): void;
|
|
13
|
+
on(signal: "activate" | "shutdown", handler: () => void): unknown;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Registers a callback to run once when the process quits, before the native
|
|
17
|
+
* runtime is torn down.
|
|
18
|
+
*
|
|
19
|
+
* @param callback Invoked during shutdown.
|
|
20
|
+
*/
|
|
21
|
+
export declare const onExit: (callback: () => void) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Runs every registered exit callback and shuts down the native runtime. Safe to
|
|
24
|
+
* call more than once; only the first call takes effect.
|
|
25
|
+
*/
|
|
26
|
+
export declare const quit: () => void;
|
|
27
|
+
/**
|
|
28
|
+
* Registers the application if needed and activates it, keeping the runtime alive
|
|
29
|
+
* while it is active and releasing it on shutdown.
|
|
30
|
+
*
|
|
31
|
+
* @param application The application to register and activate.
|
|
32
|
+
*/
|
|
33
|
+
export declare const runApplication: (application: ApplicationLike) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Closes all of the application's windows and drives its main loop until it quits,
|
|
36
|
+
* so pending shutdown work can complete before the process exits.
|
|
37
|
+
*
|
|
38
|
+
* @param application The application to shut down.
|
|
39
|
+
*/
|
|
40
|
+
export declare const quitApplication: (application: ApplicationLike) => void;
|
|
41
|
+
//# sourceMappingURL=lifecycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG;IAC1B,eAAe,IAAI,OAAO,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,IAAI,GAAG,OAAO,CAAC;IACrC,QAAQ,IAAI,IAAI,CAAC;IACjB,IAAI,IAAI,IAAI,CAAC;IACb,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAC5B,UAAU,CAAC,IAAI,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,EAAE,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,EAAE,OAAO,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC;CACrE,CAAC;AAKF;;;;;GAKG;AACH,eAAO,MAAM,MAAM,aAAc,MAAM,IAAI,KAAG,IAE7C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,IAAI,QAAO,IAOvB,CAAC;AAIF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,gBAAiB,eAAe,KAAG,IAM7D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,gBAAiB,eAAe,KAAG,IAM9D,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { keepAlive, quit as nativeQuit } from "@gtkx/native";
|
|
2
|
+
import { blockMatchedSignalHandlers } from "./signal.js";
|
|
3
|
+
const shutdownCallbacks = [];
|
|
4
|
+
let hasQuit = false;
|
|
5
|
+
/**
|
|
6
|
+
* Registers a callback to run once when the process quits, before the native
|
|
7
|
+
* runtime is torn down.
|
|
8
|
+
*
|
|
9
|
+
* @param callback Invoked during shutdown.
|
|
10
|
+
*/
|
|
11
|
+
export const onExit = (callback) => {
|
|
12
|
+
shutdownCallbacks.push(callback);
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Runs every registered exit callback and shuts down the native runtime. Safe to
|
|
16
|
+
* call more than once; only the first call takes effect.
|
|
17
|
+
*/
|
|
18
|
+
export const quit = () => {
|
|
19
|
+
if (hasQuit)
|
|
20
|
+
return;
|
|
21
|
+
hasQuit = true;
|
|
22
|
+
for (const callback of shutdownCallbacks)
|
|
23
|
+
callback();
|
|
24
|
+
nativeQuit();
|
|
25
|
+
};
|
|
26
|
+
process.on("exit", quit);
|
|
27
|
+
/**
|
|
28
|
+
* Registers the application if needed and activates it, keeping the runtime alive
|
|
29
|
+
* while it is active and releasing it on shutdown.
|
|
30
|
+
*
|
|
31
|
+
* @param application The application to register and activate.
|
|
32
|
+
*/
|
|
33
|
+
export const runApplication = (application) => {
|
|
34
|
+
application.on("activate", () => keepAlive(true));
|
|
35
|
+
application.on("shutdown", () => keepAlive(false));
|
|
36
|
+
if (!application.getIsRegistered())
|
|
37
|
+
application.register(null);
|
|
38
|
+
application.activate();
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Closes all of the application's windows and drives its main loop until it quits,
|
|
42
|
+
* so pending shutdown work can complete before the process exits.
|
|
43
|
+
*
|
|
44
|
+
* @param application The application to shut down.
|
|
45
|
+
*/
|
|
46
|
+
export const quitApplication = (application) => {
|
|
47
|
+
if (!application.getIsRegistered())
|
|
48
|
+
return;
|
|
49
|
+
for (const window of application.getWindows?.() ?? [])
|
|
50
|
+
application.removeWindow?.(window);
|
|
51
|
+
application.on("shutdown", () => application.quit());
|
|
52
|
+
blockMatchedSignalHandlers(application, "activate");
|
|
53
|
+
application.run([]);
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=lifecycle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,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E,EAAE,IAAI,EAAE;QAAE,WAAW,CAAC,YAAY,EAAE,CAAC,MAAM,CAAC,CAAC;IAC1F,WAAW,CAAC,EAAE,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC;IACrD,0BAA0B,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACpD,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AACxB,CAAC,CAAC","sourcesContent":["import { keepAlive, quit as nativeQuit } from \"@gtkx/native\";\nimport { blockMatchedSignalHandlers } from \"./signal.js\";\n\n/**\n * Minimal structural interface of a GTK4/GIO application needed to register, run,\n * and shut it down, plus the signals its lifecycle helpers connect to.\n */\nexport type ApplicationLike = {\n getIsRegistered(): boolean;\n register(cancellable: null): boolean;\n activate(): void;\n quit(): void;\n run(argv: string[]): number;\n getWindows?(): object[];\n removeWindow?(window: object): void;\n on(signal: \"activate\" | \"shutdown\", handler: () => void): unknown;\n};\n\nconst shutdownCallbacks: (() => void)[] = [];\nlet hasQuit = false;\n\n/**\n * Registers a callback to run once when the process quits, before the native\n * runtime is torn down.\n *\n * @param callback Invoked during shutdown.\n */\nexport const onExit = (callback: () => void): void => {\n shutdownCallbacks.push(callback);\n};\n\n/**\n * Runs every registered exit callback and shuts down the native runtime. Safe to\n * call more than once; only the first call takes effect.\n */\nexport const quit = (): void => {\n if (hasQuit) return;\n hasQuit = true;\n\n for (const callback of shutdownCallbacks) callback();\n\n nativeQuit();\n};\n\nprocess.on(\"exit\", quit);\n\n/**\n * Registers the application if needed and activates it, keeping the runtime alive\n * while it is active and releasing it on shutdown.\n *\n * @param application The application to register and activate.\n */\nexport const runApplication = (application: ApplicationLike): void => {\n application.on(\"activate\", () => keepAlive(true));\n application.on(\"shutdown\", () => keepAlive(false));\n\n if (!application.getIsRegistered()) application.register(null);\n application.activate();\n};\n\n/**\n * Closes all of the application's windows and drives its main loop until it quits,\n * so pending shutdown work can complete before the process exits.\n *\n * @param application The application to shut down.\n */\nexport const quitApplication = (application: ApplicationLike): void => {\n if (!application.getIsRegistered()) return;\n for (const window of application.getWindows?.() ?? []) application.removeWindow?.(window);\n application.on(\"shutdown\", () => application.quit());\n blockMatchedSignalHandlers(application, \"activate\");\n application.run([]);\n};\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { SignalHandler } from "./signal.js";
|
|
2
|
+
type SignalConnectable = {
|
|
3
|
+
connect(signal: string, handler: SignalHandler, after?: boolean): number;
|
|
4
|
+
disconnect(handlerId: number): void;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Connects a handler to a signal and tracks it so it can later be removed with
|
|
8
|
+
* {@link offSignal}.
|
|
9
|
+
*
|
|
10
|
+
* @param instance The object emitting the signal.
|
|
11
|
+
* @param signal The signal name to connect to.
|
|
12
|
+
* @param handler The callback invoked on each emission.
|
|
13
|
+
* @param after When true, run the handler after the default handler.
|
|
14
|
+
*/
|
|
15
|
+
export declare function onSignal(instance: SignalConnectable, signal: string, handler: SignalHandler, after?: boolean): void;
|
|
16
|
+
/**
|
|
17
|
+
* Connects a handler that runs at most once, disconnecting itself after the first
|
|
18
|
+
* emission.
|
|
19
|
+
*
|
|
20
|
+
* @param instance The object emitting the signal.
|
|
21
|
+
* @param signal The signal name to connect to.
|
|
22
|
+
* @param handler The callback invoked on the first emission.
|
|
23
|
+
* @param after When true, run the handler after the default handler.
|
|
24
|
+
*/
|
|
25
|
+
export declare function onceSignal(instance: SignalConnectable, signal: string, handler: SignalHandler, after?: boolean): void;
|
|
26
|
+
/**
|
|
27
|
+
* Disconnects a handler previously connected with {@link onSignal} or
|
|
28
|
+
* {@link onceSignal}. Does nothing if the handler is not currently connected.
|
|
29
|
+
*
|
|
30
|
+
* @param instance The object the handler was connected to.
|
|
31
|
+
* @param signal The signal name the handler was connected to.
|
|
32
|
+
* @param handler The handler to disconnect.
|
|
33
|
+
*/
|
|
34
|
+
export declare function offSignal(instance: SignalConnectable, signal: string, handler: SignalHandler): void;
|
|
35
|
+
export {};
|
|
36
|
+
//# sourceMappingURL=listeners.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listeners.d.ts","sourceRoot":"","sources":["../src/listeners.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,KAAK,iBAAiB,GAAG;IACrB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IACzE,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CACvC,CAAC;AA4BF;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAGnH;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAWrH;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAMnG"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const listenerTable = new WeakMap();
|
|
2
|
+
const findListenerHandlerId = (instance, signal, handler) => listenerTable.get(instance)?.get(signal)?.get(handler);
|
|
3
|
+
const trackListener = (instance, signal, handler, handlerId) => {
|
|
4
|
+
let bySignal = listenerTable.get(instance);
|
|
5
|
+
if (!bySignal) {
|
|
6
|
+
bySignal = new Map();
|
|
7
|
+
listenerTable.set(instance, bySignal);
|
|
8
|
+
}
|
|
9
|
+
let byHandler = bySignal.get(signal);
|
|
10
|
+
if (!byHandler) {
|
|
11
|
+
byHandler = new Map();
|
|
12
|
+
bySignal.set(signal, byHandler);
|
|
13
|
+
}
|
|
14
|
+
byHandler.set(handler, handlerId);
|
|
15
|
+
};
|
|
16
|
+
const untrackListener = (instance, signal, handler) => {
|
|
17
|
+
const bySignal = listenerTable.get(instance);
|
|
18
|
+
const byHandler = bySignal?.get(signal);
|
|
19
|
+
byHandler?.delete(handler);
|
|
20
|
+
if (byHandler?.size === 0)
|
|
21
|
+
bySignal?.delete(signal);
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Connects a handler to a signal and tracks it so it can later be removed with
|
|
25
|
+
* {@link offSignal}.
|
|
26
|
+
*
|
|
27
|
+
* @param instance The object emitting the signal.
|
|
28
|
+
* @param signal The signal name to connect to.
|
|
29
|
+
* @param handler The callback invoked on each emission.
|
|
30
|
+
* @param after When true, run the handler after the default handler.
|
|
31
|
+
*/
|
|
32
|
+
export function onSignal(instance, signal, handler, after) {
|
|
33
|
+
const handlerId = instance.connect(signal, handler, after);
|
|
34
|
+
trackListener(instance, signal, handler, handlerId);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Connects a handler that runs at most once, disconnecting itself after the first
|
|
38
|
+
* emission.
|
|
39
|
+
*
|
|
40
|
+
* @param instance The object emitting the signal.
|
|
41
|
+
* @param signal The signal name to connect to.
|
|
42
|
+
* @param handler The callback invoked on the first emission.
|
|
43
|
+
* @param after When true, run the handler after the default handler.
|
|
44
|
+
*/
|
|
45
|
+
export function onceSignal(instance, signal, handler, after) {
|
|
46
|
+
let handlerId = 0;
|
|
47
|
+
const wrapped = (...args) => {
|
|
48
|
+
untrackListener(instance, signal, wrapped);
|
|
49
|
+
untrackListener(instance, signal, handler);
|
|
50
|
+
instance.disconnect(handlerId);
|
|
51
|
+
return handler(...args);
|
|
52
|
+
};
|
|
53
|
+
handlerId = instance.connect(signal, wrapped, after);
|
|
54
|
+
trackListener(instance, signal, wrapped, handlerId);
|
|
55
|
+
trackListener(instance, signal, handler, handlerId);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Disconnects a handler previously connected with {@link onSignal} or
|
|
59
|
+
* {@link onceSignal}. Does nothing if the handler is not currently connected.
|
|
60
|
+
*
|
|
61
|
+
* @param instance The object the handler was connected to.
|
|
62
|
+
* @param signal The signal name the handler was connected to.
|
|
63
|
+
* @param handler The handler to disconnect.
|
|
64
|
+
*/
|
|
65
|
+
export function offSignal(instance, signal, handler) {
|
|
66
|
+
const handlerId = findListenerHandlerId(instance, signal, handler);
|
|
67
|
+
if (handlerId !== undefined) {
|
|
68
|
+
instance.disconnect(handlerId);
|
|
69
|
+
untrackListener(instance, signal, handler);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=listeners.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listeners.js","sourceRoot":"","sources":["../src/listeners.ts"],"names":[],"mappings":"AAOA,MAAM,aAAa,GAAG,IAAI,OAAO,EAAmD,CAAC;AAErF,MAAM,qBAAqB,GAAG,CAAC,QAAgB,EAAE,MAAc,EAAE,OAAsB,EAAsB,EAAE,CAC3G,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;AAE3D,MAAM,aAAa,GAAG,CAAC,QAAgB,EAAE,MAAc,EAAE,OAAsB,EAAE,SAAiB,EAAQ,EAAE;IACxG,IAAI,QAAQ,GAAG,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC3C,IAAI,CAAC,QAAQ,EAAE,CAAC;QACZ,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAC;QACrB,aAAa,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC1C,CAAC;IACD,IAAI,SAAS,GAAG,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACrC,IAAI,C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OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;IAC5B,CAAC,CAAC;IACF,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACrD,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;IACpD,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AACxD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,QAA2B,EAAE,MAAc,EAAE,OAAsB;IACzF,MAAM,SAAS,GAAG,qBAAqB,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACnE,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC1B,QAAQ,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;QAC/B,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;AACL,CAAC","sourcesContent":["import type { SignalHandler } from \"./signal.js\";\n\ntype SignalConnectable = {\n connect(signal: string, handler: SignalHandler, after?: boolean): number;\n disconnect(handlerId: number): void;\n};\n\nconst listenerTable = new WeakMap<object, Map<string, Map<SignalHandler, number>>>();\n\nconst findListenerHandlerId = (instance: object, signal: string, handler: SignalHandler): number | undefined =>\n listenerTable.get(instance)?.get(signal)?.get(handler);\n\nconst trackListener = (instance: object, signal: string, handler: SignalHandler, handlerId: number): void => {\n let bySignal = listenerTable.get(instance);\n if (!bySignal) {\n bySignal = new Map();\n listenerTable.set(instance, bySignal);\n }\n let byHandler = bySignal.get(signal);\n if (!byHandler) {\n byHandler = new Map();\n bySignal.set(signal, byHandler);\n }\n byHandler.set(handler, handlerId);\n};\n\nconst untrackListener = (instance: object, signal: string, handler: SignalHandler): void => {\n const bySignal = listenerTable.get(instance);\n const byHandler = bySignal?.get(signal);\n byHandler?.delete(handler);\n if (byHandler?.size === 0) bySignal?.delete(signal);\n};\n\n/**\n * Connects a handler to a signal and tracks it so it can later be removed with\n * {@link offSignal}.\n *\n * @param instance The object emitting the signal.\n * @param signal The signal name to connect to.\n * @param handler The callback invoked on each emission.\n * @param after When true, run the handler after the default handler.\n */\nexport function onSignal(instance: SignalConnectable, signal: string, handler: SignalHandler, after?: boolean): void {\n const handlerId = instance.connect(signal, handler, after);\n trackListener(instance, signal, handler, handlerId);\n}\n\n/**\n * Connects a handler that runs at most once, disconnecting itself after the first\n * emission.\n *\n * @param instance The object emitting the signal.\n * @param signal The signal name to connect to.\n * @param handler The callback invoked on the first emission.\n * @param after When true, run the handler after the default handler.\n */\nexport function onceSignal(instance: SignalConnectable, signal: string, handler: SignalHandler, after?: boolean): void {\n let handlerId = 0;\n const wrapped: SignalHandler = (...args) => {\n untrackListener(instance, signal, wrapped);\n untrackListener(instance, signal, handler);\n instance.disconnect(handlerId);\n return handler(...args);\n };\n handlerId = instance.connect(signal, wrapped, after);\n trackListener(instance, signal, wrapped, handlerId);\n trackListener(instance, signal, handler, handlerId);\n}\n\n/**\n * Disconnects a handler previously connected with {@link onSignal} or\n * {@link onceSignal}. Does nothing if the handler is not currently connected.\n *\n * @param instance The object the handler was connected to.\n * @param signal The signal name the handler was connected to.\n * @param handler The handler to disconnect.\n */\nexport function offSignal(instance: SignalConnectable, signal: string, handler: SignalHandler): void {\n const handlerId = findListenerHandlerId(instance, signal, handler);\n if (handlerId !== undefined) {\n instance.disconnect(handlerId);\n untrackListener(instance, signal, handler);\n }\n}\n"]}
|
package/dist/mixin.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AnyClass } from "@gtkx/utils";
|
|
2
|
+
export type MixinReceiver = {
|
|
3
|
+
connect(signal: string, handler: (...args: unknown[]) => unknown, after?: boolean): number;
|
|
4
|
+
emit(signal: string, ...args: unknown[]): unknown;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* A factory that, given a base class, returns a subclass adding extra members to
|
|
8
|
+
* be merged onto a target prototype.
|
|
9
|
+
*/
|
|
10
|
+
export type Mixin = (base: AnyClass<MixinReceiver>) => AnyClass;
|
|
11
|
+
/**
|
|
12
|
+
* Copies each mixin's prototype members onto the target class prototype, skipping
|
|
13
|
+
* any member already defined anywhere in the target's class chain.
|
|
14
|
+
*
|
|
15
|
+
* @param target The class whose prototype receives the mixin members.
|
|
16
|
+
* @param mixins The mixins to apply, in order.
|
|
17
|
+
*/
|
|
18
|
+
export declare function installMixins(target: AnyClass, mixins: Mixin[]): void;
|
|
19
|
+
//# sourceMappingURL=mixin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mixin.d.ts","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,MAAM,aAAa,GAAG;IACxB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC3F,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;CACrD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,KAAK,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,aAAa,CAAC,KAAK,QAAQ,CAAC;AAWhE;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAkBrE"}
|
package/dist/mixin.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
function definedInClassChain(prototype, key) {
|
|
2
|
+
let current = prototype;
|
|
3
|
+
while (current !== null && current !== Object.prototype) {
|
|
4
|
+
if (Object.hasOwn(current, key))
|
|
5
|
+
return true;
|
|
6
|
+
current = Object.getPrototypeOf(current);
|
|
7
|
+
}
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Copies each mixin's prototype members onto the target class prototype, skipping
|
|
12
|
+
* any member already defined anywhere in the target's class chain.
|
|
13
|
+
*
|
|
14
|
+
* @param target The class whose prototype receives the mixin members.
|
|
15
|
+
* @param mixins The mixins to apply, in order.
|
|
16
|
+
*/
|
|
17
|
+
export function installMixins(target, mixins) {
|
|
18
|
+
const empty = class {
|
|
19
|
+
connect() {
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
emit() {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
for (const mixin of mixins) {
|
|
27
|
+
const layer = mixin(empty).prototype;
|
|
28
|
+
for (const key of Object.getOwnPropertyNames(layer)) {
|
|
29
|
+
if (key === "constructor")
|
|
30
|
+
continue;
|
|
31
|
+
if (definedInClassChain(target.prototype, key))
|
|
32
|
+
continue;
|
|
33
|
+
const descriptor = Object.getOwnPropertyDescriptor(layer, key);
|
|
34
|
+
if (descriptor !== undefined)
|
|
35
|
+
Object.defineProperty(target.prototype, key, descriptor);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=mixin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mixin.js","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AAaA,SAAS,mBAAmB,CAAC,SAAiB,EAAE,GAAW;IACvD,IAAI,OAAO,GAAkB,SAAS,CAAC;IACvC,OAAO,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,MAAM,CAAC,SAAS,EAAE,CAAC;QACtD,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC;YAAE,OAAO,IAAI,CAAC;QAC7C,OAAO,GAAG,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,MAAgB,EAAE,MAAe;IAC3D,MAAM,KAAK,GAA4B;QACnC,OAAO;YACH,OAAO,CAAC,CAAC;QACb,CAAC;QACD,IAAI;YACA,OAAO,SAAS,CAAC;QACrB,CAAC;KACJ,CAAC;IACF,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACzB,MAAM,KAAK,GAAW,KAAK,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC;QAC7C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,EAAE,CAAC;YAClD,IAAI,GAAG,KAAK,aAAa;gBAAE,SAAS;YACpC,IAAI,mBAAmB,CAAC,MAAM,CAAC,SAAS,EAAE,GAAG,CAAC;gBAAE,SAAS;YACzD,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;YAC/D,IAAI,UAAU,KAAK,SAAS;gBAAE,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QAC3F,CAAC;IACL,CAAC;AACL,CAAC","sourcesContent":["import type { AnyClass } from \"@gtkx/utils\";\n\nexport type MixinReceiver = {\n connect(signal: string, handler: (...args: unknown[]) => unknown, after?: boolean): number;\n emit(signal: string, ...args: unknown[]): unknown;\n};\n\n/**\n * A factory that, given a base class, returns a subclass adding extra members to\n * be merged onto a target prototype.\n */\nexport type Mixin = (base: AnyClass<MixinReceiver>) => AnyClass;\n\nfunction definedInClassChain(prototype: object, key: string): boolean {\n let current: object | null = prototype;\n while (current !== null && current !== Object.prototype) {\n if (Object.hasOwn(current, key)) return true;\n current = Object.getPrototypeOf(current);\n }\n return false;\n}\n\n/**\n * Copies each mixin's prototype members onto the target class prototype, skipping\n * any member already defined anywhere in the target's class chain.\n *\n * @param target The class whose prototype receives the mixin members.\n * @param mixins The mixins to apply, in order.\n */\nexport function installMixins(target: AnyClass, mixins: Mixin[]): void {\n const empty: AnyClass<MixinReceiver> = class {\n connect(): number {\n return 0;\n }\n emit(): unknown {\n return undefined;\n }\n };\n for (const mixin of mixins) {\n const layer: object = mixin(empty).prototype;\n for (const key of Object.getOwnPropertyNames(layer)) {\n if (key === \"constructor\") continue;\n if (definedInClassChain(target.prototype, key)) continue;\n const descriptor = Object.getOwnPropertyDescriptor(layer, key);\n if (descriptor !== undefined) Object.defineProperty(target.prototype, key, descriptor);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Descriptor } from "@gtkx/native";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a raw value returned from native code into its JavaScript form,
|
|
4
|
+
* wrapping object, struct, boxed, and fundamental handles and recursively
|
|
5
|
+
* converting arrays and hash tables according to the descriptor.
|
|
6
|
+
*
|
|
7
|
+
* @param descriptor Describes the native type of the value.
|
|
8
|
+
* @param value The raw native value to convert.
|
|
9
|
+
*/
|
|
10
|
+
export declare function fromNative(descriptor: Descriptor, value: unknown): unknown;
|
|
11
|
+
export declare function toNative(descriptor: Descriptor, value: unknown): unknown;
|
|
12
|
+
//# sourceMappingURL=native-value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native-value.d.ts","sourceRoot":"","sources":["../src/native-value.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAA0B,MAAM,cAAc,CAAC;AAUvE;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAiC1E;AAOD,wBAAgB,QAAQ,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAmBxE"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { getWrapperClass, tryGetHandle, wrapHandle } from "./registry.js";
|
|
2
|
+
import { resolveDescriptorType } from "./type.js";
|
|
3
|
+
const collectionFromNative = (descriptor, value) => {
|
|
4
|
+
if (value === null)
|
|
5
|
+
return null;
|
|
6
|
+
return value.map((item) => fromNative(descriptor.itemDescriptor, item));
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Converts a raw value returned from native code into its JavaScript form,
|
|
10
|
+
* wrapping object, struct, boxed, and fundamental handles and recursively
|
|
11
|
+
* converting arrays and hash tables according to the descriptor.
|
|
12
|
+
*
|
|
13
|
+
* @param descriptor Describes the native type of the value.
|
|
14
|
+
* @param value The raw native value to convert.
|
|
15
|
+
*/
|
|
16
|
+
export function fromNative(descriptor, value) {
|
|
17
|
+
switch (descriptor.kind) {
|
|
18
|
+
case "object":
|
|
19
|
+
return wrapHandle(value, undefined);
|
|
20
|
+
case "struct":
|
|
21
|
+
return wrapHandle(value, descriptor.wrapperClass);
|
|
22
|
+
case "boxed":
|
|
23
|
+
return value == null
|
|
24
|
+
? null
|
|
25
|
+
: wrapHandle(value, getWrapperClass(resolveDescriptorType(descriptor)));
|
|
26
|
+
case "fundamental":
|
|
27
|
+
return value == null
|
|
28
|
+
? null
|
|
29
|
+
: wrapHandle(value, descriptor.wrapperClass ??
|
|
30
|
+
getWrapperClass(resolveDescriptorType(descriptor)));
|
|
31
|
+
case "array":
|
|
32
|
+
return collectionFromNative(descriptor, value);
|
|
33
|
+
case "hashtable": {
|
|
34
|
+
if (value === null)
|
|
35
|
+
return null;
|
|
36
|
+
const entries = value;
|
|
37
|
+
return new Map(entries.map(([key, val]) => [
|
|
38
|
+
fromNative(descriptor.keyDescriptor, key),
|
|
39
|
+
fromNative(descriptor.valueDescriptor, val),
|
|
40
|
+
]));
|
|
41
|
+
}
|
|
42
|
+
default:
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const collectionToNative = (descriptor, value) => {
|
|
47
|
+
if (value == null)
|
|
48
|
+
return null;
|
|
49
|
+
return value.map((item) => toNative(descriptor.itemDescriptor, item));
|
|
50
|
+
};
|
|
51
|
+
export function toNative(descriptor, value) {
|
|
52
|
+
switch (descriptor.kind) {
|
|
53
|
+
case "object":
|
|
54
|
+
case "struct":
|
|
55
|
+
case "boxed":
|
|
56
|
+
case "fundamental":
|
|
57
|
+
return tryGetHandle(value) ?? null;
|
|
58
|
+
case "array":
|
|
59
|
+
return collectionToNative(descriptor, value);
|
|
60
|
+
case "hashtable": {
|
|
61
|
+
if (value == null)
|
|
62
|
+
return null;
|
|
63
|
+
return [...value].map(([key, val]) => [
|
|
64
|
+
toNative(descriptor.keyDescriptor, key),
|
|
65
|
+
toNative(descriptor.valueDescriptor, val),
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
default:
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=native-value.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native-value.js","sourceRoot":"","sources":["../src/native-value.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC1E,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAElD,MAAM,oBAAoB,GAAG,CAAC,UAA2B,EAAE,KAAc,EAAW,EAAE;IAClF,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAChC,OAAQ,KAAmB,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,cAAc,EAAE,IAAI,CAAC,CAAC,CAAC;AAC3F,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAC,UAAsB,EAAE,KAAc;IAC7D,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;QACtB,KAAK,QAAQ;YACT,OAAO,UAAU,CAAC,KAAsC,EAAE,SAAS,CAAC,CAAC;QACzE,KAAK,QAAQ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Q,CAAC,UAAsB,EAAE,KAAc;IAC3D,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;QACtB,KAAK,QAAQ,CAAC;QACd,KAAK,QAAQ,CAAC;QACd,KAAK,OAAO,CAAC;QACb,KAAK,aAAa;YACd,OAAO,YAAY,CAAC,KAAkC,CAAC,IAAI,IAAI,CAAC;QACpE,KAAK,OAAO;YACR,OAAO,kBAAkB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACjD,KAAK,WAAW,EAAE,CAAC;YACf,IAAI,KAAK,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAC;YAC/B,OAAO,CAAC,GAAI,KAA+B,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,EAAsB,EAAE,CAAC;gBACjF,QAAQ,CAAC,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC;gBACvC,QAAQ,CAAC,UAAU,CAAC,eAAe,EAAE,GAAG,CAAC;aAC5C,CAAC,CAAC;QACP,CAAC;QACD;YACI,OAAO,KAAK,CAAC;IACrB,CAAC;AACL,CAAC","sourcesContent":["import type { Descriptor, ExternalObject, Handle } from \"@gtkx/native\";\nimport type { ArrayDescriptor, FundamentalDescriptor, StructDescriptor } from \"./descriptors.js\";\nimport { getWrapperClass, tryGetHandle, wrapHandle } from \"./registry.js\";\nimport { resolveDescriptorType } from \"./type.js\";\n\nconst collectionFromNative = (descriptor: ArrayDescriptor, value: unknown): unknown => {\n if (value === null) return null;\n return (value as unknown[]).map((item) => fromNative(descriptor.itemDescriptor, item));\n};\n\n/**\n * Converts a raw value returned from native code into its JavaScript form,\n * wrapping object, struct, boxed, and fundamental handles and recursively\n * converting arrays and hash tables according to the descriptor.\n *\n * @param descriptor Describes the native type of the value.\n * @param value The raw native value to convert.\n */\nexport function fromNative(descriptor: Descriptor, value: unknown): unknown {\n switch (descriptor.kind) {\n case \"object\":\n return wrapHandle(value as ExternalObject<Handle> | null, undefined);\n case \"struct\":\n return wrapHandle(value as ExternalObject<Handle> | null, (descriptor as StructDescriptor).wrapperClass);\n case \"boxed\":\n return value == null\n ? null\n : wrapHandle(value as ExternalObject<Handle>, getWrapperClass(resolveDescriptorType(descriptor)));\n case \"fundamental\":\n return value == null\n ? null\n : wrapHandle(\n value as ExternalObject<Handle>,\n (descriptor as FundamentalDescriptor).wrapperClass ??\n getWrapperClass(resolveDescriptorType(descriptor)),\n );\n case \"array\":\n return collectionFromNative(descriptor, value);\n case \"hashtable\": {\n if (value === null) return null;\n const entries = value as [unknown, unknown][];\n return new Map(\n entries.map(([key, val]): [unknown, unknown] => [\n fromNative(descriptor.keyDescriptor, key),\n fromNative(descriptor.valueDescriptor, val),\n ]),\n );\n }\n default:\n return value;\n }\n}\n\nconst collectionToNative = (descriptor: ArrayDescriptor, value: unknown): unknown => {\n if (value == null) return null;\n return (value as unknown[]).map((item) => toNative(descriptor.itemDescriptor, item));\n};\n\nexport function toNative(descriptor: Descriptor, value: unknown): unknown {\n switch (descriptor.kind) {\n case \"object\":\n case \"struct\":\n case \"boxed\":\n case \"fundamental\":\n return tryGetHandle(value as object | null | undefined) ?? null;\n case \"array\":\n return collectionToNative(descriptor, value);\n case \"hashtable\": {\n if (value == null) return null;\n return [...(value as Map<unknown, unknown>)].map(([key, val]): [unknown, unknown] => [\n toNative(descriptor.keyDescriptor, key),\n toNative(descriptor.valueDescriptor, val),\n ]);\n }\n default:\n return value;\n }\n}\n"]}
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Descriptor, ExternalObject, Handle } from "@gtkx/native";
|
|
2
|
+
/**
|
|
3
|
+
* Constructs a new GObject of the given type, setting the supplied construct
|
|
4
|
+
* properties. Entries whose value is `undefined` or not a `[descriptor, value]`
|
|
5
|
+
* pair are skipped.
|
|
6
|
+
*
|
|
7
|
+
* @param gtype The GType of the object to construct.
|
|
8
|
+
* @param props Property names mapped to `[descriptor, value]` pairs.
|
|
9
|
+
* @returns The handle of the newly created object.
|
|
10
|
+
*/
|
|
11
|
+
export declare function newObjectWithProperties(gtype: bigint, props: Record<string, unknown>): ExternalObject<Handle>;
|
|
12
|
+
/**
|
|
13
|
+
* Reads a GObject property and converts it to its JavaScript value using the
|
|
14
|
+
* descriptor.
|
|
15
|
+
*
|
|
16
|
+
* @param obj The object to read from.
|
|
17
|
+
* @param propertyName The property name.
|
|
18
|
+
* @param descriptor Describes the property's type.
|
|
19
|
+
*/
|
|
20
|
+
export declare function getObjectProperty(obj: object, propertyName: string, descriptor: Descriptor): unknown;
|
|
21
|
+
/**
|
|
22
|
+
* Writes a JavaScript value to a GObject property, converting it to native form
|
|
23
|
+
* using the descriptor.
|
|
24
|
+
*
|
|
25
|
+
* @param obj The object to write to.
|
|
26
|
+
* @param propertyName The property name.
|
|
27
|
+
* @param descriptor Describes the property's type.
|
|
28
|
+
* @param jsValue The value to set.
|
|
29
|
+
*/
|
|
30
|
+
export declare function setObjectProperty(obj: object, propertyName: string, descriptor: Descriptor, jsValue: unknown): void;
|
|
31
|
+
//# sourceMappingURL=object.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAgCvE;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,cAAc,CAAC,MAAM,CAAC,CAc7G;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAIpG;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAEnH"}
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { bind } from "./bind.js";
|
|
2
|
+
import { biguint64T, objectT, sizedArrayT, stringT, uint32T, voidT } from "./descriptors.js";
|
|
3
|
+
import { LIB, VALUE_SIZE, VALUE_T } from "./library.js";
|
|
4
|
+
import { getHandle } from "./registry.js";
|
|
5
|
+
import { fromValue, newValueForDescriptor, toValue } from "./value.js";
|
|
6
|
+
const gObjectNewWithProperties = bind(LIB, "g_object_new_with_properties", [
|
|
7
|
+
biguint64T,
|
|
8
|
+
uint32T,
|
|
9
|
+
sizedArrayT(stringT("borrowed"), 1, "borrowed"),
|
|
10
|
+
sizedArrayT(VALUE_T, 1, "borrowed", VALUE_SIZE),
|
|
11
|
+
], objectT("full"));
|
|
12
|
+
const gObjectGetProperty = bind(LIB, "g_object_get_property", [objectT("borrowed"), stringT("borrowed"), VALUE_T], voidT);
|
|
13
|
+
const gObjectSetProperty = bind(LIB, "g_object_set_property", [objectT("borrowed"), stringT("borrowed"), VALUE_T], voidT);
|
|
14
|
+
/**
|
|
15
|
+
* Constructs a new GObject of the given type, setting the supplied construct
|
|
16
|
+
* properties. Entries whose value is `undefined` or not a `[descriptor, value]`
|
|
17
|
+
* pair are skipped.
|
|
18
|
+
*
|
|
19
|
+
* @param gtype The GType of the object to construct.
|
|
20
|
+
* @param props Property names mapped to `[descriptor, value]` pairs.
|
|
21
|
+
* @returns The handle of the newly created object.
|
|
22
|
+
*/
|
|
23
|
+
export function newObjectWithProperties(gtype, props) {
|
|
24
|
+
const names = [];
|
|
25
|
+
const values = [];
|
|
26
|
+
for (const name in props) {
|
|
27
|
+
const entry = props[name];
|
|
28
|
+
if (!Array.isArray(entry))
|
|
29
|
+
continue;
|
|
30
|
+
const [descriptor, value] = entry;
|
|
31
|
+
if (value === undefined)
|
|
32
|
+
continue;
|
|
33
|
+
names.push(name);
|
|
34
|
+
values.push(toValue(descriptor, value));
|
|
35
|
+
}
|
|
36
|
+
return gObjectNewWithProperties(gtype, names.length, names, values);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Reads a GObject property and converts it to its JavaScript value using the
|
|
40
|
+
* descriptor.
|
|
41
|
+
*
|
|
42
|
+
* @param obj The object to read from.
|
|
43
|
+
* @param propertyName The property name.
|
|
44
|
+
* @param descriptor Describes the property's type.
|
|
45
|
+
*/
|
|
46
|
+
export function getObjectProperty(obj, propertyName, descriptor) {
|
|
47
|
+
const value = newValueForDescriptor(descriptor);
|
|
48
|
+
gObjectGetProperty(getHandle(obj), propertyName, value);
|
|
49
|
+
return fromValue(value);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Writes a JavaScript value to a GObject property, converting it to native form
|
|
53
|
+
* using the descriptor.
|
|
54
|
+
*
|
|
55
|
+
* @param obj The object to write to.
|
|
56
|
+
* @param propertyName The property name.
|
|
57
|
+
* @param descriptor Describes the property's type.
|
|
58
|
+
* @param jsValue The value to set.
|
|
59
|
+
*/
|
|
60
|
+
export function setObjectProperty(obj, propertyName, descriptor, jsValue) {
|
|
61
|
+
gObjectSetProperty(getHandle(obj), propertyName, toValue(descriptor, jsValue));
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,qBAAqB,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AE,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAA2B,CAAC;AAClG,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB;IACvF,MAAM,KAAK,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAC;IAChD,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;IACxD,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB,EAAE,OAAgB;IACzG,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;AACnF,CAAC","sourcesContent":["import type { Descriptor, ExternalObject, Handle } from \"@gtkx/native\";\nimport { bind } from \"./bind.js\";\nimport { biguint64T, objectT, sizedArrayT, stringT, uint32T, voidT } from \"./descriptors.js\";\nimport { LIB, VALUE_SIZE, VALUE_T } from \"./library.js\";\nimport { getHandle } from \"./registry.js\";\nimport { fromValue, newValueForDescriptor, toValue } from \"./value.js\";\n\nconst gObjectNewWithProperties = bind(\n LIB,\n \"g_object_new_with_properties\",\n [\n biguint64T,\n uint32T,\n sizedArrayT(stringT(\"borrowed\"), 1, \"borrowed\"),\n sizedArrayT(VALUE_T, 1, \"borrowed\", VALUE_SIZE),\n ],\n objectT(\"full\"),\n);\n\nconst gObjectGetProperty = bind(\n LIB,\n \"g_object_get_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\nconst gObjectSetProperty = bind(\n LIB,\n \"g_object_set_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\n\n/**\n * Constructs a new GObject of the given type, setting the supplied construct\n * properties. Entries whose value is `undefined` or not a `[descriptor, value]`\n * pair are skipped.\n *\n * @param gtype The GType of the object to construct.\n * @param props Property names mapped to `[descriptor, value]` pairs.\n * @returns The handle of the newly created object.\n */\nexport function newObjectWithProperties(gtype: bigint, props: Record<string, unknown>): ExternalObject<Handle> {\n const names: string[] = [];\n const values: ExternalObject<Handle>[] = [];\n\n for (const name in props) {\n const entry = props[name];\n if (!Array.isArray(entry)) continue;\n const [descriptor, value] = entry;\n if (value === undefined) continue;\n names.push(name);\n values.push(toValue(descriptor, value));\n }\n\n return gObjectNewWithProperties(gtype, names.length, names, values) as ExternalObject<Handle>;\n}\n\n/**\n * Reads a GObject property and converts it to its JavaScript value using the\n * descriptor.\n *\n * @param obj The object to read from.\n * @param propertyName The property name.\n * @param descriptor Describes the property's type.\n */\nexport function getObjectProperty(obj: object, propertyName: string, descriptor: Descriptor): unknown {\n const value = newValueForDescriptor(descriptor);\n gObjectGetProperty(getHandle(obj), propertyName, value);\n return fromValue(value);\n}\n\n/**\n * Writes a JavaScript value to a GObject property, converting it to native form\n * using the descriptor.\n *\n * @param obj The object to write to.\n * @param propertyName The property name.\n * @param descriptor Describes the property's type.\n * @param jsValue The value to set.\n */\nexport function setObjectProperty(obj: object, propertyName: string, descriptor: Descriptor, jsValue: unknown): void {\n gObjectSetProperty(getHandle(obj), propertyName, toValue(descriptor, jsValue));\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a GIO-style asynchronous function that takes a completion callback into a
|
|
3
|
+
* promise, invoking the finish function to extract the result. Outside production,
|
|
4
|
+
* the call site's stack is captured and attached as the rejection error's cause.
|
|
5
|
+
*
|
|
6
|
+
* @param asyncFn The async function, called with the leading arguments, the cancellable, and a completion callback.
|
|
7
|
+
* @param finish Extracts the result from the async result passed to the completion callback.
|
|
8
|
+
* @param cancellable A cancellable object, or null/undefined for none.
|
|
9
|
+
* @param leading Arguments passed to `asyncFn` before the cancellable and callback.
|
|
10
|
+
* @returns A promise resolving to the finished result, or rejecting if `finish` throws.
|
|
11
|
+
*/
|
|
12
|
+
export declare const promisify: <R extends object, T>(asyncFn: (...args: unknown[]) => void, finish: (result: R) => T, cancellable: object | null | undefined, ...leading: unknown[]) => Promise<T>;
|
|
13
|
+
//# sourceMappingURL=promisify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"promisify.d.ts","sourceRoot":"","sources":["../src/promisify.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,EAAE,CAAC,WAChC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,UAC7B,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,eACX,MAAM,GAAG,IAAI,GAAG,SAAS,cAC1B,OAAO,EAAE,KACtB,OAAO,CAAC,CAAC,CAeN,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { tryGetHandle } from "./registry.js";
|
|
2
|
+
const attachCreationStack = (error, creationStack) => {
|
|
3
|
+
if (creationStack === undefined || !(error instanceof Error))
|
|
4
|
+
return;
|
|
5
|
+
if (error.cause !== undefined || !Object.isExtensible(error))
|
|
6
|
+
return;
|
|
7
|
+
error.cause = creationStack;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Wraps a GIO-style asynchronous function that takes a completion callback into a
|
|
11
|
+
* promise, invoking the finish function to extract the result. Outside production,
|
|
12
|
+
* the call site's stack is captured and attached as the rejection error's cause.
|
|
13
|
+
*
|
|
14
|
+
* @param asyncFn The async function, called with the leading arguments, the cancellable, and a completion callback.
|
|
15
|
+
* @param finish Extracts the result from the async result passed to the completion callback.
|
|
16
|
+
* @param cancellable A cancellable object, or null/undefined for none.
|
|
17
|
+
* @param leading Arguments passed to `asyncFn` before the cancellable and callback.
|
|
18
|
+
* @returns A promise resolving to the finished result, or rejecting if `finish` throws.
|
|
19
|
+
*/
|
|
20
|
+
export const promisify = (asyncFn, finish, cancellable, ...leading) => new Promise((resolve, reject) => {
|
|
21
|
+
let creationStack;
|
|
22
|
+
if (process.env.NODE_ENV !== "production") {
|
|
23
|
+
creationStack = new Error("gtkx async operation started here");
|
|
24
|
+
Error.captureStackTrace(creationStack, promisify);
|
|
25
|
+
}
|
|
26
|
+
asyncFn(...leading, tryGetHandle(cancellable), (_source, asyncResult) => {
|
|
27
|
+
try {
|
|
28
|
+
resolve(finish(asyncResult));
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
attachCreationStack(error, creationStack);
|
|
32
|
+
reject(error);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
//# sourceMappingURL=promisify.js.map
|