Skip to content

Commit 09f4cba

Browse files
authored
Rework expo loader for better support (#21)
* Rework expo loader for better support * Fixes import
1 parent 1f70187 commit 09f4cba

File tree

4 files changed

+21
-258
lines changed

4 files changed

+21
-258
lines changed

packages/webgltexture-loader-expo/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"react-native": "*"
1616
},
1717
"dependencies": {
18+
"expo-asset-utils": "^1.2.0",
1819
"webgltexture-loader": "^1.0.0"
1920
}
2021
}

packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js

+15-67
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,36 @@
11
//@flow
22
import {
33
globalRegistry,
4-
WebGLTextureLoaderAsyncHashCache
4+
WebGLTextureLoaderAsyncHashCache,
55
} 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";
87
import { Asset } from "expo-asset";
98

10-
import md5 from "./md5";
11-
129
const neverEnding = new Promise(() => {});
1310

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-
2411
const localAsset = (module: number) => {
2512
const asset = Asset.fromModule(module);
2613
return asset.downloadAsync().then(() => asset);
2714
};
2815

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,
5221
};
5322

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;
6724

68-
export const loadAsset = (
69-
module: number | { uri: string }
70-
): Promise<AssetModel> =>
25+
export const loadAsset = (module: M): Promise<AssetModel> =>
7126
typeof module === "number"
7227
? 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);
8132

82-
class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<
83-
number | { uri: string }
84-
> {
33+
class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<M> {
8534
objIds: WeakMap<WebGLTexture, number> = new WeakMap();
8635

8736
canLoad(input: any) {
@@ -101,12 +50,11 @@ class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<
10150
const dispose = () => {
10251
disposed = true;
10352
};
104-
const promise = loadAsset(module).then(asset => {
53+
const promise = loadAsset(module).then((asset) => {
10554
if (disposed) return neverEnding;
10655
const { width, height } = asset;
10756
const texture = gl.createTexture();
10857
gl.bindTexture(gl.TEXTURE_2D, texture);
109-
// $FlowFixMe
11058
gl.texImage2D(
11159
gl.TEXTURE_2D,
11260
0,

packages/webgltexture-loader-expo/src/md5.js

-191
This file was deleted.

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3636,6 +3636,11 @@ expect@^28.1.0:
36363636
jest-message-util "^28.1.0"
36373637
jest-util "^28.1.0"
36383638

3639+
expo-asset-utils@^1.2.0:
3640+
version "1.2.0"
3641+
resolved "https://registry.yarnpkg.com/expo-asset-utils/-/expo-asset-utils-1.2.0.tgz#df52f8e8c836c343ad713a0044db79be69cfe64b"
3642+
integrity sha512-06zVi5aXzyMq7SFiawxu2FUbpbVlxnE9W44cG4K5HyhLaqyRqss+o5MZMEGn8Ibd+008UiK7yCPy/bSpx2hVag==
3643+
36393644
extend-shallow@^2.0.1:
36403645
version "2.0.1"
36413646
resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f"

0 commit comments

Comments
 (0)