@@ -7,13 +7,13 @@ import {
77 Switch ,
88} from 'react-native' ;
99import { useEffect , useMemo , useState } from 'react' ;
10+ import { useQuery } from '@tanstack/react-query' ;
1011import Animated , {
11- runOnUI ,
1212 useSharedValue ,
1313 useAnimatedStyle ,
1414 type SharedValue ,
1515} from 'react-native-reanimated' ;
16- import { NitroModules } from 'react-native-nitro-modules ' ;
16+ import { scheduleOnUI } from 'react-native-worklets ' ;
1717import {
1818 Fit ,
1919 RiveView ,
@@ -29,8 +29,7 @@ declare global {
2929 var __callMicrotasks : ( ) => void ;
3030}
3131
32- // eslint-disable-next-line @typescript-eslint/no-deprecated
33- installWorkletDispatcher ( runOnUI ) ;
32+ installWorkletDispatcher ( scheduleOnUI ) ;
3433
3534function useRiveNumberListener (
3635 property : ViewModelNumberProperty | undefined ,
@@ -41,19 +40,16 @@ function useRiveNumberListener(
4140 if ( ! property ) return ;
4241
4342 if ( useUIThread ) {
44- const boxedProperty = NitroModules . box ( property ) ;
4543 const sv = sharedValue ;
4644
47- // eslint-disable-next-line @typescript-eslint/no-deprecated
48- runOnUI ( ( ) => {
45+ scheduleOnUI ( ( ) => {
4946 'worklet' ;
50- const prop = boxedProperty . unbox ( ) ;
51- prop . addListener ( ( value : number ) => {
47+ property . addListener ( ( value : number ) => {
5248 'worklet' ;
5349 sv . value = value ;
5450 global . __callMicrotasks ( ) ;
5551 } ) ;
56- } ) ( ) ;
52+ } ) ;
5753
5854 return ( ) => {
5955 property . removeListeners ( ) ;
@@ -89,27 +85,31 @@ export default function RiveToReactNativeExample() {
8985}
9086
9187function WithViewModelSetup ( { file } : { file : RiveFile } ) {
92- // eslint-disable-next-line @typescript-eslint/no-deprecated -- replaced with async API in next PR
93- const viewModel = useMemo ( ( ) => file . defaultArtboardViewModel ( ) , [ file ] ) ;
94- const instance = useMemo (
95- // eslint-disable-next-line @typescript-eslint/no-deprecated
96- ( ) => viewModel ?. createDefaultInstance ( ) ,
97- [ viewModel ]
98- ) ;
9988 const [ useUIThread , setUseUIThread ] = useState ( true ) ;
10089
101- if ( ! instance || ! viewModel ) {
90+ const { data : instance , error } = useQuery ( {
91+ queryKey : [ 'bouncing-ball-instance' , file ] ,
92+ queryFn : async ( ) => {
93+ const vm = await file . defaultArtboardViewModelAsync ( ) ;
94+ if ( ! vm ) throw new Error ( 'No view model found.' ) ;
95+ const inst = await vm . createDefaultInstanceAsync ( ) ;
96+ if ( ! inst ) throw new Error ( 'Failed to create view model instance' ) ;
97+ return inst ;
98+ } ,
99+ } ) ;
100+
101+ if ( error ) {
102102 return (
103103 < View style = { styles . errorContainer } >
104- < Text style = { styles . errorText } >
105- { ! viewModel
106- ? 'No view model found.'
107- : 'Failed to create view model instance' }
108- </ Text >
104+ < Text style = { styles . errorText } > { error . message } </ Text >
109105 </ View >
110106 ) ;
111107 }
112108
109+ if ( ! instance ) {
110+ return < ActivityIndicator size = "large" color = "#0000ff" /> ;
111+ }
112+
113113 return (
114114 < BouncingBallTracker
115115 instance = { instance }
0 commit comments