File tree Expand file tree Collapse file tree
packages/react-native-nitro-image/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { type AsyncImageSource , isHybridObject } from "./AsyncImageSource" ;
2+ import type { Image } from "./specs/Image.nitro" ;
3+ import type { ImageLoader } from "./specs/ImageLoader.nitro" ;
4+
5+ let counter = 0 ;
6+ export function markHybridObject (
7+ object : Image | ImageLoader ,
8+ source : AsyncImageSource ,
9+ ) : typeof object {
10+ if ( isHybridObject ( source ) ) {
11+ // `source` is a HybridObject - to avoid recursion, we just set it to an incrementing counter.
12+ Object . defineProperty ( object , "__source" , {
13+ enumerable : true ,
14+ configurable : true ,
15+ value : counter ,
16+ } ) ;
17+ counter ++ ;
18+ } else {
19+ // `source` is just an input object, we can use it to tag the Image properly
20+ Object . defineProperty ( object , "__source" , {
21+ enumerable : true ,
22+ configurable : true ,
23+ value : source ,
24+ } ) ;
25+ }
26+ return object ;
27+ }
Original file line number Diff line number Diff line change 11import { useEffect , useState } from "react" ;
22import { type AsyncImageSource , isHybridObject } from "./AsyncImageSource" ;
33import { loadImage } from "./loadImage" ;
4+ import { markHybridObject } from "./markHybridObject" ;
45import type { Image } from "./specs/Image.nitro" ;
56
67type Result =
@@ -41,11 +42,8 @@ export function useImage(source: AsyncImageSource): Result {
4142 // 1. Create the Image/ImageLoader instance
4243 const result = await loadImage ( source ) ;
4344 // 2. Add `__source` as a property on the JS side so React diffs properly
44- Object . defineProperty ( result , "__source" , {
45- enumerable : true ,
46- configurable : true ,
47- value : source ,
48- } ) ;
45+ markHybridObject ( result , source ) ;
46+ // 3. Update the state
4947 setImage ( { image : result , error : undefined } ) ;
5048 } catch ( e ) {
5149 const error = e instanceof Error ? e : new Error ( `${ e } ` ) ;
Original file line number Diff line number Diff line change 11import { useMemo } from "react" ;
22import { type AsyncImageSource , isHybridObject } from "./AsyncImageSource" ;
33import { createImageLoader } from "./createImageLoader" ;
4+ import { markHybridObject } from "./markHybridObject" ;
45import type { Image } from "./specs/Image.nitro" ;
56import type { ImageLoader } from "./specs/ImageLoader.nitro" ;
67
@@ -12,11 +13,7 @@ export function useImageLoader(
1213 // 1. Create the Image/ImageLoader instance
1314 const loader = createImageLoader ( source ) ;
1415 // 2. Add `__source` as a property on the JS side so React diffs properly
15- Object . defineProperty ( loader , "__source" , {
16- enumerable : true ,
17- configurable : true ,
18- value : source ,
19- } ) ;
16+ markHybridObject ( loader , source ) ;
2017 // 3. Return it
2118 return loader ;
2219 } , [ isHybridObject ( source ) ? source : JSON . stringify ( source ) ] ) ;
You can’t perform that action at this time.
0 commit comments