gnim 1.2.0 → 1.2.2
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 +3 -8
- package/dist/dbus.d.ts +17 -19
- package/dist/dbus.js +56 -89
- package/dist/gnim.gresource +0 -0
- package/dist/gobject.d.ts +4 -2
- package/dist/gobject.js +48 -7
- package/dist/jsx/index.d.ts +1 -1
- package/dist/jsx/index.js +1 -1
- package/dist/jsx/jsx.d.ts +1 -1
- package/dist/jsx/state.d.ts +38 -6
- package/dist/jsx/state.js +49 -5
- package/dist/util.d.ts +3 -2
- package/dist/util.js +5 -2
- package/dist/variant.d.ts +77 -0
- package/dist/variant.js +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,12 +33,7 @@ function Counter() {
|
|
|
33
33
|
}
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
##
|
|
36
|
+
## Templates
|
|
37
37
|
|
|
38
|
-
- [
|
|
39
|
-
|
|
40
|
-
## Credits
|
|
41
|
-
|
|
42
|
-
- [JU12000](https://github.com/JU12000) for suggesting the name Gnim
|
|
43
|
-
- [Azazel-Woodwind](https://github.com/Azazel-Woodwind) for a lot of early
|
|
44
|
-
testing
|
|
38
|
+
- [gnome-extension](https://github.com/Aylur/gnome-shell-extension-template/)
|
|
39
|
+
- [gtk4](https://github.com/Aylur/gnim-gtk4-template/)
|
package/dist/dbus.d.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
import Gio from "gi://Gio";
|
|
8
8
|
import GLib from "gi://GLib";
|
|
9
9
|
import GObject from "gi://GObject";
|
|
10
|
+
import { type InferVariant } from "./variant.js";
|
|
11
|
+
import { register } from "./gobject.js";
|
|
10
12
|
declare const info: unique symbol;
|
|
11
13
|
declare const internals: unique symbol;
|
|
12
14
|
declare const remoteMethod: unique symbol;
|
|
@@ -51,26 +53,19 @@ export declare class Service extends GObject.Object {
|
|
|
51
53
|
* Registers a {@link Service} as a dbus interface.
|
|
52
54
|
*
|
|
53
55
|
* @param name Interface name of the object. For example "org.gnome.Shell.SearchProvider2"
|
|
54
|
-
* @param
|
|
56
|
+
* @param options optional properties to pass to {@link register}
|
|
55
57
|
*/
|
|
56
|
-
export declare function iface(name: string,
|
|
57
|
-
$gtype: GObject.GType;
|
|
58
|
-
}>, never>): (cls: {
|
|
58
|
+
export declare function iface(name: string, options?: Parameters<typeof register>[0]): (cls: {
|
|
59
59
|
new (...args: any[]): Service;
|
|
60
60
|
}, ctx: ClassDecoratorContext) => void;
|
|
61
61
|
type DBusType = string | {
|
|
62
62
|
type: string;
|
|
63
63
|
name: string;
|
|
64
64
|
};
|
|
65
|
-
type InferVariantTuple<NestedVariant extends string> = NestedVariant extends string ? Array<any> : never;
|
|
66
|
-
/**
|
|
67
|
-
* Infer the runtime JavaScript type of a Variant type
|
|
68
|
-
*/
|
|
69
|
-
export type InferVariantType<T extends string> = T extends "v" ? Variant<any> : T extends "b" ? boolean : T extends "y" | "n" | "q" | "i" | "u" | "x" | "t" | "h" | "d" ? number : T extends "s" | "g" ? string : T extends "o" ? `/${string}` : T extends `a{${infer K}${infer S}}` ? S extends string ? K extends string ? Record<Extract<InferVariantType<K>, string | number>, InferVariantType<S>> : never : never : T extends `a${infer S}` ? S extends string ? Array<InferVariantType<S>> : never : T extends `(${infer S})` ? S extends string ? InferVariantTuple<S> : never : never;
|
|
70
65
|
type InferVariantTypes<T extends Array<DBusType>> = {
|
|
71
|
-
[K in keyof T]: T[K] extends string ?
|
|
66
|
+
[K in keyof T]: T[K] extends string ? InferVariant<T[K]> : T[K] extends {
|
|
72
67
|
type: infer S;
|
|
73
|
-
} ? S extends string ?
|
|
68
|
+
} ? S extends string ? InferVariant<S> : never : unknown;
|
|
74
69
|
};
|
|
75
70
|
/**
|
|
76
71
|
* Registers a method.
|
|
@@ -108,33 +103,36 @@ export declare function methodAsync<const InArgs extends Array<DBusType>>(...inA
|
|
|
108
103
|
* not emit the notify signal.
|
|
109
104
|
* ```
|
|
110
105
|
*/
|
|
111
|
-
export declare function property<T extends string>(type: T): (_: void, ctx: ClassFieldDecoratorContext<Service,
|
|
106
|
+
export declare function property<T extends string>(type: T): (_: void, ctx: ClassFieldDecoratorContext<Service, InferVariant<T>>) => (this: Service, init: InferVariant<T>) => InferVariant<T>;
|
|
112
107
|
/**
|
|
113
108
|
* Registers a read-only property. Can be used in conjuction with {@link setter} to define
|
|
114
109
|
* read-write properties as accessors.
|
|
115
110
|
*
|
|
116
111
|
* Note that you will need to explicitly emit the notify signal.
|
|
117
112
|
*/
|
|
118
|
-
export declare function getter<T extends string>(type: T): (getter: (this: Service) =>
|
|
113
|
+
export declare function getter<T extends string>(type: T): (getter: (this: Service) => InferVariant<T>, ctx: ClassGetterDecoratorContext<Service, InferVariant<T>>) => (this: Service) => InferVariant<T>;
|
|
119
114
|
/**
|
|
120
115
|
* Registers a write-only property. Can be used in conjuction with {@link getter} to define
|
|
121
116
|
* read-write properties as accessors.
|
|
122
117
|
*
|
|
123
118
|
* Note that you will need to explicitly emit the notify signal.
|
|
124
119
|
*/
|
|
125
|
-
export declare function setter<T extends string>(type: T): (setter: (this: Service, value:
|
|
120
|
+
export declare function setter<T extends string>(type: T): (setter: (this: Service, value: InferVariant<T>) => void, ctx: ClassSetterDecoratorContext<Service, InferVariant<T>>) => (this: Service, value: InferVariant<T>) => void;
|
|
126
121
|
/**
|
|
127
122
|
* Registers a signal which when invoked will emit the signal
|
|
128
123
|
* on the local object and the exported object.
|
|
129
124
|
*
|
|
130
|
-
* Note
|
|
125
|
+
* **Note**: its not possible to emit signals on remote objects through proxies.
|
|
131
126
|
*/
|
|
132
127
|
export declare function signal<const Params extends Array<DBusType>>(...params: Params): (method: (this: Service, ...params: InferVariantTypes<Params>) => void, ctx: ClassMethodDecoratorContext<Service, typeof method>) => typeof method;
|
|
133
128
|
declare class GLibVariant<T extends string = any> extends GLib.Variant<T> {
|
|
134
|
-
static new<T extends string>(signature: T, value:
|
|
135
|
-
constructor(signature: T, value:
|
|
136
|
-
deepUnpack<V =
|
|
137
|
-
deep_unpack<V =
|
|
129
|
+
static new<T extends string>(signature: T, value: InferVariant<T>): GLib.Variant<T>;
|
|
130
|
+
constructor(signature: T, value: InferVariant<T>);
|
|
131
|
+
deepUnpack<V = InferVariant<T>>(): V;
|
|
132
|
+
deep_unpack<V = InferVariant<T>>(): V;
|
|
133
|
+
}
|
|
134
|
+
export declare namespace Variant {
|
|
135
|
+
type Infer<S extends string> = InferVariant<S>;
|
|
138
136
|
}
|
|
139
137
|
export declare const Variant: typeof GLibVariant;
|
|
140
138
|
export type Variant<T extends string = any> = GLibVariant<T>;
|
package/dist/dbus.js
CHANGED
|
@@ -19,7 +19,8 @@ var _Service_instances, _a, _Service_info, _Service_handlePropertyGet, _Service_
|
|
|
19
19
|
import Gio from "gi://Gio";
|
|
20
20
|
import GLib from "gi://GLib";
|
|
21
21
|
import GObject from "gi://GObject";
|
|
22
|
-
import {
|
|
22
|
+
import { definePropertyGetter, kebabify, xml } from "./util.js";
|
|
23
|
+
import { register, property as gproperty, signal as gsignal, getter as ggetter, setter as gsetter, } from "./gobject.js";
|
|
23
24
|
const DEFAULT_TIMEOUT = 10000;
|
|
24
25
|
const info = Symbol("dbus interface info");
|
|
25
26
|
const internals = Symbol("dbus interface internals");
|
|
@@ -275,79 +276,18 @@ _a = Service;
|
|
|
275
276
|
(() => {
|
|
276
277
|
GObject.registerClass(_a);
|
|
277
278
|
})();
|
|
278
|
-
function paramSpecFromVariant(type, name, { read, write }) {
|
|
279
|
-
let flags = 0;
|
|
280
|
-
if (read)
|
|
281
|
-
flags |= GObject.ParamFlags.READABLE;
|
|
282
|
-
if (write)
|
|
283
|
-
flags |= GObject.ParamFlags.WRITABLE;
|
|
284
|
-
if (type.startsWith("a") || type.startsWith("(") || type === "v") {
|
|
285
|
-
return GObject.ParamSpec.jsobject(name, name, name, flags);
|
|
286
|
-
}
|
|
287
|
-
switch (type) {
|
|
288
|
-
case "b":
|
|
289
|
-
return GObject.ParamSpec.boolean(name, name, name, flags, false);
|
|
290
|
-
case "y":
|
|
291
|
-
case "n":
|
|
292
|
-
case "q":
|
|
293
|
-
case "i":
|
|
294
|
-
case "u":
|
|
295
|
-
case "x":
|
|
296
|
-
case "t":
|
|
297
|
-
case "h":
|
|
298
|
-
case "d":
|
|
299
|
-
// TODO: be more specific about number types
|
|
300
|
-
return GObject.ParamSpec.double(name, name, name, flags, Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER, 0);
|
|
301
|
-
case "s":
|
|
302
|
-
case "g":
|
|
303
|
-
case "o":
|
|
304
|
-
return GObject.ParamSpec.string(name, name, name, flags, "");
|
|
305
|
-
default:
|
|
306
|
-
break;
|
|
307
|
-
}
|
|
308
|
-
throw Error(`cannot infer ParamSpec from variant "${type}"`);
|
|
309
|
-
}
|
|
310
|
-
function inferGTypeFromVariant(type) {
|
|
311
|
-
if (type.startsWith("a") || type.startsWith("(")) {
|
|
312
|
-
return GObject.TYPE_JSOBJECT;
|
|
313
|
-
}
|
|
314
|
-
switch (type) {
|
|
315
|
-
case "v":
|
|
316
|
-
return GObject.TYPE_VARIANT;
|
|
317
|
-
case "b":
|
|
318
|
-
return GObject.TYPE_BOOLEAN;
|
|
319
|
-
case "y":
|
|
320
|
-
case "n":
|
|
321
|
-
case "q":
|
|
322
|
-
case "i":
|
|
323
|
-
case "u":
|
|
324
|
-
case "x":
|
|
325
|
-
case "t":
|
|
326
|
-
case "h":
|
|
327
|
-
case "d":
|
|
328
|
-
// TODO: be more specific about number types
|
|
329
|
-
return GObject.TYPE_DOUBLE;
|
|
330
|
-
case "s":
|
|
331
|
-
case "g":
|
|
332
|
-
case "o":
|
|
333
|
-
return GObject.TYPE_STRING;
|
|
334
|
-
default:
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
throw Error(`cannot infer GType from variant "${type}"`);
|
|
338
|
-
}
|
|
339
279
|
/**
|
|
340
280
|
* Registers a {@link Service} as a dbus interface.
|
|
341
281
|
*
|
|
342
282
|
* @param name Interface name of the object. For example "org.gnome.Shell.SearchProvider2"
|
|
343
|
-
* @param
|
|
283
|
+
* @param options optional properties to pass to {@link register}
|
|
344
284
|
*/
|
|
345
|
-
export function iface(name,
|
|
285
|
+
export function iface(name, options) {
|
|
346
286
|
return function (cls, ctx) {
|
|
347
287
|
const meta = ctx.metadata;
|
|
348
288
|
if (!meta)
|
|
349
289
|
throw Error(`${cls.name} is not an interface`);
|
|
350
|
-
const {
|
|
290
|
+
const { dbusMethods = {}, dbusSignals = {}, dbusProperties = {} } = meta;
|
|
351
291
|
const infoXml = xml({
|
|
352
292
|
name: "node",
|
|
353
293
|
children: [
|
|
@@ -355,17 +295,17 @@ export function iface(name, gobjectMetaInfo) {
|
|
|
355
295
|
name: "interface",
|
|
356
296
|
attributes: { name },
|
|
357
297
|
children: [
|
|
358
|
-
...Object.entries(
|
|
298
|
+
...Object.entries(dbusMethods).map(([name, args]) => ({
|
|
359
299
|
name: "method",
|
|
360
300
|
attributes: { name },
|
|
361
301
|
children: args.map((arg) => ({ name: "arg", attributes: arg })),
|
|
362
302
|
})),
|
|
363
|
-
...Object.entries(
|
|
303
|
+
...Object.entries(dbusSignals).map(([name, args]) => ({
|
|
364
304
|
name: "signal",
|
|
365
305
|
attributes: { name },
|
|
366
306
|
children: args.map((arg) => ({ name: "arg", attributes: arg })),
|
|
367
307
|
})),
|
|
368
|
-
...Object.values(
|
|
308
|
+
...Object.values(dbusProperties).map(({ name, type, read, write }) => ({
|
|
369
309
|
name: "property",
|
|
370
310
|
attributes: {
|
|
371
311
|
...(name && { name }),
|
|
@@ -378,27 +318,13 @@ export function iface(name, gobjectMetaInfo) {
|
|
|
378
318
|
],
|
|
379
319
|
});
|
|
380
320
|
Object.assign(cls, { [info]: Gio.DBusInterfaceInfo.new_for_xml(infoXml) });
|
|
381
|
-
|
|
382
|
-
Properties: Object.fromEntries(Object.values(properties).map(({ type, name, read, write }) => {
|
|
383
|
-
const key = kebabify(name);
|
|
384
|
-
return [key, paramSpecFromVariant(type, key, { read, write })];
|
|
385
|
-
})),
|
|
386
|
-
Signals: Object.fromEntries(Object.entries(signals).map(([name, args]) => {
|
|
387
|
-
return [
|
|
388
|
-
kebabify(name),
|
|
389
|
-
{
|
|
390
|
-
param_types: args.map((a) => inferGTypeFromVariant(typeof a === "string" ? a : a.type)),
|
|
391
|
-
},
|
|
392
|
-
];
|
|
393
|
-
})),
|
|
394
|
-
...(gobjectMetaInfo ?? {}),
|
|
395
|
-
}, cls);
|
|
321
|
+
register(options)(cls, ctx);
|
|
396
322
|
};
|
|
397
323
|
}
|
|
398
324
|
function installMethod(args, method, ctx) {
|
|
399
325
|
const name = ctx.name;
|
|
400
326
|
const meta = ctx.metadata;
|
|
401
|
-
const methods = (meta.
|
|
327
|
+
const methods = (meta.dbusMethods ?? (meta.dbusMethods = {}));
|
|
402
328
|
if (typeof name !== "string") {
|
|
403
329
|
throw Error("only string named methods are allowed");
|
|
404
330
|
}
|
|
@@ -419,7 +345,7 @@ function installProperty(type, ctx) {
|
|
|
419
345
|
const kind = ctx.kind;
|
|
420
346
|
const name = ctx.name;
|
|
421
347
|
const meta = ctx.metadata;
|
|
422
|
-
const properties = (meta.
|
|
348
|
+
const properties = (meta.dbusProperties ?? (meta.dbusProperties = {}));
|
|
423
349
|
if (typeof name !== "string") {
|
|
424
350
|
throw Error("only string named properties are allowed");
|
|
425
351
|
}
|
|
@@ -444,13 +370,51 @@ function installProperty(type, ctx) {
|
|
|
444
370
|
function installSignal(params, ctx) {
|
|
445
371
|
const name = ctx.name;
|
|
446
372
|
const meta = ctx.metadata;
|
|
447
|
-
const signals = (meta.
|
|
373
|
+
const signals = (meta.dbusSignals ?? (meta.dbusSignals = {}));
|
|
448
374
|
if (typeof name === "symbol") {
|
|
449
375
|
throw Error("symbols are not valid signals");
|
|
450
376
|
}
|
|
451
377
|
signals[name] = params.map((arg) => (typeof arg === "string" ? { type: arg } : arg));
|
|
452
378
|
return name;
|
|
453
379
|
}
|
|
380
|
+
function inferGTypeFromVariant(type) {
|
|
381
|
+
if (typeof type !== "string")
|
|
382
|
+
return inferGTypeFromVariant(type.type);
|
|
383
|
+
if (type.startsWith("a") || type.startsWith("(")) {
|
|
384
|
+
return GObject.TYPE_JSOBJECT;
|
|
385
|
+
}
|
|
386
|
+
switch (type) {
|
|
387
|
+
case "v":
|
|
388
|
+
return GObject.TYPE_VARIANT;
|
|
389
|
+
case "b":
|
|
390
|
+
return GObject.TYPE_BOOLEAN;
|
|
391
|
+
case "y":
|
|
392
|
+
return GObject.TYPE_UINT;
|
|
393
|
+
case "n":
|
|
394
|
+
return GObject.TYPE_INT;
|
|
395
|
+
case "q":
|
|
396
|
+
return GObject.TYPE_UINT;
|
|
397
|
+
case "i":
|
|
398
|
+
return GObject.TYPE_INT;
|
|
399
|
+
case "u":
|
|
400
|
+
return GObject.TYPE_UINT;
|
|
401
|
+
case "x":
|
|
402
|
+
return GObject.TYPE_INT64;
|
|
403
|
+
case "t":
|
|
404
|
+
return GObject.TYPE_UINT64;
|
|
405
|
+
case "h":
|
|
406
|
+
return GObject.TYPE_INT;
|
|
407
|
+
case "d":
|
|
408
|
+
return GObject.TYPE_DOUBLE;
|
|
409
|
+
case "s":
|
|
410
|
+
case "g":
|
|
411
|
+
case "o":
|
|
412
|
+
return GObject.TYPE_STRING;
|
|
413
|
+
default:
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
throw Error(`cannot infer GType from variant "${type}"`);
|
|
417
|
+
}
|
|
454
418
|
export function method(...args) {
|
|
455
419
|
return function (method, ctx) {
|
|
456
420
|
const name = installMethod(args, method, ctx);
|
|
@@ -490,8 +454,8 @@ export function methodAsync(...args) {
|
|
|
490
454
|
export function property(type) {
|
|
491
455
|
return function (_, ctx) {
|
|
492
456
|
const name = installProperty(type, ctx);
|
|
457
|
+
void gproperty({ $gtype: inferGTypeFromVariant(type) })(_, ctx, { metaOnly: true });
|
|
493
458
|
ctx.addInitializer(function () {
|
|
494
|
-
getterWorkaround(this, name);
|
|
495
459
|
Object.defineProperty(this, name, {
|
|
496
460
|
configurable: false,
|
|
497
461
|
enumerable: true,
|
|
@@ -532,8 +496,9 @@ export function getter(type) {
|
|
|
532
496
|
return function (getter, ctx) {
|
|
533
497
|
const name = installProperty(type, ctx);
|
|
534
498
|
ctx.addInitializer(function () {
|
|
535
|
-
|
|
499
|
+
definePropertyGetter(this, name);
|
|
536
500
|
});
|
|
501
|
+
void ggetter({ $gtype: inferGTypeFromVariant(type) })(() => { }, ctx);
|
|
537
502
|
return function () {
|
|
538
503
|
const { proxy } = this[internals];
|
|
539
504
|
return proxy
|
|
@@ -551,6 +516,7 @@ export function getter(type) {
|
|
|
551
516
|
export function setter(type) {
|
|
552
517
|
return function (setter, ctx) {
|
|
553
518
|
const name = installProperty(type, ctx);
|
|
519
|
+
void gsetter({ $gtype: inferGTypeFromVariant(type) })(() => { }, ctx);
|
|
554
520
|
return function (value) {
|
|
555
521
|
const { proxy } = this[internals];
|
|
556
522
|
if (proxy) {
|
|
@@ -566,11 +532,12 @@ export function setter(type) {
|
|
|
566
532
|
* Registers a signal which when invoked will emit the signal
|
|
567
533
|
* on the local object and the exported object.
|
|
568
534
|
*
|
|
569
|
-
* Note
|
|
535
|
+
* **Note**: its not possible to emit signals on remote objects through proxies.
|
|
570
536
|
*/
|
|
571
537
|
export function signal(...params) {
|
|
572
538
|
return function (method, ctx) {
|
|
573
539
|
const name = installSignal(params, ctx);
|
|
540
|
+
void gsignal(...params.map(inferGTypeFromVariant))(() => { }, ctx);
|
|
574
541
|
return function (...params) {
|
|
575
542
|
if (this[internals].proxy) {
|
|
576
543
|
console.warn(`cannot emit signal "${name}" on remote object`);
|
|
Binary file
|
package/dist/gobject.d.ts
CHANGED
|
@@ -35,7 +35,9 @@ type PropertyTypeDeclaration<T> = ((name: string, flags: ParamFlags) => ParamSpe
|
|
|
35
35
|
* }
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
|
-
export declare function property<T>(typeDeclaration: PropertyTypeDeclaration<T>): (_: void, ctx: ClassFieldDecoratorContext<GObj, T
|
|
38
|
+
export declare function property<T>(typeDeclaration: PropertyTypeDeclaration<T>): (_: void, ctx: ClassFieldDecoratorContext<GObj, T>, options?: {
|
|
39
|
+
metaOnly: true;
|
|
40
|
+
}) => (this: GObj, init: T) => T;
|
|
39
41
|
/**
|
|
40
42
|
* Defines a read-only property to be registered when using the {@link register} decorator.
|
|
41
43
|
* If the getter has a setter pair decorated with the {@link setter} decorator the property will be readable *and* writeable.
|
|
@@ -121,7 +123,7 @@ export declare function signal<const Params extends Array<{
|
|
|
121
123
|
*/
|
|
122
124
|
export declare function signal<Params extends Array<{
|
|
123
125
|
$gtype: GType;
|
|
124
|
-
} | GType>>(...params: Params): (method: (this:
|
|
126
|
+
} | GType>>(...params: Params): (method: (this: GObject.Object, ...args: ParamTypes<Params>) => void, ctx: ClassMethodDecoratorContext<GObj, typeof method>) => typeof method;
|
|
125
127
|
type MetaInfo = GObject.MetaInfo<never, Array<{
|
|
126
128
|
$gtype: GType<unknown>;
|
|
127
129
|
}>, never>;
|
package/dist/gobject.js
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* so I'm waiting for a better alternative.
|
|
10
10
|
*/
|
|
11
11
|
import GObject from "gi://GObject";
|
|
12
|
-
import
|
|
12
|
+
import GLib from "gi://GLib";
|
|
13
|
+
import { definePropertyGetter, kebabify } from "./util.js";
|
|
13
14
|
const priv = Symbol("gobject private");
|
|
14
15
|
const { defineProperty, fromEntries, entries } = Object;
|
|
15
16
|
const { Object: GObj, registerClass } = GObject;
|
|
@@ -40,14 +41,16 @@ function assertField(ctx) {
|
|
|
40
41
|
* ```
|
|
41
42
|
*/
|
|
42
43
|
export function property(typeDeclaration) {
|
|
43
|
-
return function (_, ctx) {
|
|
44
|
+
return function (_, ctx, options) {
|
|
44
45
|
const fieldName = assertField(ctx);
|
|
45
46
|
const key = kebabify(fieldName);
|
|
46
47
|
const meta = ctx.metadata;
|
|
47
48
|
meta.properties ?? (meta.properties = {});
|
|
48
49
|
meta.properties[fieldName] = { flags: ParamFlags.READWRITE, type: typeDeclaration };
|
|
49
50
|
ctx.addInitializer(function () {
|
|
50
|
-
|
|
51
|
+
definePropertyGetter(this, fieldName);
|
|
52
|
+
if (options && options.metaOnly)
|
|
53
|
+
return;
|
|
51
54
|
defineProperty(this, fieldName, {
|
|
52
55
|
enumerable: true,
|
|
53
56
|
configurable: false,
|
|
@@ -178,10 +181,48 @@ export function signal(...args) {
|
|
|
178
181
|
};
|
|
179
182
|
};
|
|
180
183
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
+
const MAXINT = 2 ** 31 - 1;
|
|
185
|
+
const MININT = -(2 ** 31);
|
|
186
|
+
const MAXUINT = 2 ** 32 - 1;
|
|
187
|
+
const MAXFLOAT = 3.4028235e38;
|
|
188
|
+
const MINFLOAT = -3.4028235e38;
|
|
189
|
+
const MININT64 = Number.MIN_SAFE_INTEGER;
|
|
190
|
+
const MAXINT64 = Number.MAX_SAFE_INTEGER;
|
|
191
|
+
function pspecFromGType(type, name, flags) {
|
|
192
|
+
switch (type) {
|
|
193
|
+
case GObject.TYPE_BOOLEAN:
|
|
194
|
+
return ParamSpec.boolean(name, "", "", flags, false);
|
|
195
|
+
case GObject.TYPE_STRING:
|
|
196
|
+
return ParamSpec.string(name, "", "", flags, "");
|
|
197
|
+
case GObject.TYPE_INT:
|
|
198
|
+
return ParamSpec.int(name, "", "", flags, MININT, MAXINT, 0);
|
|
199
|
+
case GObject.TYPE_UINT:
|
|
200
|
+
return ParamSpec.uint(name, "", "", flags, 0, MAXUINT, 0);
|
|
201
|
+
case GObject.TYPE_INT64:
|
|
202
|
+
return ParamSpec.int64(name, "", "", flags, MININT64, MAXINT64, 0);
|
|
203
|
+
case GObject.TYPE_UINT64:
|
|
204
|
+
return ParamSpec.uint64(name, "", "", flags, 0, Number.MAX_SAFE_INTEGER, 0);
|
|
205
|
+
case GObject.type_from_name("gfloat"):
|
|
206
|
+
return ParamSpec.float(name, "", "", flags, MINFLOAT, MAXFLOAT, 0);
|
|
207
|
+
case GObject.TYPE_DOUBLE:
|
|
208
|
+
return ParamSpec.double(name, "", "", flags, Number.MIN_VALUE, Number.MIN_VALUE, 0);
|
|
209
|
+
case GObject.TYPE_JSOBJECT:
|
|
210
|
+
return ParamSpec.jsobject(name, "", "", flags);
|
|
211
|
+
case GObject.TYPE_VARIANT:
|
|
212
|
+
return ParamSpec.object(name, "", "", flags, GLib.Variant);
|
|
213
|
+
case GObject.TYPE_ENUM:
|
|
214
|
+
case GObject.TYPE_INTERFACE:
|
|
215
|
+
case GObject.TYPE_BOXED:
|
|
216
|
+
case GObject.TYPE_POINTER:
|
|
217
|
+
case GObject.TYPE_PARAM:
|
|
218
|
+
case GObject.type_from_name("GType"):
|
|
219
|
+
throw Error(`cannot guess ParamSpec from GType "${type}"`);
|
|
220
|
+
case GObject.TYPE_OBJECT:
|
|
221
|
+
default:
|
|
222
|
+
return ParamSpec.object(name, "", "", flags, type);
|
|
184
223
|
}
|
|
224
|
+
}
|
|
225
|
+
function pspec(name, flags, declaration) {
|
|
185
226
|
if (declaration instanceof ParamSpec)
|
|
186
227
|
return declaration;
|
|
187
228
|
if (declaration === Object || declaration === Function || declaration === Array) {
|
|
@@ -197,7 +238,7 @@ function pspec(name, flags, declaration) {
|
|
|
197
238
|
return ParamSpec.boolean(name, "", "", flags, false);
|
|
198
239
|
}
|
|
199
240
|
if ("$gtype" in declaration) {
|
|
200
|
-
return
|
|
241
|
+
return pspecFromGType(declaration.$gtype, name, flags);
|
|
201
242
|
}
|
|
202
243
|
if (typeof declaration === "function") {
|
|
203
244
|
return declaration(name, flags);
|
package/dist/jsx/index.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export { For } from "./For.js";
|
|
|
4
4
|
export { With } from "./With.js";
|
|
5
5
|
export { This } from "./This.js";
|
|
6
6
|
export { createRoot, getScope, onCleanup, onMount, createContext } from "./scope.js";
|
|
7
|
-
export { type Accessed, type State, type Setter, Accessor, createState, createComputed, createBinding, createConnection, createExternal, } from "./state.js";
|
|
7
|
+
export { type Accessed, type State, type Setter, Accessor, createState, createComputed, createBinding, createConnection, createExternal, createSettings, } from "./state.js";
|
package/dist/jsx/index.js
CHANGED
|
@@ -4,4 +4,4 @@ export { For } from "./For.js";
|
|
|
4
4
|
export { With } from "./With.js";
|
|
5
5
|
export { This } from "./This.js";
|
|
6
6
|
export { createRoot, getScope, onCleanup, onMount, createContext } from "./scope.js";
|
|
7
|
-
export { Accessor, createState, createComputed, createBinding, createConnection, createExternal, } from "./state.js";
|
|
7
|
+
export { Accessor, createState, createComputed, createBinding, createConnection, createExternal, createSettings, } from "./state.js";
|
package/dist/jsx/jsx.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ export type CCProps<Self extends GObject.Object, Props> = {
|
|
|
57
57
|
*/
|
|
58
58
|
css?: string | Accessor<string>;
|
|
59
59
|
} & {
|
|
60
|
-
[K in keyof Props]
|
|
60
|
+
[K in keyof Props]: Accessor<NonNullable<Props[K]>> | Props[K];
|
|
61
61
|
} & {
|
|
62
62
|
[S in keyof Self["$signals"] as S extends `notify::${infer P}` ? `onNotify${Pascalify<P>}` : S extends string ? `on${Pascalify<S>}` : never]?: GObject.SignalCallback<Self, Self["$signals"][S]>;
|
|
63
63
|
};
|
package/dist/jsx/state.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import GObject from "gi://GObject";
|
|
2
2
|
import Gio from "gi://Gio";
|
|
3
|
+
import { type Pascalify } from "../util.js";
|
|
4
|
+
import { type InferVariantRec, type InferVariant } from "../variant.js";
|
|
3
5
|
type SubscribeCallback = () => void;
|
|
4
6
|
type DisposeFunction = () => void;
|
|
5
|
-
type
|
|
7
|
+
type SubscribeFunction = (callback: SubscribeCallback) => DisposeFunction;
|
|
6
8
|
export type Accessed<T> = T extends Accessor<infer V> ? V : never;
|
|
7
9
|
export declare class Accessor<T = unknown> extends Function {
|
|
8
10
|
#private;
|
|
9
11
|
static $gtype: GObject.GType<Accessor>;
|
|
10
12
|
/** @experimental */
|
|
11
13
|
static evaluating?: Set<Accessor<unknown>>;
|
|
12
|
-
constructor(get: () => T, subscribe?:
|
|
14
|
+
constructor(get: () => T, subscribe?: SubscribeFunction);
|
|
13
15
|
/**
|
|
14
16
|
* Subscribe for value changes.
|
|
15
17
|
* @param callback The function to run when the current value changes.
|
|
@@ -49,16 +51,15 @@ export type State<T> = [Accessor<T>, Setter<T>];
|
|
|
49
51
|
*/
|
|
50
52
|
export declare function createState<T>(init: T): State<T>;
|
|
51
53
|
/**
|
|
54
|
+
* Create an `Accessor` which is computed from a list of `Accessor`s.
|
|
55
|
+
*
|
|
52
56
|
* ```ts Example
|
|
53
57
|
* let a: Accessor<number>
|
|
54
58
|
* let b: Accessor<string>
|
|
55
|
-
*
|
|
56
59
|
* const c: Accessor<[number, string]> = createComputed([a, b])
|
|
57
|
-
*
|
|
58
60
|
* const d: Accessor<string> = createComputed([a, b], (a: number, b: string) => `${a} ${b}`)
|
|
59
61
|
* ```
|
|
60
62
|
*
|
|
61
|
-
* Create an `Accessor` which is computed from a list of `Accessor`s.
|
|
62
63
|
* @param deps List of `Accessors`.
|
|
63
64
|
* @param transform An optional transform function.
|
|
64
65
|
* @returns The computed `Accessor`.
|
|
@@ -83,6 +84,8 @@ export declare function createBinding<T extends GObject.Object, P extends keyof
|
|
|
83
84
|
export declare function createBinding<T>(settings: Gio.Settings, key: string): Accessor<T>;
|
|
84
85
|
type ConnectionHandler<O extends GObject.Object, S extends keyof O["$signals"], Return> = O["$signals"][S] extends (...args: any[]) => infer R ? void extends R ? (...args: Parameters<O["$signals"][S]>) => Return : never : never;
|
|
85
86
|
/**
|
|
87
|
+
* Create an `Accessor` which sets up a list of `GObject.Object` signal connections.
|
|
88
|
+
*
|
|
86
89
|
* ```ts Example
|
|
87
90
|
* const value: Accessor<string> = createConnection(
|
|
88
91
|
* "initial value",
|
|
@@ -91,7 +94,6 @@ type ConnectionHandler<O extends GObject.Object, S extends keyof O["$signals"],
|
|
|
91
94
|
* )
|
|
92
95
|
* ```
|
|
93
96
|
*
|
|
94
|
-
* Create an `Accessor` which sets up a list of `GObject.Object` signal connections.
|
|
95
97
|
* @param init The initial value
|
|
96
98
|
* @param signals A list of `GObject.Object`, signal name and callback pairs to connect.
|
|
97
99
|
*/
|
|
@@ -114,4 +116,34 @@ export declare function createConnection<T, O1 extends GObject.Object, S1 extend
|
|
|
114
116
|
* @param producer The producer function which should return a cleanup function
|
|
115
117
|
*/
|
|
116
118
|
export declare function createExternal<T>(init: T, producer: (set: Setter<T>) => DisposeFunction): Accessor<T>;
|
|
119
|
+
/** @experimental */
|
|
120
|
+
type Settings<T extends Record<string, string>> = {
|
|
121
|
+
[K in keyof T as Uncapitalize<Pascalify<K>>]: Accessor<InferVariantRec<T[K]>>;
|
|
122
|
+
} & {
|
|
123
|
+
[K in keyof T as `set${Pascalify<K>}`]: Setter<InferVariant<T[K]>>;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* @experimental
|
|
127
|
+
*
|
|
128
|
+
* Wrap a {@link Gio.Settings} into a collection of setters and accessors.
|
|
129
|
+
*
|
|
130
|
+
* Example:
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* const s = createSettings(settings, {
|
|
134
|
+
* "complex-key": "a{sa{ss}}",
|
|
135
|
+
* "simple-key": "s",
|
|
136
|
+
* })
|
|
137
|
+
*
|
|
138
|
+
* s.complexKey.subscribe(() => {
|
|
139
|
+
* print(s.complexKey.get())
|
|
140
|
+
* })
|
|
141
|
+
*
|
|
142
|
+
* s.setComplexKey((prev) => ({
|
|
143
|
+
* ...prev,
|
|
144
|
+
* key: { nested: "" },
|
|
145
|
+
* }))
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export declare function createSettings<const T extends Record<string, string>>(settings: Gio.Settings, keys: T): Settings<T>;
|
|
117
149
|
export {};
|
package/dist/jsx/state.js
CHANGED
|
@@ -12,7 +12,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
12
12
|
var _Accessor_get, _Accessor_subscribe;
|
|
13
13
|
import GObject from "gi://GObject";
|
|
14
14
|
import Gio from "gi://Gio";
|
|
15
|
-
import
|
|
15
|
+
import GLib from "gi://GLib";
|
|
16
|
+
import { camelify, kebabify } from "../util.js";
|
|
16
17
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
|
17
18
|
export class Accessor extends Function {
|
|
18
19
|
constructor(get, subscribe) {
|
|
@@ -79,16 +80,15 @@ export function createState(init) {
|
|
|
79
80
|
return [new Accessor(() => currentValue, subscribe), set];
|
|
80
81
|
}
|
|
81
82
|
/**
|
|
83
|
+
* Create an `Accessor` which is computed from a list of `Accessor`s.
|
|
84
|
+
*
|
|
82
85
|
* ```ts Example
|
|
83
86
|
* let a: Accessor<number>
|
|
84
87
|
* let b: Accessor<string>
|
|
85
|
-
*
|
|
86
88
|
* const c: Accessor<[number, string]> = createComputed([a, b])
|
|
87
|
-
*
|
|
88
89
|
* const d: Accessor<string> = createComputed([a, b], (a: number, b: string) => `${a} ${b}`)
|
|
89
90
|
* ```
|
|
90
91
|
*
|
|
91
|
-
* Create an `Accessor` which is computed from a list of `Accessor`s.
|
|
92
92
|
* @param deps List of `Accessors`.
|
|
93
93
|
* @param transform An optional transform function.
|
|
94
94
|
* @returns The computed `Accessor`.
|
|
@@ -154,6 +154,8 @@ export function createBinding(object, key) {
|
|
|
154
154
|
return new Accessor(get, subscribe);
|
|
155
155
|
}
|
|
156
156
|
/**
|
|
157
|
+
* Create an `Accessor` which sets up a list of `GObject.Object` signal connections.
|
|
158
|
+
*
|
|
157
159
|
* ```ts Example
|
|
158
160
|
* const value: Accessor<string> = createConnection(
|
|
159
161
|
* "initial value",
|
|
@@ -162,7 +164,6 @@ export function createBinding(object, key) {
|
|
|
162
164
|
* )
|
|
163
165
|
* ```
|
|
164
166
|
*
|
|
165
|
-
* Create an `Accessor` which sets up a list of `GObject.Object` signal connections.
|
|
166
167
|
* @param init The initial value
|
|
167
168
|
* @param signals A list of `GObject.Object`, signal name and callback pairs to connect.
|
|
168
169
|
*/
|
|
@@ -236,3 +237,46 @@ export function createExternal(init, producer) {
|
|
|
236
237
|
};
|
|
237
238
|
return new Accessor(() => currentValue, subscribe);
|
|
238
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* @experimental
|
|
242
|
+
*
|
|
243
|
+
* Wrap a {@link Gio.Settings} into a collection of setters and accessors.
|
|
244
|
+
*
|
|
245
|
+
* Example:
|
|
246
|
+
*
|
|
247
|
+
* ```ts
|
|
248
|
+
* const s = createSettings(settings, {
|
|
249
|
+
* "complex-key": "a{sa{ss}}",
|
|
250
|
+
* "simple-key": "s",
|
|
251
|
+
* })
|
|
252
|
+
*
|
|
253
|
+
* s.complexKey.subscribe(() => {
|
|
254
|
+
* print(s.complexKey.get())
|
|
255
|
+
* })
|
|
256
|
+
*
|
|
257
|
+
* s.setComplexKey((prev) => ({
|
|
258
|
+
* ...prev,
|
|
259
|
+
* key: { nested: "" },
|
|
260
|
+
* }))
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
// TODO: come up with an API
|
|
264
|
+
// - to manually annotate Variant typed setters/getters
|
|
265
|
+
// - to set recursive vs deep unpack per key
|
|
266
|
+
export function createSettings(settings, keys) {
|
|
267
|
+
return Object.fromEntries(Object.entries(keys).flatMap(([key, type]) => [
|
|
268
|
+
[
|
|
269
|
+
camelify(key),
|
|
270
|
+
new Accessor(() => settings.get_value(key).recursiveUnpack(), (callback) => {
|
|
271
|
+
const id = settings.connect(`changed::${key}`, callback);
|
|
272
|
+
return () => settings.disconnect(id);
|
|
273
|
+
}),
|
|
274
|
+
],
|
|
275
|
+
[
|
|
276
|
+
`set${key[0].toUpperCase() + camelify(key).slice(1)}`,
|
|
277
|
+
(v) => {
|
|
278
|
+
settings.set_value(key, new GLib.Variant(type, typeof v === "function" ? v(settings.get_value(key).deepUnpack()) : v));
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
]));
|
|
282
|
+
}
|
package/dist/util.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type GObject from "gi://GObject";
|
|
2
2
|
export declare function kebabify(str: string): string;
|
|
3
|
-
export type Pascalify<S extends string> = S extends `${infer Head}-${infer Tail}` ? `${Capitalize<Head>}${Pascalify<Tail>}` : Capitalize<S>;
|
|
4
3
|
export declare function snakeify(str: string): string;
|
|
4
|
+
export declare function camelify(str: string): string;
|
|
5
|
+
export type Pascalify<S> = S extends `${infer Head}${"-" | "_"}${infer Tail}` ? `${Capitalize<Head>}${Pascalify<Tail>}` : S extends string ? Capitalize<S> : never;
|
|
5
6
|
export type XmlNode = {
|
|
6
7
|
name: string;
|
|
7
8
|
attributes?: Record<string, string>;
|
|
8
9
|
children?: Array<XmlNode>;
|
|
9
10
|
};
|
|
10
11
|
export declare function xml({ name, attributes, children }: XmlNode): string;
|
|
11
|
-
export declare function
|
|
12
|
+
export declare function definePropertyGetter<T extends object>(object: T, prop: Extract<keyof T, string>): void;
|
|
12
13
|
export declare function set(obj: GObject.Object, prop: string, value: any): void;
|
package/dist/util.js
CHANGED
|
@@ -10,6 +10,9 @@ export function snakeify(str) {
|
|
|
10
10
|
.replaceAll("-", "_")
|
|
11
11
|
.toLowerCase();
|
|
12
12
|
}
|
|
13
|
+
export function camelify(str) {
|
|
14
|
+
return str.replace(/[-_](.)/g, (_, char) => char.toUpperCase());
|
|
15
|
+
}
|
|
13
16
|
export function xml({ name, attributes, children }) {
|
|
14
17
|
let builder = `<${name}`;
|
|
15
18
|
const attrs = Object.entries(attributes ?? []);
|
|
@@ -33,8 +36,8 @@ export function xml({ name, attributes, children }) {
|
|
|
33
36
|
// Bindings work over properties in kebab-case because thats the convention of gobject
|
|
34
37
|
// however in js its either snake_case or camelCase
|
|
35
38
|
// also on DBus interfaces its PascalCase by convention
|
|
36
|
-
// so as a workaround we use get_property_name as a
|
|
37
|
-
export function
|
|
39
|
+
// so as a workaround we use get_property_name and only use the property field as a fallback
|
|
40
|
+
export function definePropertyGetter(object, prop) {
|
|
38
41
|
Object.defineProperty(object, `get_${kebabify(prop).replaceAll("-", "_")}`, {
|
|
39
42
|
configurable: false,
|
|
40
43
|
enumerable: true,
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type GLib from "gi://GLib";
|
|
2
|
+
type Variant<S extends string = any> = GLib.Variant<S>;
|
|
3
|
+
type CreateIndexType<Key extends string, Value> = Key extends `s` | `o` | `g` ? {
|
|
4
|
+
[key: string]: Value;
|
|
5
|
+
} : Key extends `n` | `q` | `t` | `d` | `u` | `i` | `x` | `y` ? {
|
|
6
|
+
[key: number]: Value;
|
|
7
|
+
} : never;
|
|
8
|
+
type VariantTypeError<T extends string> = {
|
|
9
|
+
error: true;
|
|
10
|
+
} & T;
|
|
11
|
+
/**
|
|
12
|
+
* Handles the {kv} of a{kv} where k is a basic type and v is any possible variant type string.
|
|
13
|
+
*/
|
|
14
|
+
type $ParseDeepVariantDict<State extends string, Memo extends Record<string, any> = {}> = string extends State ? VariantTypeError<"$ParseDeepVariantDict: 'string' is not a supported type."> : State extends `}${infer State}` ? [Memo, State] : State extends `${infer Key}${''}${infer State}` ? $ParseDeepVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `}${infer State}` ? [CreateIndexType<Key, Value>, State] : VariantTypeError<`$ParseDeepVariantDict encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseDeepVariantValue returned unexpected value for: ${State}`> : VariantTypeError<`$ParseDeepVariantDict encountered an invalid variant string: ${State} (2)`>;
|
|
15
|
+
/**
|
|
16
|
+
* Handles parsing values within a tuple (e.g. (vvv)) where v is any possible variant type string.
|
|
17
|
+
*/
|
|
18
|
+
type $ParseDeepVariantArray<State extends string, Memo extends any[] = []> = string extends State ? VariantTypeError<"$ParseDeepVariantArray: 'string' is not a supported type."> : State extends `)${infer State}` ? [Memo, State] : $ParseDeepVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `${infer _NextValue})${infer _NextState}` ? $ParseDeepVariantArray<State, [...Memo, Value]> : State extends `)${infer State}` ? [[...Memo, Value], State] : VariantTypeError<`1: $ParseDeepVariantArray encountered an invalid variant string: ${State}`> : VariantTypeError<`2: $ParseDeepVariantValue returned unexpected value for: ${State}`>;
|
|
19
|
+
/**
|
|
20
|
+
* Handles parsing {kv} without an 'a' prefix (key-value pair) where k is a basic type
|
|
21
|
+
* and v is any possible variant type string.
|
|
22
|
+
*/
|
|
23
|
+
type $ParseDeepVariantKeyValue<State extends string, Memo extends any[] = []> = string extends State ? VariantTypeError<"$ParseDeepVariantKeyValue: 'string' is not a supported type."> : State extends `}${infer State}` ? [Memo, State] : State extends `${infer Key}${''}${infer State}` ? $ParseDeepVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `}${infer State}` ? [[...Memo, $ParseVariant<Key>, Value], State] : VariantTypeError<`$ParseDeepVariantKeyValue encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseDeepVariantKeyValue returned unexpected value for: ${State}`> : VariantTypeError<`$ParseDeepVariantKeyValue encountered an invalid variant string: ${State} (2)`>;
|
|
24
|
+
/**
|
|
25
|
+
* Handles parsing any variant 'value' or base unit.
|
|
26
|
+
*
|
|
27
|
+
* - ay - Array of bytes (Uint8Array)
|
|
28
|
+
* - a* - Array of type *
|
|
29
|
+
* - a{k*} - Dictionary
|
|
30
|
+
* - {k*} - KeyValue
|
|
31
|
+
* - (**) - tuple
|
|
32
|
+
* - s | o | g - string types
|
|
33
|
+
* - n | q | t | d | u | i | x | y - number types
|
|
34
|
+
* - b - boolean type
|
|
35
|
+
* - v - unknown Variant type
|
|
36
|
+
* - h | ? - unknown types
|
|
37
|
+
*/
|
|
38
|
+
type $ParseDeepVariantValue<State extends string> = string extends State ? unknown : State extends `${`s` | `o` | `g`}${infer State}` ? [string, State] : State extends `${`n` | `q` | `t` | `d` | `u` | `i` | `x` | `y`}${infer State}` ? [number, State] : State extends `b${infer State}` ? [boolean, State] : State extends `v${infer State}` ? [Variant, State] : State extends `${'h' | '?'}${infer State}` ? [unknown, State] : State extends `(${infer State}` ? $ParseDeepVariantArray<State> : State extends `a{${infer State}` ? $ParseDeepVariantDict<State> : State extends `{${infer State}` ? $ParseDeepVariantKeyValue<State> : State extends `ay${infer State}` ? [
|
|
39
|
+
Uint8Array,
|
|
40
|
+
State
|
|
41
|
+
] : State extends `m${infer State}` ? $ParseDeepVariantValue<State> extends [infer Value, `${infer State}`] ? [Value | null, State] : VariantTypeError<`$ParseDeepVariantValue encountered an invalid variant string: ${State} (3)`> : State extends `a${infer State}` ? $ParseDeepVariantValue<State> extends [infer Value, `${infer State}`] ? [
|
|
42
|
+
Value[],
|
|
43
|
+
State
|
|
44
|
+
] : VariantTypeError<`$ParseDeepVariantValue encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseDeepVariantValue encountered an invalid variant string: ${State} (2)`>;
|
|
45
|
+
type $ParseDeepVariant<T extends string> = $ParseDeepVariantValue<T> extends infer Result ? Result extends [infer Value, string] ? Value : Result extends VariantTypeError<any> ? Result : VariantTypeError<"$ParseDeepVariantValue returned unexpected Result"> : VariantTypeError<"$ParseDeepVariantValue returned uninferrable Result">;
|
|
46
|
+
type $ParseRecursiveVariantDict<State extends string, Memo extends Record<string, any> = {}> = string extends State ? VariantTypeError<"$ParseRecursiveVariantDict: 'string' is not a supported type."> : State extends `}${infer State}` ? [Memo, State] : State extends `${infer Key}${''}${infer State}` ? $ParseRecursiveVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `}${infer State}` ? [CreateIndexType<Key, Value>, State] : VariantTypeError<`$ParseRecursiveVariantDict encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseRecursiveVariantValue returned unexpected value for: ${State}`> : VariantTypeError<`$ParseRecursiveVariantDict encountered an invalid variant string: ${State} (2)`>;
|
|
47
|
+
type $ParseRecursiveVariantArray<State extends string, Memo extends any[] = []> = string extends State ? VariantTypeError<"$ParseRecursiveVariantArray: 'string' is not a supported type."> : State extends `)${infer State}` ? [Memo, State] : $ParseRecursiveVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `${infer _NextValue})${infer _NextState}` ? $ParseRecursiveVariantArray<State, [...Memo, Value]> : State extends `)${infer State}` ? [[...Memo, Value], State] : VariantTypeError<`$ParseRecursiveVariantArray encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseRecursiveVariantValue returned unexpected value for: ${State} (2)`>;
|
|
48
|
+
type $ParseRecursiveVariantKeyValue<State extends string, Memo extends any[] = []> = string extends State ? VariantTypeError<"$ParseRecursiveVariantKeyValue: 'string' is not a supported type."> : State extends `}${infer State}` ? [Memo, State] : State extends `${infer Key}${''}${infer State}` ? $ParseRecursiveVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `}${infer State}` ? [[...Memo, Key, Value], State] : VariantTypeError<`$ParseRecursiveVariantKeyValue encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseRecursiveVariantKeyValue returned unexpected value for: ${State}`> : VariantTypeError<`$ParseRecursiveVariantKeyValue encountered an invalid variant string: ${State} (2)`>;
|
|
49
|
+
type $ParseRecursiveVariantValue<State extends string> = string extends State ? unknown : State extends `${`s` | `o` | `g`}${infer State}` ? [string, State] : State extends `${`n` | `q` | `t` | `d` | `u` | `i` | `x` | `y`}${infer State}` ? [number, State] : State extends `b${infer State}` ? [boolean, State] : State extends `v${infer State}` ? [unknown, State] : State extends `${'h' | '?'}${infer State}` ? [unknown, State] : State extends `(${infer State}` ? $ParseRecursiveVariantArray<State> : State extends `a{${infer State}` ? $ParseRecursiveVariantDict<State> : State extends `{${infer State}` ? $ParseRecursiveVariantKeyValue<State> : State extends `ay${infer State}` ? [
|
|
50
|
+
Uint8Array,
|
|
51
|
+
State
|
|
52
|
+
] : State extends `m${infer State}` ? $ParseRecursiveVariantValue<State> extends [infer Value, `${infer State}`] ? [Value | null, State] : VariantTypeError<`$ParseRecursiveVariantValue encountered an invalid variant string: ${State} (3)`> : State extends `a${infer State}` ? $ParseRecursiveVariantValue<State> extends [infer Value, `${infer State}`] ? [
|
|
53
|
+
Value[],
|
|
54
|
+
State
|
|
55
|
+
] : VariantTypeError<`$ParseRecursiveVariantValue encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseRecursiveVariantValue encountered an invalid variant string: ${State} (2)`>;
|
|
56
|
+
type $ParseRecursiveVariant<T extends string> = $ParseRecursiveVariantValue<T> extends infer Result ? Result extends [infer Value, string] ? Value : Result extends VariantTypeError<any> ? Result : never : never;
|
|
57
|
+
type $ParseVariantDict<State extends string, Memo extends Record<string, any> = {}> = string extends State ? VariantTypeError<"$ParseVariantDict: 'string' is not a supported type."> : State extends `}${infer State}` ? [Memo, State] : State extends `${infer Key}${''}${infer State}` ? $ParseVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `}${infer State}` ? [CreateIndexType<Key, Variant<Value extends string ? Value : any>>, State] : VariantTypeError<`$ParseVariantDict encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseVariantValue returned unexpected value for: ${State}`> : VariantTypeError<`$ParseVariantDict encountered an invalid variant string: ${State} (2)`>;
|
|
58
|
+
type $ParseVariantArray<State extends string, Memo extends any[] = []> = string extends State ? VariantTypeError<"$ParseVariantArray: 'string' is not a supported type."> : State extends `)${infer State}` ? [Memo, State] : $ParseVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `${infer _NextValue})${infer _NextState}` ? $ParseVariantArray<State, [...Memo, Variant<Value extends string ? Value : any>]> : State extends `)${infer State}` ? [[...Memo, Variant<Value extends string ? Value : any>], State] : VariantTypeError<`$ParseVariantArray encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseVariantValue returned unexpected value for: ${State} (2)`>;
|
|
59
|
+
type $ParseVariantKeyValue<State extends string, Memo extends any[] = []> = string extends State ? VariantTypeError<"$ParseVariantKeyValue: 'string' is not a supported type."> : State extends `}${infer State}` ? [Memo, State] : State extends `${infer Key}${''}${infer State}` ? $ParseVariantValue<State> extends [infer Value, `${infer State}`] ? State extends `}${infer State}` ? [[...Memo, Variant<Key>, Variant<Value extends string ? Value : any>], State] : VariantTypeError<`$ParseVariantKeyValue encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseVariantKeyValue returned unexpected value for: ${State}`> : VariantTypeError<`$ParseVariantKeyValue encountered an invalid variant string: ${State} (2)`>;
|
|
60
|
+
type $ParseShallowRootVariantValue<State extends string> = string extends State ? unknown : State extends `${`s` | `o` | `g`}${infer State}` ? [string, State] : State extends `${`n` | `q` | `t` | `d` | `u` | `i` | `x` | `y`}${infer State}` ? [number, State] : State extends `b${infer State}` ? [boolean, State] : State extends `v${infer State}` ? [Variant, State] : State extends `h${infer State}` ? [unknown, State] : State extends `?${infer State}` ? [unknown, State] : State extends `(${infer State}` ? $ParseVariantArray<State> : State extends `a{${infer State}` ? $ParseVariantDict<State> : State extends `{${infer State}` ? $ParseVariantKeyValue<State> : State extends `ay${infer State}` ? [
|
|
61
|
+
Uint8Array,
|
|
62
|
+
State
|
|
63
|
+
] : State extends `m${infer State}` ? $ParseVariantValue<State> extends [infer Value, `${infer State}`] ? [Value | null, State] : VariantTypeError<`$ParseShallowRootVariantValue encountered an invalid variant string: ${State} (2)`> : State extends `a${infer State}` ? [
|
|
64
|
+
Variant<State>[],
|
|
65
|
+
State
|
|
66
|
+
] : VariantTypeError<`$ParseShallowRootVariantValue encountered an invalid variant string: ${State} (1)`>;
|
|
67
|
+
type $ParseVariantValue<State extends string> = string extends State ? unknown : State extends `s${infer State}` ? ['s', State] : State extends `o${infer State}` ? ['o', State] : State extends `g${infer State}` ? ['g', State] : State extends `n${infer State}` ? ["n", State] : State extends `q${infer State}` ? ["q", State] : State extends `t${infer State}` ? ["t", State] : State extends `d${infer State}` ? ["d", State] : State extends `u${infer State}` ? ["u", State] : State extends `i${infer State}` ? ["i", State] : State extends `x${infer State}` ? ["x", State] : State extends `y${infer State}` ? ["y", State] : State extends `b${infer State}` ? ['b', State] : State extends `v${infer State}` ? ['v', State] : State extends `h${infer State}` ? ['h', State] : State extends `?${infer State}` ? ['?', State] : State extends `(${infer State}` ? $ParseVariantArray<State> : State extends `a{${infer State}` ? $ParseVariantDict<State> : State extends `{${infer State}` ? $ParseVariantKeyValue<State> : State extends `ay${infer State}` ? [
|
|
68
|
+
Uint8Array,
|
|
69
|
+
State
|
|
70
|
+
] : State extends `m${infer State}` ? $ParseVariantValue<State> extends [infer Value, `${infer State}`] ? [Value | null, State] : VariantTypeError<`$ParseVariantValue encountered an invalid variant string: ${State} (3)`> : State extends `a${infer State}` ? $ParseVariantValue<State> extends [infer Value, `${infer State}`] ? [
|
|
71
|
+
Value[],
|
|
72
|
+
State
|
|
73
|
+
] : VariantTypeError<`$ParseVariantValue encountered an invalid variant string: ${State} (1)`> : VariantTypeError<`$ParseVariantValue encountered an invalid variant string: ${State} (2)`>;
|
|
74
|
+
type $ParseVariant<T extends string> = $ParseShallowRootVariantValue<T> extends infer Result ? Result extends [infer Value, string] ? Value : Result extends VariantTypeError<any> ? Result : never : never;
|
|
75
|
+
export type InferVariant<S extends string> = $ParseDeepVariant<S>;
|
|
76
|
+
export type InferVariantRec<S extends string> = $ParseRecursiveVariant<S>;
|
|
77
|
+
export {};
|
package/dist/variant.js
ADDED