Skip to content

Commit b54c52c

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

4 files changed

Lines changed: 31 additions & 9 deletions

File tree

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: 11 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,7 @@ export type UseViewModelInstanceResult =
266267
export function useViewModelInstance(
267268
source: RiveFile,
268269
params: UseViewModelInstanceFileParams & { required: true }
269-
): { instance: ViewModelInstance; error: null };
270+
): { instance: ViewModelInstance; error: null } | { instance: undefined; error: null };
270271
export function useViewModelInstance(
271272
source: RiveFile | null,
272273
params?: UseViewModelInstanceFileParams
@@ -276,7 +277,7 @@ export function useViewModelInstance(
276277
export function useViewModelInstance(
277278
source: ViewModel,
278279
params: UseViewModelInstanceViewModelParams & { required: true }
279-
): { instance: ViewModelInstance; error: null };
280+
): { instance: ViewModelInstance; error: null } | { instance: undefined; error: null };
280281
export function useViewModelInstance(
281282
source: ViewModel | null,
282283
params?: UseViewModelInstanceViewModelParams
@@ -286,7 +287,7 @@ export function useViewModelInstance(
286287
export function useViewModelInstance(
287288
source: RiveViewRef,
288289
params: UseViewModelInstanceRefParams & { required: true }
289-
): { instance: ViewModelInstance; error: null };
290+
): { instance: ViewModelInstance; error: null } | { instance: undefined; error: null };
290291
export function useViewModelInstance(
291292
source: RiveViewRef | null,
292293
params?: UseViewModelInstanceRefParams
@@ -315,7 +316,7 @@ export function useViewModelInstance(
315316
const onInit = params?.onInit;
316317

317318
const prevInstanceRef = useRef<{
318-
instance: ViewModelInstance | null;
319+
instance: ViewModelInstance | null | undefined;
319320
needsDispose: boolean;
320321
} | null>(null);
321322

@@ -372,5 +373,8 @@ export function useViewModelInstance(
372373
if (result.instance) {
373374
return { instance: result.instance, error: null };
374375
}
376+
if (result.instance === undefined) {
377+
return { instance: undefined, error: null };
378+
}
375379
return { instance: null, error };
376380
}

0 commit comments

Comments
 (0)