@gtkx/runtime 1.0.0-rc.4 → 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 +5 -5
- 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 +2 -1
- package/dist/arg.d.ts.map +1 -1
- package/dist/arg.js +2 -1
- 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 +118 -20
- 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 +74 -3
- package/dist/descriptors.d.ts.map +1 -1
- package/dist/descriptors.js +44 -0
- package/dist/descriptors.js.map +1 -1
- package/dist/error.d.ts +1 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js.map +1 -1
- package/dist/fn.d.ts +19 -2
- package/dist/fn.d.ts.map +1 -1
- package/dist/fn.js +64 -27
- package/dist/fn.js.map +1 -1
- package/dist/index.d.ts +14 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -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 +43 -11
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +62 -19
- package/dist/lifecycle.js.map +1 -1
- 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 +3 -2
- package/dist/mixin.d.ts.map +1 -1
- package/dist/mixin.js +13 -1
- package/dist/mixin.js.map +1 -1
- package/dist/native-value.d.ts.map +1 -1
- package/dist/native-value.js +4 -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.map +1 -1
- package/dist/promisify.js +3 -2
- package/dist/promisify.js.map +1 -1
- package/dist/properties.d.ts +38 -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 +117 -12
- package/dist/register-class.d.ts.map +1 -1
- package/dist/register-class.js +108 -76
- package/dist/register-class.js.map +1 -1
- package/dist/registry.d.ts +69 -19
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +70 -26
- package/dist/registry.js.map +1 -1
- package/dist/signal.d.ts +9 -2
- package/dist/signal.d.ts.map +1 -1
- package/dist/signal.js +63 -7
- package/dist/signal.js.map +1 -1
- package/dist/type.d.ts +2 -1
- 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 +8 -1
- package/dist/value.d.ts.map +1 -1
- package/dist/value.js +42 -13
- 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 +2 -1
- package/src/bind.ts +11 -0
- package/src/callback.ts +162 -22
- package/src/closure.ts +136 -0
- package/src/descriptors.ts +85 -24
- package/src/error.ts +1 -0
- package/src/fn.ts +92 -31
- package/src/index.ts +21 -6
- package/src/internal.ts +1 -0
- package/src/library.ts +2 -1
- package/src/lifecycle.ts +90 -23
- package/src/listeners.ts +3 -1
- package/src/mixin.ts +15 -2
- package/src/native-value.ts +5 -3
- package/src/object.ts +128 -34
- package/src/param-spec.ts +235 -0
- package/src/promisify.ts +4 -2
- package/src/properties.ts +458 -45
- package/src/register-class.ts +326 -109
- package/src/registry.ts +138 -40
- package/src/signal.ts +84 -14
- package/src/type.ts +3 -1
- package/src/value.ts +55 -10
- package/src/vfunc-call.ts +243 -0
- package/src/vfunc-seeds.ts +36 -0
- package/src/vfunc.ts +31 -0
package/dist/lifecycle.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { keepAlive, quit as nativeQuit } from "@gtkx/native";
|
|
2
|
-
import {
|
|
2
|
+
import { isDerivedApplication, shutDownThroughRun } from "./application-class.js";
|
|
3
3
|
const shutdownCallbacks = [];
|
|
4
|
+
const startedApplications = new WeakSet();
|
|
5
|
+
const shutDownApplications = new WeakSet();
|
|
4
6
|
/**
|
|
5
7
|
* Runs every registered exit callback and shuts down the native runtime. Safe to
|
|
6
8
|
* call more than once; only the first call takes effect.
|
|
@@ -28,44 +30,85 @@ function createQuit() {
|
|
|
28
30
|
const onExit = (callback) => {
|
|
29
31
|
shutdownCallbacks.push(callback);
|
|
30
32
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* while it is active and releasing it on shutdown.
|
|
34
|
-
*
|
|
35
|
-
* @param application The application to register and activate.
|
|
36
|
-
*/
|
|
37
|
-
const runApplication = (application) => {
|
|
33
|
+
const startApplication = (application, argv) => {
|
|
34
|
+
startedApplications.add(application);
|
|
38
35
|
application.on("activate", () => {
|
|
39
36
|
keepAlive(true);
|
|
40
37
|
});
|
|
41
38
|
application.on("shutdown", () => {
|
|
42
39
|
keepAlive(false);
|
|
43
40
|
});
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
return application.runLocalCommandLine(argv)[2];
|
|
42
|
+
};
|
|
43
|
+
const restartApplication = (application) => {
|
|
44
|
+
shutDownApplications.delete(application);
|
|
45
|
+
if (!application.register(null)) {
|
|
46
|
+
return 1;
|
|
46
47
|
}
|
|
47
48
|
application.activate();
|
|
49
|
+
return 0;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Hands `argv` to GLib's own command line handling, which parses the application's options, prints
|
|
53
|
+
* `--help`, runs `handle-local-options`, registers the application, and either activates it or
|
|
54
|
+
* forwards the command line to the process that already owns the application ID. The runtime is
|
|
55
|
+
* held alive while the application is active and released on shutdown.
|
|
56
|
+
*
|
|
57
|
+
* `g_application_run()` is not what starts the application, because it would drive its own main loop
|
|
58
|
+
* and freeze Node; only the local command line handling it delegates to runs here. {@link
|
|
59
|
+
* quitApplication} calls it once no window holds the application open, to reach the teardown only it
|
|
60
|
+
* performs.
|
|
61
|
+
*
|
|
62
|
+
* GLib parses a given application's command line at most once, so starting an application that has
|
|
63
|
+
* already run registers and activates it instead of reading `argv` again.
|
|
64
|
+
*
|
|
65
|
+
* When another process already owns the application ID, this one registers as a remote instance: no
|
|
66
|
+
* user interface may be built here, because a remote application has no `GtkApplicationImpl` and
|
|
67
|
+
* attaching a window to it crashes.
|
|
68
|
+
*
|
|
69
|
+
* @param application The application to start.
|
|
70
|
+
* @param argv The command line, whose first entry names the program as `--help` should print it.
|
|
71
|
+
* @returns Whether this process may build a user interface, and the status to exit with.
|
|
72
|
+
*/
|
|
73
|
+
const runApplication = (application, argv) => {
|
|
74
|
+
if (!isDerivedApplication(application)) {
|
|
75
|
+
throw new Error("runApplication: this application was not built by GTKX, so its command line cannot be " +
|
|
76
|
+
"parsed and it cannot be shut down safely; render <GtkApplication> or <AdwApplication>, " +
|
|
77
|
+
"or construct it with createApplication from @gtkx/runtime");
|
|
78
|
+
}
|
|
79
|
+
const exitStatus = startedApplications.has(application)
|
|
80
|
+
? restartApplication(application)
|
|
81
|
+
: startApplication(application, argv);
|
|
82
|
+
const isPrimary = application.getIsRegistered() && application.getIsRemote?.() !== true;
|
|
83
|
+
if (isPrimary) {
|
|
84
|
+
keepAlive(true);
|
|
85
|
+
}
|
|
86
|
+
return { isPrimary, exitStatus };
|
|
48
87
|
};
|
|
49
88
|
/**
|
|
50
|
-
* Detaches every window from the application and runs
|
|
51
|
-
*
|
|
52
|
-
*
|
|
89
|
+
* Detaches every window from the application and runs GLib's own shutdown, which emits `shutdown`,
|
|
90
|
+
* destroys the application implementation and releases the D-Bus registration. Does nothing for an
|
|
91
|
+
* application that is not registered, so a repeated call is a no-op.
|
|
92
|
+
*
|
|
93
|
+
* GLib's own shutdown is reachable once per application: reaching it marks the application as
|
|
94
|
+
* quitting, which GLib never undoes. An application that has already quit falls back to emitting
|
|
95
|
+
* `shutdown`, which releases the runtime and leaves the registration for GLib to drop at finalize.
|
|
53
96
|
*
|
|
54
97
|
* @param application The application to shut down.
|
|
55
98
|
*/
|
|
56
99
|
const quitApplication = (application) => {
|
|
57
|
-
if (!application.getIsRegistered()) {
|
|
100
|
+
if (!application.getIsRegistered() || shutDownApplications.has(application)) {
|
|
58
101
|
return;
|
|
59
102
|
}
|
|
60
103
|
const windows = application.getWindows?.() ?? [];
|
|
61
104
|
for (const window of windows) {
|
|
62
105
|
application.removeWindow?.(window);
|
|
63
106
|
}
|
|
64
|
-
application
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
107
|
+
shutDownThroughRun(application);
|
|
108
|
+
if (application.getIsRegistered()) {
|
|
109
|
+
shutDownApplications.add(application);
|
|
110
|
+
application.emit("shutdown");
|
|
111
|
+
}
|
|
69
112
|
};
|
|
70
113
|
export { onExit, quit, runApplication, quitApplication };
|
|
71
114
|
//# sourceMappingURL=lifecycle.js.map
|
package/dist/lifecycle.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,IAAI,UAAU,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAoC,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAuCpH,MAAM,iBAAiB,GAAmB,EAAE,CAAC;AAC7C,MAAM,mBAAmB,GAAoB,IAAI,OAAO,EAAE,CAAC;AAC3D,MAAM,oBAAoB,GAAoB,IAAI,OAAO,EAAE,CAAC;AAC5D;;;GAGG;AACH,MAAM,IAAI,GAAe,UAAU,EAAE,CAAC;AAEtC,SAAS,UAAU;IACf,IAAI,OAAO,GAAG,KAAK,CAAC;IAEpB,OAAO,GAAG,EAAE;QACR,IAAI,OAAO,EAAE,CAAC;YACV,OAAO;QACX,CAAC;QAED,OAAO,GAAG,IAAI,CAAC;QAEf,KAAK,MAAM,QAAQ,IAAI,iBAAiB,EAAE,CAAC;YACvC,QAAQ,EAAE,CAAC;QACf,CAAC;QAED,UAAU,EAAE,CAAC;IACjB,CAAC,CAAC;AACN,CAAC;AAED;;;;;GAKG;AACH,MAAM,MAAM,GAAG,CAAC,QAAoB,EAAQ,EAAE;IAC1C,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACrC,CAAC,CAAC;AAEF,MAAM,gBAAgB,GAAG,CAAC,WAA0D,EAAE,IAAc,EAAU,EAAE;IAC5G,m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AAC;IACpB,CAAC;IAED,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AACrC,CAAC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,eAAe,GAAG,CAAC,WAA4B,EAAQ,EAAE;IAC3D,IAAI,CAAC,WAAW,CAAC,eAAe,EAAE,IAAI,oBAAoB,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,CAAC;QAC1E,OAAO;IACX,CAAC;IAED,MAAM,OAAO,GAAG,WAAW,CAAC,UAAU,EAAE,EAAE,IAAI,EAAE,CAAC;IAEjD,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC3B,WAAW,CAAC,YAAY,EAAE,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAED,kBAAkB,CAAC,WAAW,CAAC,CAAC;IAEhC,IAAI,WAAW,CAAC,eAAe,EAAE,EAAE,CAAC;QAChC,oBAAoB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;QACtC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACjC,CAAC;AACL,CAAC,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,eAAe,EAAmD,CAAC","sourcesContent":["import { keepAlive, quit as nativeQuit } from \"@gtkx/native\";\nimport { isDerivedApplication, type LocalCommandLineApplication, shutDownThroughRun } from \"./application-class.js\";\n\n/**\n * The GIO and GTK application surface {@link runApplication} and {@link quitApplication} drive, so any\n * application subclass satisfies it structurally; the window members only `Gtk.Application` defines are\n * optional.\n */\ntype ApplicationLike = {\n /** Returns whether the application has already been registered. */\n getIsRegistered(): boolean;\n /** Returns whether another process owns the application ID, making this one a remote instance. */\n getIsRemote?(): boolean;\n /** Registers the application with the session, returning whether it succeeded. */\n register(cancellable: null): boolean;\n /** Emits `activate`, bringing up the application's initial user interface. */\n activate(): void;\n /**\n * Runs GLib's own `g_application_run`, whose tail emits `shutdown`, destroys the application\n * implementation and clears the registration.\n */\n run(argv: string[]): number;\n /** Returns the windows currently attached to the application. */\n getWindows?(): object[];\n /** Detaches a window, so it no longer holds the application open. */\n removeWindow?(window: object): void;\n /** Connects a handler to the application's `activate` or `shutdown` signal. */\n on(signal: \"activate\" | \"shutdown\", handler: () => void): unknown;\n /** Emits one of the application's own signals. */\n emit(signal: \"shutdown\"): unknown;\n};\n\n/** What {@link runApplication} reports about the process it just started. */\ntype RunApplicationResult = {\n /** Whether this process owns the application ID and may build a user interface. */\n isPrimary: boolean;\n /** The status GLib determined for the command line, which the process should exit with. */\n exitStatus: number;\n};\n\nconst shutdownCallbacks: (() => void)[] = [];\nconst startedApplications: WeakSet<object> = new WeakSet();\nconst shutDownApplications: WeakSet<object> = new WeakSet();\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 */\nconst quit: () => void = createQuit();\n\nfunction createQuit(): () => void {\n let hasQuit = false;\n\n return () => {\n if (hasQuit) {\n return;\n }\n\n hasQuit = true;\n\n for (const callback of shutdownCallbacks) {\n callback();\n }\n\n nativeQuit();\n };\n}\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 */\nconst onExit = (callback: () => void): void => {\n shutdownCallbacks.push(callback);\n};\n\nconst startApplication = (application: ApplicationLike & LocalCommandLineApplication, argv: string[]): number => {\n startedApplications.add(application);\n\n application.on(\"activate\", () => {\n keepAlive(true);\n });\n\n application.on(\"shutdown\", () => {\n keepAlive(false);\n });\n\n return application.runLocalCommandLine(argv)[2];\n};\n\nconst restartApplication = (application: ApplicationLike): number => {\n shutDownApplications.delete(application);\n\n if (!application.register(null)) {\n return 1;\n }\n\n application.activate();\n\n return 0;\n};\n\n/**\n * Hands `argv` to GLib's own command line handling, which parses the application's options, prints\n * `--help`, runs `handle-local-options`, registers the application, and either activates it or\n * forwards the command line to the process that already owns the application ID. The runtime is\n * held alive while the application is active and released on shutdown.\n *\n * `g_application_run()` is not what starts the application, because it would drive its own main loop\n * and freeze Node; only the local command line handling it delegates to runs here. {@link\n * quitApplication} calls it once no window holds the application open, to reach the teardown only it\n * performs.\n *\n * GLib parses a given application's command line at most once, so starting an application that has\n * already run registers and activates it instead of reading `argv` again.\n *\n * When another process already owns the application ID, this one registers as a remote instance: no\n * user interface may be built here, because a remote application has no `GtkApplicationImpl` and\n * attaching a window to it crashes.\n *\n * @param application The application to start.\n * @param argv The command line, whose first entry names the program as `--help` should print it.\n * @returns Whether this process may build a user interface, and the status to exit with.\n */\nconst runApplication = (application: ApplicationLike, argv: string[]): RunApplicationResult => {\n if (!isDerivedApplication(application)) {\n throw new Error(\n \"runApplication: this application was not built by GTKX, so its command line cannot be \" +\n \"parsed and it cannot be shut down safely; render <GtkApplication> or <AdwApplication>, \" +\n \"or construct it with createApplication from @gtkx/runtime\",\n );\n }\n\n const exitStatus = startedApplications.has(application)\n ? restartApplication(application)\n : startApplication(application, argv);\n\n const isPrimary = application.getIsRegistered() && application.getIsRemote?.() !== true;\n\n if (isPrimary) {\n keepAlive(true);\n }\n\n return { isPrimary, exitStatus };\n};\n\n/**\n * Detaches every window from the application and runs GLib's own shutdown, which emits `shutdown`,\n * destroys the application implementation and releases the D-Bus registration. Does nothing for an\n * application that is not registered, so a repeated call is a no-op.\n *\n * GLib's own shutdown is reachable once per application: reaching it marks the application as\n * quitting, which GLib never undoes. An application that has already quit falls back to emitting\n * `shutdown`, which releases the runtime and leaves the registration for GLib to drop at finalize.\n *\n * @param application The application to shut down.\n */\nconst quitApplication = (application: ApplicationLike): void => {\n if (!application.getIsRegistered() || shutDownApplications.has(application)) {\n return;\n }\n\n const windows = application.getWindows?.() ?? [];\n\n for (const window of windows) {\n application.removeWindow?.(window);\n }\n\n shutDownThroughRun(application);\n\n if (application.getIsRegistered()) {\n shutDownApplications.add(application);\n application.emit(\"shutdown\");\n }\n};\n\nexport { onExit, quit, runApplication, quitApplication, type ApplicationLike, type RunApplicationResult };\n"]}
|
package/dist/listeners.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listeners.d.ts","sourceRoot":"","sources":["../src/listeners.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,aAAa,
|
|
1
|
+
{"version":3,"file":"listeners.d.ts","sourceRoot":"","sources":["../src/listeners.ts"],"names":[],"mappings":"AAAA,OAAO,EAA4B,KAAK,aAAa,EAAqB,MAAM,aAAa,CAAC;AAE9F;;;GAGG;AACH,KAAK,iBAAiB,GAAG;IACrB,iEAAiE;IACjE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC3E,uEAAuE;IACvE,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CACvC,CAAC;AA4CF;;;;;;;;GAQG;AACH,iBAAS,QAAQ,CAAC,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAG9G;AAED;;;;;;;;GAQG;AACH,iBAAS,UAAU,CAAC,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAchH;AAED;;;;;;;GAOG;AACH,iBAAS,SAAS,CAAC,QAAQ,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAe5F;AAED,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/listeners.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isSignalHandlerConnected } from "./signal.js";
|
|
1
|
+
import { isSignalHandlerConnected, untrackConnection } from "./signal.js";
|
|
2
2
|
const listenerTable = new WeakMap();
|
|
3
3
|
const findListenerHandlerId = (instance, signal, handler) => listenerTable.get(instance)?.get(signal)?.get(handler);
|
|
4
4
|
const trackListener = (instance, signal, handler, handlerId) => {
|
|
@@ -55,6 +55,7 @@ function onceSignal(instance, signal, handler, isAfter) {
|
|
|
55
55
|
let handlerId = 0;
|
|
56
56
|
const wrapped = (...args) => {
|
|
57
57
|
untrackHandlerId(instance, signal, handlerId);
|
|
58
|
+
untrackConnection(instance, handlerId);
|
|
58
59
|
instance.disconnect(handlerId);
|
|
59
60
|
return handler(...args);
|
|
60
61
|
};
|
|
@@ -76,6 +77,7 @@ function offSignal(instance, signal, handler) {
|
|
|
76
77
|
return;
|
|
77
78
|
}
|
|
78
79
|
untrackHandlerId(instance, signal, handlerId);
|
|
80
|
+
untrackConnection(instance, handlerId);
|
|
79
81
|
if (!isSignalHandlerConnected(instance, handlerId)) {
|
|
80
82
|
return;
|
|
81
83
|
}
|
package/dist/listeners.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listeners.js","sourceRoot":"","sources":["../src/listeners.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAsB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"listeners.js","sourceRoot":"","sources":["../src/listeners.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAsB,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAa9F,MAAM,aAAa,GAA6D,IAAI,OAAO,EAAE,CAAC;AAE9F,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,CA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C,CAAC;AAED,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC","sourcesContent":["import { isSignalHandlerConnected, type SignalHandler, untrackConnection } from \"./signal.js\";\n\n/**\n * The connect and disconnect surface {@link onSignal}, {@link onceSignal} and {@link offSignal}\n * drive on an emitter.\n */\ntype SignalConnectable = {\n /** Connects a handler to a signal and returns its handler id. */\n connect(signal: string, handler: SignalHandler, isAfter?: boolean): number;\n /** Disconnects the handler previously connected under the given id. */\n disconnect(handlerId: number): void;\n};\n\nconst listenerTable: WeakMap<object, Map<string, Map<SignalHandler, number>>> = new WeakMap();\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\n if (!bySignal) {\n bySignal = new Map();\n listenerTable.set(instance, bySignal);\n }\n\n let byHandler = bySignal.get(signal);\n\n if (!byHandler) {\n byHandler = new Map();\n bySignal.set(signal, byHandler);\n }\n\n byHandler.set(handler, handlerId);\n};\n\nconst untrackHandlerId = (instance: object, signal: string, handlerId: number): void => {\n const bySignal = listenerTable.get(instance);\n const byHandler = bySignal?.get(signal);\n\n if (byHandler === undefined) {\n return;\n }\n\n for (const [handler, id] of byHandler) {\n if (id === handlerId) {\n byHandler.delete(handler);\n }\n }\n\n if (byHandler.size === 0) {\n bySignal?.delete(signal);\n }\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 isAfter When true, run the handler after the default handler.\n */\nfunction onSignal(instance: SignalConnectable, signal: string, handler: SignalHandler, isAfter?: boolean): void {\n const handlerId = instance.connect(signal, handler, isAfter);\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 isAfter When true, run the handler after the default handler.\n */\nfunction onceSignal(instance: SignalConnectable, signal: string, handler: SignalHandler, isAfter?: boolean): void {\n let handlerId = 0;\n\n const wrapped: SignalHandler = (...args) => {\n untrackHandlerId(instance, signal, handlerId);\n untrackConnection(instance, handlerId);\n instance.disconnect(handlerId);\n\n return handler(...args);\n };\n\n handlerId = instance.connect(signal, wrapped, isAfter);\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 */\nfunction offSignal(instance: SignalConnectable, signal: string, handler: SignalHandler): void {\n const handlerId = findListenerHandlerId(instance, signal, handler);\n\n if (handlerId === undefined) {\n return;\n }\n\n untrackHandlerId(instance, signal, handlerId);\n untrackConnection(instance, handlerId);\n\n if (!isSignalHandlerConnected(instance, handlerId)) {\n return;\n }\n\n instance.disconnect(handlerId);\n}\n\nexport { onSignal, onceSignal, offSignal };\n"]}
|
package/dist/mixin.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AnyClass } from "@gtkx/utils";
|
|
2
2
|
/**
|
|
3
3
|
* The signal plumbing every base class passed to a {@link Mixin} provides, so mixed-in interface
|
|
4
4
|
* members can connect and emit without knowing the concrete class.
|
|
@@ -22,5 +22,6 @@ type Mixin = (base: AnyClass<MixinReceiver>) => AnyClass;
|
|
|
22
22
|
* @param mixins The mixins to apply, in order.
|
|
23
23
|
*/
|
|
24
24
|
declare function installMixins(target: AnyClass, mixins: Mixin[]): void;
|
|
25
|
-
|
|
25
|
+
declare function insertMixinLayer(target: AnyClass, mixin: Mixin, inheritedNames: Set<string>): void;
|
|
26
|
+
export { insertMixinLayer, installMixins, type MixinReceiver, type Mixin };
|
|
26
27
|
//# sourceMappingURL=mixin.d.ts.map
|
package/dist/mixin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mixin.d.ts","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"mixin.d.ts","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAkB,MAAM,aAAa,CAAC;AAE5D;;;GAGG;AACH,KAAK,aAAa,GAAG;IACjB,iEAAiE;IACjE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC7F,0FAA0F;IAC1F,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;CACrD,CAAC;AAEF;;;GAGG;AACH,KAAK,KAAK,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,aAAa,CAAC,KAAK,QAAQ,CAAC;AAsCzD;;;;;;GAMG;AACH,iBAAS,aAAa,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAc9D;AAQD,iBAAS,gBAAgB,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,cAAc,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAK3F;AAED,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,KAAK,aAAa,EAAE,KAAK,KAAK,EAAE,CAAC"}
|
package/dist/mixin.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getParentClass } from "@gtkx/utils";
|
|
1
2
|
function isDefinedInClassChain(prototype, key) {
|
|
2
3
|
let current = prototype;
|
|
3
4
|
while (current !== null && current !== Object.prototype) {
|
|
@@ -45,5 +46,16 @@ function installMixins(target, mixins) {
|
|
|
45
46
|
copyLayerMembers(target, mixin(empty).prototype);
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
|
-
|
|
49
|
+
function dropLayerMembers(layer, names) {
|
|
50
|
+
for (const name of names) {
|
|
51
|
+
Reflect.deleteProperty(layer.prototype, name);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function insertMixinLayer(target, mixin, inheritedNames) {
|
|
55
|
+
const layer = mixin(getParentClass(target));
|
|
56
|
+
dropLayerMembers(layer, inheritedNames);
|
|
57
|
+
Object.setPrototypeOf(target.prototype, layer.prototype);
|
|
58
|
+
Object.setPrototypeOf(target, layer);
|
|
59
|
+
}
|
|
60
|
+
export { insertMixinLayer, installMixins };
|
|
49
61
|
//# sourceMappingURL=mixin.js.map
|
package/dist/mixin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mixin.js","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mixin.js","sourceRoot":"","sources":["../src/mixin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,cAAc,EAAE,MAAM,aAAa,CAAC;AAmB5D,SAAS,qBAAqB,CAAC,SAAiB,EAAE,GAAW;IACzD,IAAI,OAAO,GAAkB,SAAS,CAAC;IAEvC,OAAO,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,MAAM,CAAC,SAAS,EAAE,CAAC;QACtD,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC9C,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC;AAED,SAAS,eAAe,CAAC,MAAgB,EAAE,KAAa,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AC,cAAc,CAAC,MAAM,CAA4B,CAAC,CAAC;IACvE,gBAAgB,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;IACxC,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IACzD,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AACzC,CAAC;AAED,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAkC,CAAC","sourcesContent":["import { type AnyClass, getParentClass } from \"@gtkx/utils\";\n\n/**\n * The signal plumbing every base class passed to a {@link Mixin} provides, so mixed-in interface\n * members can connect and emit without knowing the concrete class.\n */\ntype MixinReceiver = {\n /** Connects a handler to a signal and returns its handler id. */\n connect(signal: string, handler: (...args: unknown[]) => unknown, isAfter?: boolean): number;\n /** Emits a signal with the given arguments and returns whatever the emission produced. */\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 */\ntype Mixin = (base: AnyClass<MixinReceiver>) => AnyClass;\n\nfunction isDefinedInClassChain(prototype: object, key: string): boolean {\n let current: object | null = prototype;\n\n while (current !== null && current !== Object.prototype) {\n if (Object.hasOwn(current, key)) {\n return true;\n }\n\n current = Reflect.getPrototypeOf(current);\n }\n\n return false;\n}\n\nfunction copyLayerMember(target: AnyClass, layer: object, key: string): void {\n if (key === \"constructor\") {\n return;\n }\n\n if (isDefinedInClassChain(target.prototype, key)) {\n return;\n }\n\n const descriptor = Object.getOwnPropertyDescriptor(layer, key);\n\n if (descriptor !== undefined) {\n Object.defineProperty(target.prototype, key, descriptor);\n }\n}\n\nfunction copyLayerMembers(target: AnyClass, layer: object): void {\n for (const key of Object.getOwnPropertyNames(layer)) {\n copyLayerMember(target, layer, key);\n }\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 */\nfunction installMixins(target: AnyClass, mixins: Mixin[]): void {\n const empty: AnyClass<MixinReceiver> = class {\n connect(): number {\n return 0;\n }\n\n emit(): unknown {\n return undefined;\n }\n };\n\n for (const mixin of mixins) {\n copyLayerMembers(target, mixin(empty).prototype);\n }\n}\n\nfunction dropLayerMembers(layer: AnyClass, names: Set<string>): void {\n for (const name of names) {\n Reflect.deleteProperty(layer.prototype, name);\n }\n}\n\nfunction insertMixinLayer(target: AnyClass, mixin: Mixin, inheritedNames: Set<string>): void {\n const layer = mixin(getParentClass(target) as AnyClass<MixinReceiver>);\n dropLayerMembers(layer, inheritedNames);\n Object.setPrototypeOf(target.prototype, layer.prototype);\n Object.setPrototypeOf(target, layer);\n}\n\nexport { insertMixinLayer, installMixins, type MixinReceiver, type Mixin };\n"]}
|
|
@@ -1 +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;AA+DvE;;;;;;;GAOG;AACH,iBAAS,UAAU,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAyBnE;AAiBD;;;;;;;GAOG;AACH,iBAAS,QAAQ,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,
|
|
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;AA+DvE;;;;;;;GAOG;AACH,iBAAS,UAAU,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAyBnE;AAiBD;;;;;;;GAOG;AACH,iBAAS,QAAQ,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAqBjE;AAED,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC"}
|
package/dist/native-value.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getHandle, getWrapperClass, wrapHandle, wrapObject } from "./registry.js";
|
|
2
2
|
import { resolveDescriptorType } from "./type.js";
|
|
3
3
|
const MARSHALLED_KINDS = new Set([
|
|
4
4
|
"object",
|
|
@@ -51,7 +51,7 @@ function fromNative(descriptor, value) {
|
|
|
51
51
|
}
|
|
52
52
|
switch (descriptor.kind) {
|
|
53
53
|
case "object": {
|
|
54
|
-
return
|
|
54
|
+
return wrapObject(value);
|
|
55
55
|
}
|
|
56
56
|
case "struct": {
|
|
57
57
|
return wrapHandle(value, descriptor.wrapperClass);
|
|
@@ -99,7 +99,8 @@ function toNative(descriptor, value) {
|
|
|
99
99
|
case "struct":
|
|
100
100
|
case "boxed":
|
|
101
101
|
case "fundamental": {
|
|
102
|
-
|
|
102
|
+
const instance = value;
|
|
103
|
+
return instance == null ? null : getHandle(instance);
|
|
103
104
|
}
|
|
104
105
|
case "array": {
|
|
105
106
|
return collectionToNative(descriptor, value);
|
package/dist/native-value.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"native-value.js","sourceRoot":"","sources":["../src/native-value.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"native-value.js","sourceRoot":"","sources":["../src/native-value.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACnF,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAKlD,MAAM,gBAAgB,GAA4B,IAAI,GAAG,CAAiB;IACtE,QAAQ;IACR,QAAQ;IACR,OAAO;IACP,aAAa;IACb,OAAO;IACP,WAAW;CACd,CAAC,CAAC;AAEH,SAAS,sBAAsB,CAAC,UAAsB;IA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OAAO,CAAC,GAAI,KAA+B,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,EAAsB,EAAE,CAAC;QACjF,QAAQ,CAAC,UAAU,CAAC,aAAa,EAAE,GAAG,CAAC;QACvC,QAAQ,CAAC,UAAU,CAAC,eAAe,EAAE,GAAG,CAAC;KAC5C,CAAC,CAAC;AACP,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,QAAQ,CAAC,UAAsB,EAAE,KAAc;IACpD,IAAI,CAAC,sBAAsB,CAAC,UAAU,CAAC,EAAE,CAAC;QACtC,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,QAAQ,UAAU,CAAC,IAAI,EAAE,CAAC;QACtB,KAAK,QAAQ,CAAC;QACd,KAAK,QAAQ,CAAC;QACd,KAAK,OAAO,CAAC;QACb,KAAK,aAAa,CAAC,CAAC,CAAC;YACjB,MAAM,QAAQ,GAAG,KAAkC,CAAC;YAEpD,OAAO,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QACzD,CAAC;QACD,KAAK,OAAO,CAAC,CAAC,CAAC;YACX,OAAO,kBAAkB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACjD,CAAC;QACD,KAAK,WAAW,CAAC,CAAC,CAAC;YACf,OAAO,iBAAiB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAChD,CAAC;IACL,CAAC;AACL,CAAC;AAED,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC","sourcesContent":["import type { Descriptor, ExternalObject, Handle } from \"@gtkx/native\";\nimport type { ArrayDescriptor, FundamentalDescriptor, HashTableDescriptor, StructDescriptor } from \"./descriptors.js\";\nimport { getHandle, getWrapperClass, wrapHandle, wrapObject } from \"./registry.js\";\nimport { resolveDescriptorType } from \"./type.js\";\n\ntype MarshalledKind = \"object\" | \"struct\" | \"boxed\" | \"fundamental\" | \"array\" | \"hashtable\";\ntype MarshalledDescriptor = Extract<Descriptor, { kind: MarshalledKind }>;\n\nconst MARSHALLED_KINDS: Set<Descriptor[\"kind\"]> = new Set<MarshalledKind>([\n \"object\",\n \"struct\",\n \"boxed\",\n \"fundamental\",\n \"array\",\n \"hashtable\",\n]);\n\nfunction isMarshalledDescriptor(descriptor: Descriptor): descriptor is MarshalledDescriptor {\n return MARSHALLED_KINDS.has(descriptor.kind);\n}\n\nfunction mapCollection(\n descriptor: ArrayDescriptor,\n value: unknown,\n convert: (itemDescriptor: Descriptor, item: unknown) => unknown,\n): unknown[] {\n return (value as unknown[]).map((item) => convert(descriptor.itemDescriptor, item));\n}\n\nfunction collectionFromNative(descriptor: ArrayDescriptor, value: unknown): unknown {\n return value === null ? null : mapCollection(descriptor, value, fromNative);\n}\n\nfunction boxedFromNative(descriptor: Descriptor, value: unknown): unknown {\n return value == null\n ? null\n : wrapHandle(value as ExternalObject<Handle>, getWrapperClass(resolveDescriptorType(descriptor)));\n}\n\nfunction fundamentalFromNative(descriptor: FundamentalDescriptor, value: unknown): unknown {\n return value == null\n ? null\n : wrapHandle(\n value as ExternalObject<Handle>,\n descriptor.wrapperClass ?? getWrapperClass(resolveDescriptorType(descriptor)),\n );\n}\n\nfunction hashTableFromNative(descriptor: HashTableDescriptor, value: unknown): unknown {\n if (value === null) {\n return null;\n }\n\n const entries = value as [unknown, unknown][];\n\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\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 */\nfunction fromNative(descriptor: Descriptor, value: unknown): unknown {\n if (!isMarshalledDescriptor(descriptor)) {\n return value;\n }\n\n switch (descriptor.kind) {\n case \"object\": {\n return wrapObject(value);\n }\n case \"struct\": {\n return wrapHandle(value as ExternalObject<Handle> | null, (descriptor as StructDescriptor).wrapperClass);\n }\n case \"boxed\": {\n return boxedFromNative(descriptor, value);\n }\n case \"fundamental\": {\n return fundamentalFromNative(descriptor, value);\n }\n case \"array\": {\n return collectionFromNative(descriptor, value);\n }\n case \"hashtable\": {\n return hashTableFromNative(descriptor, value);\n }\n }\n}\n\nfunction collectionToNative(descriptor: ArrayDescriptor, value: unknown): unknown {\n return value == null ? null : mapCollection(descriptor, value, toNative);\n}\n\nfunction hashTableToNative(descriptor: HashTableDescriptor, value: unknown): unknown {\n if (value == null) {\n return null;\n }\n\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\n/**\n * Converts a JavaScript value into the raw form native code expects, unwrapping\n * object, struct, boxed, and fundamental wrappers back to their handles and\n * recursively converting arrays and maps according to the descriptor.\n *\n * @param descriptor Describes the native type to convert to.\n * @param value The JavaScript value to convert.\n */\nfunction toNative(descriptor: Descriptor, value: unknown): unknown {\n if (!isMarshalledDescriptor(descriptor)) {\n return value;\n }\n\n switch (descriptor.kind) {\n case \"object\":\n case \"struct\":\n case \"boxed\":\n case \"fundamental\": {\n const instance = value as object | null | undefined;\n\n return instance == null ? null : getHandle(instance);\n }\n case \"array\": {\n return collectionToNative(descriptor, value);\n }\n case \"hashtable\": {\n return hashTableToNative(descriptor, value);\n }\n }\n}\n\nexport { fromNative, toNative };\n"]}
|
package/dist/object.d.ts
CHANGED
|
@@ -1,14 +1,42 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Descriptor, type ExternalObject, type Handle } from "@gtkx/native";
|
|
2
|
+
import { type AnyClass } from "@gtkx/utils";
|
|
3
|
+
/**
|
|
4
|
+
* One construct property a wrapper class accepts: the canonical `GObject` name it is set under,
|
|
5
|
+
* and the descriptor its value is marshalled through.
|
|
6
|
+
*/
|
|
7
|
+
type ConstructBinding = [name: string, descriptor: Descriptor];
|
|
8
|
+
/** The construct properties a wrapper class accepts, keyed by the camelCased name callers give them. */
|
|
9
|
+
type ConstructBindings = Record<string, ConstructBinding>;
|
|
10
|
+
/**
|
|
11
|
+
* Declares the construct properties a wrapper class accepts, so `newObjectWithProperties`
|
|
12
|
+
* marshals each one through its descriptor rather than resolving it from the
|
|
13
|
+
* `GObject.ParamSpec` the type installs. A class inherits the declarations of its ancestors.
|
|
14
|
+
* Registering takes effect immediately, including for classes already constructed from and for
|
|
15
|
+
* subclasses that already inherited an earlier declaration.
|
|
16
|
+
*
|
|
17
|
+
* @param cls The wrapper class the properties are declared on.
|
|
18
|
+
* @param bindings CamelCased property names mapped to their canonical name and descriptor.
|
|
19
|
+
*/
|
|
20
|
+
declare function registerConstructProperties(cls: AnyClass, bindings: ConstructBindings): void;
|
|
2
21
|
/**
|
|
3
22
|
* Constructs a new GObject of the given type, setting the supplied construct
|
|
4
|
-
* properties
|
|
5
|
-
*
|
|
23
|
+
* properties, and binds `wrapper` to it. A property the wrapper's class declares
|
|
24
|
+
* through `registerConstructProperties` is marshalled through its descriptor; any
|
|
25
|
+
* other one is marshalled through the `GObject.ParamSpec` the type installs under
|
|
26
|
+
* that name, dashed or camelCased, and is skipped when the type installs none.
|
|
27
|
+
* A value that ParamSpec would refuse throws before GObject sees it: a `TypeError`
|
|
28
|
+
* for a read-only property and for a value of a type the property cannot hold, and
|
|
29
|
+
* a `RangeError` for a value the ParamSpec rejects.
|
|
30
|
+
* Properties whose value is `undefined` are skipped. A type registered with
|
|
31
|
+
* `registerClass` binds the wrapper before its `constructed` slot runs, so an
|
|
32
|
+
* override of that slot already sees a usable instance.
|
|
6
33
|
*
|
|
7
34
|
* @param gtype The GType of the object to construct.
|
|
8
|
-
* @param props Property names mapped to
|
|
35
|
+
* @param props Property names mapped to the values to set them to.
|
|
36
|
+
* @param wrapper The wrapper instance to bind to the new object.
|
|
9
37
|
* @returns The handle of the newly created object.
|
|
10
38
|
*/
|
|
11
|
-
declare function newObjectWithProperties(gtype: bigint, props:
|
|
39
|
+
declare function newObjectWithProperties(gtype: bigint, props: object, wrapper: object): ExternalObject<Handle>;
|
|
12
40
|
/**
|
|
13
41
|
* Reads a GObject property and converts it to its JavaScript value using the
|
|
14
42
|
* descriptor.
|
|
@@ -28,5 +56,5 @@ declare function getObjectProperty(obj: object, propertyName: string, descriptor
|
|
|
28
56
|
* @param jsValue The value to set.
|
|
29
57
|
*/
|
|
30
58
|
declare function setObjectProperty(obj: object, propertyName: string, descriptor: Descriptor, jsValue: unknown): void;
|
|
31
|
-
export { newObjectWithProperties, getObjectProperty, setObjectProperty };
|
|
59
|
+
export { newObjectWithProperties, getObjectProperty, registerConstructProperties, setObjectProperty, type ConstructBinding, type ConstructBindings, };
|
|
32
60
|
//# sourceMappingURL=object.d.ts.map
|
package/dist/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,cAAc,EAAE,KAAK,MAAM,EAAa,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAE,KAAK,QAAQ,EAAkB,MAAM,aAAa,CAAC;AAQ5D;;;GAGG;AACH,KAAK,gBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC;AAC/D,wGAAwG;AACxG,KAAK,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;AAmE1D;;;;;;;;;GASG;AACH,iBAAS,2BAA2B,CAAC,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAGrF;AAoBD;;;;;;;;;;;;;;;;;GAiBG;AACH,iBAAS,uBAAuB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC,CAkBtG;AAED;;;;;;;GAOG;AACH,iBAAS,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAK7F;AAED;;;;;;;;GAQG;AACH,iBAAS,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAE5G;AAED,OAAO,EACH,uBAAuB,EACvB,iBAAiB,EACjB,2BAA2B,EAC3B,iBAAiB,EACjB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,GACzB,CAAC"}
|
package/dist/object.js
CHANGED
|
@@ -1,41 +1,105 @@
|
|
|
1
|
+
import { newObject } from "@gtkx/native";
|
|
2
|
+
import { getParentClass } from "@gtkx/utils";
|
|
1
3
|
import { bind } from "./bind.js";
|
|
2
|
-
import {
|
|
3
|
-
import { LIB,
|
|
4
|
-
import {
|
|
4
|
+
import { objectT, stringT, voidT } from "./descriptors.js";
|
|
5
|
+
import { LIB, VALUE_T } from "./library.js";
|
|
6
|
+
import { constructPropertyFor } from "./properties.js";
|
|
7
|
+
import { getHandle, registerWrapper } from "./registry.js";
|
|
5
8
|
import { fromValue, newValueForDescriptor, toValue } from "./value.js";
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
sizedArrayT(VALUE_T, 1, "borrowed", VALUE_SIZE),
|
|
11
|
-
], objectT("full"));
|
|
9
|
+
const declaredBindings = new WeakMap();
|
|
10
|
+
const resolvedBindings = new WeakMap();
|
|
11
|
+
const declarations = { generation: 0 };
|
|
12
|
+
const NO_BINDINGS = Object.create(null);
|
|
12
13
|
const gObjectGetProperty = bind(LIB, "g_object_get_property", [objectT("borrowed"), stringT("borrowed"), VALUE_T], voidT);
|
|
13
14
|
const gObjectSetProperty = bind(LIB, "g_object_set_property", [objectT("borrowed"), stringT("borrowed"), VALUE_T], voidT);
|
|
15
|
+
function collectDeclaredBindings(cls) {
|
|
16
|
+
const declared = [];
|
|
17
|
+
let current = cls;
|
|
18
|
+
while (current !== null) {
|
|
19
|
+
const own = declaredBindings.get(current);
|
|
20
|
+
if (own !== undefined) {
|
|
21
|
+
declared.push(own);
|
|
22
|
+
}
|
|
23
|
+
current = getParentClass(current);
|
|
24
|
+
}
|
|
25
|
+
return declared;
|
|
26
|
+
}
|
|
27
|
+
function mergeDeclaredBindings(cls) {
|
|
28
|
+
const declared = collectDeclaredBindings(cls);
|
|
29
|
+
const merged = Object.create(null);
|
|
30
|
+
for (let index = declared.length - 1; index >= 0; index--) {
|
|
31
|
+
Object.assign(merged, declared[index]);
|
|
32
|
+
}
|
|
33
|
+
return merged;
|
|
34
|
+
}
|
|
35
|
+
function constructBindingsFor(cls) {
|
|
36
|
+
if (cls === undefined) {
|
|
37
|
+
return NO_BINDINGS;
|
|
38
|
+
}
|
|
39
|
+
const cached = resolvedBindings.get(cls);
|
|
40
|
+
if (cached?.generation === declarations.generation) {
|
|
41
|
+
return cached.bindings;
|
|
42
|
+
}
|
|
43
|
+
const bindings = mergeDeclaredBindings(cls);
|
|
44
|
+
resolvedBindings.set(cls, { generation: declarations.generation, bindings });
|
|
45
|
+
return bindings;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Declares the construct properties a wrapper class accepts, so `newObjectWithProperties`
|
|
49
|
+
* marshals each one through its descriptor rather than resolving it from the
|
|
50
|
+
* `GObject.ParamSpec` the type installs. A class inherits the declarations of its ancestors.
|
|
51
|
+
* Registering takes effect immediately, including for classes already constructed from and for
|
|
52
|
+
* subclasses that already inherited an earlier declaration.
|
|
53
|
+
*
|
|
54
|
+
* @param cls The wrapper class the properties are declared on.
|
|
55
|
+
* @param bindings CamelCased property names mapped to their canonical name and descriptor.
|
|
56
|
+
*/
|
|
57
|
+
function registerConstructProperties(cls, bindings) {
|
|
58
|
+
declaredBindings.set(cls, bindings);
|
|
59
|
+
declarations.generation += 1;
|
|
60
|
+
}
|
|
61
|
+
function constructPropertyForEntry(source, name, value) {
|
|
62
|
+
if (value === undefined) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const binding = source.bindings[name];
|
|
66
|
+
if (binding === undefined) {
|
|
67
|
+
return constructPropertyFor(source.gtype, name, value, source.wrapper);
|
|
68
|
+
}
|
|
69
|
+
return { name: binding[0], value: toValue(binding[1], value) };
|
|
70
|
+
}
|
|
14
71
|
/**
|
|
15
72
|
* Constructs a new GObject of the given type, setting the supplied construct
|
|
16
|
-
* properties
|
|
17
|
-
*
|
|
73
|
+
* properties, and binds `wrapper` to it. A property the wrapper's class declares
|
|
74
|
+
* through `registerConstructProperties` is marshalled through its descriptor; any
|
|
75
|
+
* other one is marshalled through the `GObject.ParamSpec` the type installs under
|
|
76
|
+
* that name, dashed or camelCased, and is skipped when the type installs none.
|
|
77
|
+
* A value that ParamSpec would refuse throws before GObject sees it: a `TypeError`
|
|
78
|
+
* for a read-only property and for a value of a type the property cannot hold, and
|
|
79
|
+
* a `RangeError` for a value the ParamSpec rejects.
|
|
80
|
+
* Properties whose value is `undefined` are skipped. A type registered with
|
|
81
|
+
* `registerClass` binds the wrapper before its `constructed` slot runs, so an
|
|
82
|
+
* override of that slot already sees a usable instance.
|
|
18
83
|
*
|
|
19
84
|
* @param gtype The GType of the object to construct.
|
|
20
|
-
* @param props Property names mapped to
|
|
85
|
+
* @param props Property names mapped to the values to set them to.
|
|
86
|
+
* @param wrapper The wrapper instance to bind to the new object.
|
|
21
87
|
* @returns The handle of the newly created object.
|
|
22
88
|
*/
|
|
23
|
-
function newObjectWithProperties(gtype, props) {
|
|
89
|
+
function newObjectWithProperties(gtype, props, wrapper) {
|
|
24
90
|
const names = [];
|
|
25
91
|
const values = [];
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
continue;
|
|
92
|
+
const bindings = constructBindingsFor(wrapper.constructor);
|
|
93
|
+
const source = { gtype, bindings, wrapper };
|
|
94
|
+
for (const name of Object.keys(props)) {
|
|
95
|
+
const property = constructPropertyForEntry(source, name, Reflect.get(props, name));
|
|
96
|
+
if (property !== undefined) {
|
|
97
|
+
names.push(property.name);
|
|
98
|
+
values.push(property.value);
|
|
34
99
|
}
|
|
35
|
-
names.push(name);
|
|
36
|
-
values.push(toValue(descriptor, value));
|
|
37
100
|
}
|
|
38
|
-
|
|
101
|
+
newObject(gtype, names, values, wrapper, registerWrapper);
|
|
102
|
+
return getHandle(wrapper);
|
|
39
103
|
}
|
|
40
104
|
/**
|
|
41
105
|
* Reads a GObject property and converts it to its JavaScript value using the
|
|
@@ -62,5 +126,5 @@ function getObjectProperty(obj, propertyName, descriptor) {
|
|
|
62
126
|
function setObjectProperty(obj, propertyName, descriptor, jsValue) {
|
|
63
127
|
gObjectSetProperty(getHandle(obj), propertyName, toValue(descriptor, jsValue));
|
|
64
128
|
}
|
|
65
|
-
export { newObjectWithProperties, getObjectProperty, setObjectProperty };
|
|
129
|
+
export { newObjectWithProperties, getObjectProperty, registerConstructProperties, setObjectProperty, };
|
|
66
130
|
//# sourceMappingURL=object.js.map
|
package/dist/object.js.map
CHANGED
|
@@ -1 +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,M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AC,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED,OAAO,wBAAwB,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAA2B,CAAC;AAClG,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB;IAChF,MAAM,KAAK,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAC;IAChD,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;IAExD,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB,EAAE,OAAgB;IAClG,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;AACnF,CAAC;AAED,OAAO,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,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);\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 */\nfunction 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: unknown = props[name];\n\n if (!Array.isArray(entry)) {\n continue;\n }\n\n const [descriptor, value] = entry as [Descriptor, unknown];\n\n if (value === undefined) {\n continue;\n }\n\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 */\nfunction getObjectProperty(obj: object, propertyName: string, descriptor: Descriptor): unknown {\n const value = newValueForDescriptor(descriptor);\n gObjectGetProperty(getHandle(obj), propertyName, value);\n\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 */\nfunction setObjectProperty(obj: object, propertyName: string, descriptor: Descriptor, jsValue: unknown): void {\n gObjectSetProperty(getHandle(obj), propertyName, toValue(descriptor, jsValue));\n}\n\nexport { newObjectWithProperties, getObjectProperty, setObjectProperty };\n"]}
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqD,SAAS,EAAE,MAAM,cAAc,CAAC;AAC5F,OAAO,EAAiB,cAAc,E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IAED,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,CAAC,CAAC;IAE1D,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC;AAC9B,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB;IAChF,MAAM,KAAK,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAC;IAChD,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;IAExD,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,iBAAiB,CAAC,GAAW,EAAE,YAAoB,EAAE,UAAsB,EAAE,OAAgB;IAClG,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;AACnF,CAAC;AAED,OAAO,EACH,uBAAuB,EACvB,iBAAiB,EACjB,2BAA2B,EAC3B,iBAAiB,GAGpB,CAAC","sourcesContent":["import { type Descriptor, type ExternalObject, type Handle, newObject } from \"@gtkx/native\";\nimport { type AnyClass, getParentClass } from \"@gtkx/utils\";\nimport { bind } from \"./bind.js\";\nimport { objectT, stringT, voidT } from \"./descriptors.js\";\nimport { LIB, VALUE_T } from \"./library.js\";\nimport { type ConstructProperty, constructPropertyFor } from \"./properties.js\";\nimport { getHandle, registerWrapper } from \"./registry.js\";\nimport { fromValue, newValueForDescriptor, toValue } from \"./value.js\";\n\n/**\n * One construct property a wrapper class accepts: the canonical `GObject` name it is set under,\n * and the descriptor its value is marshalled through.\n */\ntype ConstructBinding = [name: string, descriptor: Descriptor];\n/** The construct properties a wrapper class accepts, keyed by the camelCased name callers give them. */\ntype ConstructBindings = Record<string, ConstructBinding>;\ntype ResolvedBindings = { generation: number; bindings: ConstructBindings };\n\nconst declaredBindings: WeakMap<AnyClass, ConstructBindings> = new WeakMap();\nconst resolvedBindings: WeakMap<AnyClass, ResolvedBindings> = new WeakMap();\nconst declarations = { generation: 0 };\nconst NO_BINDINGS: ConstructBindings = Object.create(null) as ConstructBindings;\n\nconst gObjectGetProperty = bind(\n LIB,\n \"g_object_get_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\n\nconst gObjectSetProperty = bind(\n LIB,\n \"g_object_set_property\",\n [objectT(\"borrowed\"), stringT(\"borrowed\"), VALUE_T],\n voidT,\n);\n\nfunction collectDeclaredBindings(cls: AnyClass): ConstructBindings[] {\n const declared: ConstructBindings[] = [];\n let current: AnyClass | null = cls;\n\n while (current !== null) {\n const own = declaredBindings.get(current);\n\n if (own !== undefined) {\n declared.push(own);\n }\n\n current = getParentClass(current);\n }\n\n return declared;\n}\n\nfunction mergeDeclaredBindings(cls: AnyClass): ConstructBindings {\n const declared = collectDeclaredBindings(cls);\n const merged: ConstructBindings = Object.create(null) as ConstructBindings;\n\n for (let index = declared.length - 1; index >= 0; index--) {\n Object.assign(merged, declared[index]);\n }\n\n return merged;\n}\n\nfunction constructBindingsFor(cls: AnyClass | undefined): ConstructBindings {\n if (cls === undefined) {\n return NO_BINDINGS;\n }\n\n const cached = resolvedBindings.get(cls);\n\n if (cached?.generation === declarations.generation) {\n return cached.bindings;\n }\n\n const bindings = mergeDeclaredBindings(cls);\n resolvedBindings.set(cls, { generation: declarations.generation, bindings });\n\n return bindings;\n}\n\n/**\n * Declares the construct properties a wrapper class accepts, so `newObjectWithProperties`\n * marshals each one through its descriptor rather than resolving it from the\n * `GObject.ParamSpec` the type installs. A class inherits the declarations of its ancestors.\n * Registering takes effect immediately, including for classes already constructed from and for\n * subclasses that already inherited an earlier declaration.\n *\n * @param cls The wrapper class the properties are declared on.\n * @param bindings CamelCased property names mapped to their canonical name and descriptor.\n */\nfunction registerConstructProperties(cls: AnyClass, bindings: ConstructBindings): void {\n declaredBindings.set(cls, bindings);\n declarations.generation += 1;\n}\n\nfunction constructPropertyForEntry(\n source: { gtype: bigint; bindings: ConstructBindings; wrapper: object },\n name: string,\n value: unknown,\n): ConstructProperty | undefined {\n if (value === undefined) {\n return undefined;\n }\n\n const binding = source.bindings[name];\n\n if (binding === undefined) {\n return constructPropertyFor(source.gtype, name, value, source.wrapper);\n }\n\n return { name: binding[0], value: toValue(binding[1], value) };\n}\n\n/**\n * Constructs a new GObject of the given type, setting the supplied construct\n * properties, and binds `wrapper` to it. A property the wrapper's class declares\n * through `registerConstructProperties` is marshalled through its descriptor; any\n * other one is marshalled through the `GObject.ParamSpec` the type installs under\n * that name, dashed or camelCased, and is skipped when the type installs none.\n * A value that ParamSpec would refuse throws before GObject sees it: a `TypeError`\n * for a read-only property and for a value of a type the property cannot hold, and\n * a `RangeError` for a value the ParamSpec rejects.\n * Properties whose value is `undefined` are skipped. A type registered with\n * `registerClass` binds the wrapper before its `constructed` slot runs, so an\n * override of that slot already sees a usable instance.\n *\n * @param gtype The GType of the object to construct.\n * @param props Property names mapped to the values to set them to.\n * @param wrapper The wrapper instance to bind to the new object.\n * @returns The handle of the newly created object.\n */\nfunction newObjectWithProperties(gtype: bigint, props: object, wrapper: object): ExternalObject<Handle> {\n const names: string[] = [];\n const values: ExternalObject<Handle>[] = [];\n const bindings = constructBindingsFor(wrapper.constructor as AnyClass | undefined);\n const source = { gtype, bindings, wrapper };\n\n for (const name of Object.keys(props)) {\n const property = constructPropertyForEntry(source, name, Reflect.get(props, name));\n\n if (property !== undefined) {\n names.push(property.name);\n values.push(property.value);\n }\n }\n\n newObject(gtype, names, values, wrapper, registerWrapper);\n\n return getHandle(wrapper);\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 */\nfunction getObjectProperty(obj: object, propertyName: string, descriptor: Descriptor): unknown {\n const value = newValueForDescriptor(descriptor);\n gObjectGetProperty(getHandle(obj), propertyName, value);\n\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 */\nfunction setObjectProperty(obj: object, propertyName: string, descriptor: Descriptor, jsValue: unknown): void {\n gObjectSetProperty(getHandle(obj), propertyName, toValue(descriptor, jsValue));\n}\n\nexport {\n newObjectWithProperties,\n getObjectProperty,\n registerConstructProperties,\n setObjectProperty,\n type ConstructBinding,\n type ConstructBindings,\n};\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ExternalObject, type Handle } from "@gtkx/native";
|
|
2
|
+
type ValueGuard = (value: unknown) => boolean;
|
|
3
|
+
declare const getParamFlags: (pspec: ExternalObject<Handle>) => number;
|
|
4
|
+
declare const getParamValueType: (pspec: ExternalObject<Handle>) => bigint;
|
|
5
|
+
declare const isParamWritable: (flags: number) => boolean;
|
|
6
|
+
declare const isParamConstructOnly: (flags: number) => boolean;
|
|
7
|
+
declare const isParamLaxlyValidated: (flags: number) => boolean;
|
|
8
|
+
declare const wasParamValueModified: (pspec: ExternalObject<Handle>, value: ExternalObject<Handle>) => boolean;
|
|
9
|
+
declare function valueGuardFor(valueType: bigint): ValueGuard;
|
|
10
|
+
export { getParamFlags, getParamValueType, isParamConstructOnly, isParamLaxlyValidated, isParamWritable, type ValueGuard, valueGuardFor, wasParamValueModified, };
|
|
11
|
+
//# sourceMappingURL=param-spec.d.ts.map
|