1- import React , { useState } from "react" ;
1+ import React , { useEffect , useState } from "react" ;
22import {
33 Alert ,
44 Button ,
@@ -18,13 +18,35 @@ import {
1818 withTiming ,
1919} from "react-native-reanimated" ;
2020
21+ const logMemoryStats = ( ) => {
22+ "worklet" ;
23+ if (
24+ typeof HermesInternal !== "undefined" &&
25+ // @ts -expect-error - HermesInternal is available in Hermes runtime
26+ HermesInternal ?. getInstrumentedStats
27+ ) {
28+ // // @ts -expect-error - HermesInternal is available in Hermes runtime
29+ // const stats = HermesInternal.getInstrumentedStats();
30+ // const externalMB = (stats.js_externalBytes / (1024 * 1024)).toFixed(2);
31+ // const totalAllocatedMB = (
32+ // stats.js_totalAllocatedBytes /
33+ // (1024 * 1024)
34+ // ).toFixed(2);
35+ // const heapSizeMB = (stats.js_heapSize / (1024 * 1024)).toFixed(2);
36+ // console.log(
37+ // `External: ${externalMB} MB, Total Allocated: ${totalAllocatedMB} MB, Heap Size: ${heapSizeMB} MB`
38+ // );
39+ }
40+ } ;
41+
2142const drawFrame = (
2243 surface : SharedValue < SkSurface | null > ,
2344 image : SharedValue < SkImage | null > ,
2445 x : SharedValue < number > ,
2546 pixelRatio : number
2647) => {
2748 "worklet" ;
49+ logMemoryStats ( ) ;
2850 if ( ! surface . value ) {
2951 Alert . alert ( "surface is null" ) ;
3052 return false ;
@@ -56,11 +78,16 @@ function continueBlink(
5678 blinkCnt : SharedValue < number > ,
5779 isLeft : SharedValue < boolean > ,
5880 pixelRatio : number ,
59- revert : ( d : string ) => void
81+ revert : ( d : string ) => void ,
82+ shouldContinue : SharedValue < boolean >
6083) {
6184 "worklet" ;
85+ if ( ! shouldContinue . value ) {
86+ return ;
87+ }
6288 blinkCnt . value ++ ;
6389 looper . value = withTiming ( 1 - looper . value , { duration : 10 } , ( ) => {
90+ if ( ! shouldContinue . value ) return ;
6491 if ( ! drawFrame ( surface , image , x , pixelRatio ) ) return ;
6592 if ( blinkCnt . value < 20 ) {
6693 continueBlink (
@@ -71,13 +98,16 @@ function continueBlink(
7198 blinkCnt ,
7299 isLeft ,
73100 pixelRatio ,
74- revert
101+ revert ,
102+ shouldContinue
75103 ) ;
76104 } else {
105+ if ( ! shouldContinue . value ) return ;
77106 runOnJS ( revert ) ( isLeft . value ? "right" : "left" ) ;
78107 isLeft . value = ! isLeft . value ;
79108 blinkCnt . value = 0 ;
80109 x . value = withTiming ( 400 - x . value , { duration : 200 } , ( ) => {
110+ if ( ! shouldContinue . value ) return ;
81111 continueBlink (
82112 surface ,
83113 image ,
@@ -86,7 +116,8 @@ function continueBlink(
86116 blinkCnt ,
87117 isLeft ,
88118 pixelRatio ,
89- revert
119+ revert ,
120+ shouldContinue
90121 ) ;
91122 } ) ;
92123 }
@@ -101,9 +132,11 @@ const startBlink = (
101132 blinkCnt : SharedValue < number > ,
102133 isLeft : SharedValue < boolean > ,
103134 pixelRatio : number ,
104- revert : ( d : string ) => void
135+ revert : ( d : string ) => void ,
136+ shouldContinue : SharedValue < boolean >
105137) => {
106138 "worklet" ;
139+ if ( ! shouldContinue . value ) return ;
107140 blinkCnt . value = 0 ;
108141 if ( drawFrame ( surface , image , x , pixelRatio ) ) {
109142 continueBlink (
@@ -114,7 +147,8 @@ const startBlink = (
114147 blinkCnt ,
115148 isLeft ,
116149 pixelRatio ,
117- revert
150+ revert ,
151+ shouldContinue
118152 ) ;
119153 }
120154} ;
@@ -127,7 +161,8 @@ const startAnimation = (
127161 blinkCnt : SharedValue < number > ,
128162 isLeft : SharedValue < boolean > ,
129163 pixelRatio : number ,
130- revert : ( d : string ) => void
164+ revert : ( d : string ) => void ,
165+ shouldContinue : SharedValue < boolean >
131166) => {
132167 "worklet" ;
133168 if ( ! surface . value ) {
@@ -136,7 +171,17 @@ const startAnimation = (
136171 pixelRatio * 400
137172 ) ;
138173 }
139- startBlink ( surface , image , x , looper , blinkCnt , isLeft , pixelRatio , revert ) ;
174+ startBlink (
175+ surface ,
176+ image ,
177+ x ,
178+ looper ,
179+ blinkCnt ,
180+ isLeft ,
181+ pixelRatio ,
182+ revert ,
183+ shouldContinue
184+ ) ;
140185} ;
141186
142187export const StressTest4 = ( ) => {
@@ -149,10 +194,12 @@ export const StressTest4 = () => {
149194 const [ direction , setDirection ] = useState ( "left" ) ;
150195 const isLeft = useSharedValue ( true ) ;
151196 const blinkCnt = useSharedValue ( 0 ) ;
197+ const shouldContinue = useSharedValue ( true ) ;
152198
153199 const imageX = useDerivedValue ( ( ) => - x . value ) ;
154200
155201 const handleStart = ( ) => {
202+ shouldContinue . value = true ;
156203 runOnUI ( startAnimation ) (
157204 surface ,
158205 image ,
@@ -161,10 +208,18 @@ export const StressTest4 = () => {
161208 blinkCnt ,
162209 isLeft ,
163210 pixelRatio ,
164- setDirection
211+ setDirection ,
212+ shouldContinue
165213 ) ;
166214 } ;
167215
216+ useEffect ( ( ) => {
217+ // Stop animation on unmount
218+ return ( ) => {
219+ shouldContinue . value = false ;
220+ } ;
221+ } , [ shouldContinue ] ) ;
222+
168223 return (
169224 < SafeAreaView style = { styles . container } >
170225 < Text > Direction: { direction } </ Text >
0 commit comments