Skip to content

Commit c574ca5

Browse files
committed
Use callable schemas for initial value
1 parent 61ab04f commit c574ca5

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

packages/typegpu-react/src/use-uniform-value.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ import type { ValidateUniformSchema } from 'typegpu';
55

66
interface UniformValue<TSchema, TValue extends d.Infer<TSchema>> {
77
schema: TSchema;
8-
value: TValue | undefined;
8+
value: TValue;
99
readonly $: d.InferGPU<TSchema>;
1010
}
1111

12+
function initialValueFromSchema<T extends d.AnyWgslData>(
13+
schema: ValidateUniformSchema<T>,
14+
): d.Infer<T> {
15+
if (typeof schema !== 'function') {
16+
throw new Error('Cannot use a non-callable schema with `useUniformValue`');
17+
}
18+
19+
return schema() as d.Infer<T>;
20+
}
21+
1222
export function useUniformValue<
1323
TSchema extends d.AnyWgslData,
1424
TValue extends d.Infer<TSchema>,
@@ -40,17 +50,15 @@ export function useUniformValue<
4050

4151
// biome-ignore lint/correctness/useExhaustiveDependencies: This value needs to be stable
4252
const uniformValue = useMemo(() => {
43-
let currentValue = initialValue;
53+
let currentValue = initialValue ?? initialValueFromSchema(schema) as TValue;
4454
return {
4555
schema,
4656
get value() {
4757
return currentValue;
4858
},
49-
set value(newValue: TValue | undefined) {
59+
set value(newValue: TValue) {
5060
currentValue = newValue;
51-
if (newValue !== undefined) {
52-
uniformBuffer.write(newValue);
53-
}
61+
uniformBuffer.write(newValue);
5462
},
5563
get $() {
5664
return uniformBuffer.$;

0 commit comments

Comments
 (0)