1
1
//@flow
2
2
import {
3
3
globalRegistry ,
4
- WebGLTextureLoaderAsyncHashCache
4
+ WebGLTextureLoaderAsyncHashCache ,
5
5
} from "webgltexture-loader" ;
6
- import { Image } from "react-native" ;
7
- import * as FileSystem from "expo-file-system" ;
6
+ import * as AssetUtils from "expo-asset-utils" ;
8
7
import { Asset } from "expo-asset" ;
9
8
10
- import md5 from "./md5" ;
11
-
12
9
const neverEnding = new Promise ( ( ) => { } ) ;
13
10
14
- type AssetModel = {
15
- width : number ,
16
- height : number ,
17
- uri : string ,
18
- localUri : string
19
- } ;
20
-
21
- const hash = ( module : number | { uri : string } ) =>
22
- typeof module === "number" ? module : module . uri ;
23
-
24
11
const localAsset = ( module : number ) => {
25
12
const asset = Asset . fromModule ( module ) ;
26
13
return asset . downloadAsync ( ) . then ( ( ) => asset ) ;
27
14
} ;
28
15
29
- const remoteAssetCache = { } ;
30
-
31
- const remoteAsset = ( uri : string ) => {
32
- const i = uri . lastIndexOf ( "." ) ;
33
- const ext = i !== - 1 ? uri . slice ( i ) : ".jpg" ;
34
- const key = md5 ( uri ) ;
35
- if ( key in remoteAssetCache ) {
36
- return Promise . resolve ( remoteAssetCache [ key ] ) ;
37
- }
38
- const promise = Promise . all ( [
39
- new Promise ( ( success , failure ) =>
40
- Image . getSize ( uri , ( width , height ) => success ( { width, height } ) , failure )
41
- ) ,
42
- FileSystem . downloadAsync (
43
- uri ,
44
- FileSystem . documentDirectory + `ExponentAsset-${ key } ${ ext } ` ,
45
- {
46
- cache : true
47
- }
48
- )
49
- ] ) . then ( ( [ size , asset ] ) => ( { ...size , uri, localUri : asset . uri } ) ) ;
50
- remoteAssetCache [ key ] = promise ;
51
- return promise ;
16
+ type AssetModel = {
17
+ width : number ,
18
+ height : number ,
19
+ uri : string ,
20
+ localUri : string ,
52
21
} ;
53
22
54
- const localFile = ( uri : string ) => {
55
- const i = uri . lastIndexOf ( "." ) ;
56
- const ext = i !== - 1 ? uri . slice ( i ) : ".jpg" ;
57
- const key = md5 ( uri ) ;
58
- if ( key in remoteAssetCache ) {
59
- return Promise . resolve ( remoteAssetCache [ key ] ) ;
60
- }
61
- const promise = new Promise ( ( success , failure ) =>
62
- Image . getSize ( uri , ( width , height ) => success ( { width, height } ) , failure )
63
- ) . then ( size => ( { ...size , uri, localUri : uri } ) ) ;
64
- remoteAssetCache [ key ] = promise ;
65
- return promise ;
66
- } ;
23
+ type M = number | { uri : string } | AssetModel ;
67
24
68
- export const loadAsset = (
69
- module : number | { uri : string }
70
- ) : Promise < AssetModel > =>
25
+ export const loadAsset = ( module : M ) : Promise < AssetModel > =>
71
26
typeof module === "number"
72
27
? localAsset ( module )
73
- : module . uri . startsWith ( "file:" ) ||
74
- module . uri . startsWith ( "data:" ) ||
75
- module . uri . startsWith ( "asset:" ) || // All local paths in Android Expo standalone app
76
- module . uri . startsWith ( "assets-library:" ) || // CameraRoll.getPhotos iOS
77
- module . uri . startsWith ( "content:" ) || // CameraRoll.getPhotos Android
78
- module . uri . startsWith ( "/" ) // Expo.takeSnapshotAsync in DEV in Expo 31
79
- ? localFile ( module . uri )
80
- : remoteAsset ( module . uri ) ;
28
+ : module . localUri
29
+ ? // $FlowFixMe
30
+ Promise . resolve ( module )
31
+ : AssetUtils . resolveAsync ( module . uri ) ;
81
32
82
- class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache <
83
- number | { uri : string }
84
- > {
33
+ class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache < M > {
85
34
objIds : WeakMap < WebGLTexture , number > = new WeakMap ( ) ;
86
35
87
36
canLoad ( input : any ) {
@@ -101,12 +50,11 @@ class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<
101
50
const dispose = ( ) => {
102
51
disposed = true ;
103
52
} ;
104
- const promise = loadAsset ( module ) . then ( asset => {
53
+ const promise = loadAsset ( module ) . then ( ( asset ) => {
105
54
if ( disposed ) return neverEnding ;
106
55
const { width, height } = asset ;
107
56
const texture = gl . createTexture ( ) ;
108
57
gl . bindTexture ( gl . TEXTURE_2D , texture ) ;
109
- // $FlowFixMe
110
58
gl . texImage2D (
111
59
gl . TEXTURE_2D ,
112
60
0 ,
0 commit comments