Add Component.useSignalValues
Some checks failed
Lint / lint (push) Failing after 9s

This commit is contained in:
Julien Valverdé
2026-01-02 21:17:30 +01:00
parent dc2f4d81d6
commit 703b0f13d4

View File

@@ -80,4 +80,33 @@ export const useSignal = <T extends Godot.Node<Godot.NodePathMap>, A extends use
const callable = Godot.Callable.create(ref.current, f) const callable = Godot.Callable.create(ref.current, f)
signal.connect(callable) signal.connect(callable)
return () => { signal.disconnect(callable) } return () => { signal.disconnect(callable) }
}, [ref.current]) }, [ref.current, name])
export declare namespace useSignalValues {
export type SignalNames<T extends Godot.Node<Godot.NodePathMap>> = useSignal.SignalNames<T>
export type SignalValues<T extends Godot.Node<Godot.NodePathMap>, N extends useSignal.SignalNames<T>> = (
T[N] extends Godot.Signal<infer F>
? Parameters<F>
: never
)
}
export const useSignalValues = <T extends Godot.Node<Godot.NodePathMap>, A extends useSignal.SignalNames<T>>(
ref: React.RefObject<T | null>,
name: A,
initialValue:
| useSignalValues.SignalValues<NoInfer<T>, NoInfer<A>>
| (() => useSignalValues.SignalValues<NoInfer<T>, NoInfer<A>>),
): useSignalValues.SignalValues<T, A> => {
const [values, setValues] = React.useState(initialValue)
React.useEffect(() => {
if (!ref.current) return
const signal = ref.current[name] as Godot.Signal
const callable = Godot.Callable.create(setValues)
signal.connect(callable)
return () => { signal.disconnect(callable) }
}, [ref.current, name])
return values
}