nixos-configs/modules/ags/gtk4/widget/astalify/bindings.ts

28 lines
692 B
TypeScript
Raw Normal View History

2025-01-14 10:12:20 -05:00
import { Variable } from 'astal';
import { Binding } from 'astal/binding';
export const mergeBindings = <Value = unknown>(
array: (Value | Binding<Value> | Binding<Value[]>)[],
): Value[] | Binding<Value[]> => {
const getValues = (args: Value[]) => {
let i = 0;
return array.map((value) => value instanceof Binding ?
args[i++] :
value);
};
const bindings = array.filter((i) => i instanceof Binding);
if (bindings.length === 0) {
return array as Value[];
}
if (bindings.length === 1) {
return (bindings[0] as Binding<Value[]>).as(getValues);
}
return Variable.derive(bindings, getValues)();
};