Skip to content

Commit 5a9c651

Browse files
committed
fix!: use undefined for loading state, null for resolved-empty in useViewModelInstance
1 parent 7d9c179 commit 5a9c651

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

example/__tests__/hooks.harness.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function createUseRiveNumberContext(): UseRiveNumberContext {
3434
}
3535

3636
type UseViewModelInstanceContext = {
37-
instance: ViewModelInstance | null;
37+
instance: ViewModelInstance | null | undefined;
3838
age: number | undefined;
3939
};
4040

example/src/exercisers/MenuListExample.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ export default function MenuListExample() {
3939
}
4040

4141
function MenuList({ file }: { file: RiveFile }) {
42-
const { instance } = useViewModelInstance(file, { required: true });
42+
const { instance, error } = useViewModelInstance(file);
43+
44+
if (error) {
45+
console.error(error.message);
46+
return null;
47+
}
48+
49+
if (!instance) {
50+
return <ActivityIndicator size="large" color="#007AFF" />;
51+
}
4352

4453
return <MenuListContent file={file} instance={instance} />;
4554
}

src/hooks/__tests__/useViewModelInstance.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,12 @@ describe('useViewModelInstance - ViewModel source', () => {
362362
expect(result.current.error).toBeNull();
363363
});
364364
});
365+
366+
describe('useViewModelInstance - null source', () => {
367+
it('should return undefined instance when source is null', () => {
368+
const { result } = renderHook(() => useViewModelInstance(null));
369+
370+
expect(result.current.instance).toBeUndefined();
371+
expect(result.current.error).toBeNull();
372+
});
373+
});

src/hooks/useViewModelInstance.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function isRiveFile(source: ViewModelSource | null): source is RiveFile {
9797
}
9898

9999
type CreateInstanceResult = {
100-
instance: ViewModelInstance | null;
100+
instance: ViewModelInstance | null | undefined;
101101
needsDispose: boolean;
102102
error?: string;
103103
};
@@ -110,7 +110,7 @@ function createInstance(
110110
useNew: boolean
111111
): CreateInstanceResult {
112112
if (!source) {
113-
return { instance: null, needsDispose: false };
113+
return { instance: undefined, needsDispose: false };
114114
}
115115

116116
if (isRiveViewRef(source)) {
@@ -179,7 +179,8 @@ function createInstance(
179179
export type UseViewModelInstanceResult =
180180
| { instance: ViewModelInstance; error: null }
181181
| { instance: null; error: Error }
182-
| { instance: null; error: null };
182+
| { instance: null; error: null }
183+
| { instance: undefined; error: null };
183184

184185
/**
185186
* Hook for getting a ViewModelInstance from a RiveFile, ViewModel, or RiveViewRef.
@@ -266,7 +267,9 @@ export type UseViewModelInstanceResult =
266267
export function useViewModelInstance(
267268
source: RiveFile,
268269
params: UseViewModelInstanceFileParams & { required: true }
269-
): { instance: ViewModelInstance; error: null };
270+
):
271+
| { instance: ViewModelInstance; error: null }
272+
| { instance: undefined; error: null };
270273
export function useViewModelInstance(
271274
source: RiveFile | null,
272275
params?: UseViewModelInstanceFileParams
@@ -276,7 +279,9 @@ export function useViewModelInstance(
276279
export function useViewModelInstance(
277280
source: ViewModel,
278281
params: UseViewModelInstanceViewModelParams & { required: true }
279-
): { instance: ViewModelInstance; error: null };
282+
):
283+
| { instance: ViewModelInstance; error: null }
284+
| { instance: undefined; error: null };
280285
export function useViewModelInstance(
281286
source: ViewModel | null,
282287
params?: UseViewModelInstanceViewModelParams
@@ -286,7 +291,9 @@ export function useViewModelInstance(
286291
export function useViewModelInstance(
287292
source: RiveViewRef,
288293
params: UseViewModelInstanceRefParams & { required: true }
289-
): { instance: ViewModelInstance; error: null };
294+
):
295+
| { instance: ViewModelInstance; error: null }
296+
| { instance: undefined; error: null };
290297
export function useViewModelInstance(
291298
source: RiveViewRef | null,
292299
params?: UseViewModelInstanceRefParams
@@ -315,7 +322,7 @@ export function useViewModelInstance(
315322
const onInit = params?.onInit;
316323

317324
const prevInstanceRef = useRef<{
318-
instance: ViewModelInstance | null;
325+
instance: ViewModelInstance | null | undefined;
319326
needsDispose: boolean;
320327
} | null>(null);
321328

@@ -372,5 +379,8 @@ export function useViewModelInstance(
372379
if (result.instance) {
373380
return { instance: result.instance, error: null };
374381
}
382+
if (result.instance === undefined) {
383+
return { instance: undefined, error: null };
384+
}
375385
return { instance: null, error };
376386
}

0 commit comments

Comments
 (0)