Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework expo loader for better support #21

Merged
merged 3 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/webgltexture-loader-expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"react-native": "*"
},
"dependencies": {
"expo-asset-utils": "^1.2.0",
"webgltexture-loader": "^1.0.0"
}
}
82 changes: 15 additions & 67 deletions packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,36 @@
//@flow
import {
globalRegistry,
WebGLTextureLoaderAsyncHashCache
WebGLTextureLoaderAsyncHashCache,
} from "webgltexture-loader";
import { Image } from "react-native";
import * as FileSystem from "expo-file-system";
import * as AssetUtils from "expo-asset-utils";
import { Asset } from "expo-asset";

import md5 from "./md5";

const neverEnding = new Promise(() => {});

type AssetModel = {
width: number,
height: number,
uri: string,
localUri: string
};

const hash = (module: number | { uri: string }) =>
typeof module === "number" ? module : module.uri;

const localAsset = (module: number) => {
const asset = Asset.fromModule(module);
return asset.downloadAsync().then(() => asset);
};

const remoteAssetCache = {};

const remoteAsset = (uri: string) => {
const i = uri.lastIndexOf(".");
const ext = i !== -1 ? uri.slice(i) : ".jpg";
const key = md5(uri);
if (key in remoteAssetCache) {
return Promise.resolve(remoteAssetCache[key]);
}
const promise = Promise.all([
new Promise((success, failure) =>
Image.getSize(uri, (width, height) => success({ width, height }), failure)
),
FileSystem.downloadAsync(
uri,
FileSystem.documentDirectory + `ExponentAsset-${key}${ext}`,
{
cache: true
}
)
]).then(([size, asset]) => ({ ...size, uri, localUri: asset.uri }));
remoteAssetCache[key] = promise;
return promise;
type AssetModel = {
width: number,
height: number,
uri: string,
localUri: string,
};

const localFile = (uri: string) => {
const i = uri.lastIndexOf(".");
const ext = i !== -1 ? uri.slice(i) : ".jpg";
const key = md5(uri);
if (key in remoteAssetCache) {
return Promise.resolve(remoteAssetCache[key]);
}
const promise = new Promise((success, failure) =>
Image.getSize(uri, (width, height) => success({ width, height }), failure)
).then(size => ({ ...size, uri, localUri: uri }));
remoteAssetCache[key] = promise;
return promise;
};
type M = number | { uri: string } | AssetModel;

export const loadAsset = (
module: number | { uri: string }
): Promise<AssetModel> =>
export const loadAsset = (module: M): Promise<AssetModel> =>
typeof module === "number"
? localAsset(module)
: module.uri.startsWith("file:") ||
module.uri.startsWith("data:") ||
module.uri.startsWith("asset:") || // All local paths in Android Expo standalone app
module.uri.startsWith("assets-library:") || // CameraRoll.getPhotos iOS
module.uri.startsWith("content:") || // CameraRoll.getPhotos Android
module.uri.startsWith("/") // Expo.takeSnapshotAsync in DEV in Expo 31
? localFile(module.uri)
: remoteAsset(module.uri);
: module.localUri
? // $FlowFixMe
Promise.resolve(module)
: AssetUtils.resolveAsync(module.uri);

class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<
number | { uri: string }
> {
class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<M> {
objIds: WeakMap<WebGLTexture, number> = new WeakMap();

canLoad(input: any) {
Expand All @@ -101,12 +50,11 @@ class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache<
const dispose = () => {
disposed = true;
};
const promise = loadAsset(module).then(asset => {
const promise = loadAsset(module).then((asset) => {
if (disposed) return neverEnding;
const { width, height } = asset;
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// $FlowFixMe
gl.texImage2D(
gl.TEXTURE_2D,
0,
Expand Down
191 changes: 0 additions & 191 deletions packages/webgltexture-loader-expo/src/md5.js

This file was deleted.

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3636,6 +3636,11 @@ expect@^28.1.0:
jest-message-util "^28.1.0"
jest-util "^28.1.0"

expo-asset-utils@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/expo-asset-utils/-/expo-asset-utils-1.2.0.tgz#df52f8e8c836c343ad713a0044db79be69cfe64b"
integrity sha512-06zVi5aXzyMq7SFiawxu2FUbpbVlxnE9W44cG4K5HyhLaqyRqss+o5MZMEGn8Ibd+008UiK7yCPy/bSpx2hVag==

extend-shallow@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f"
Expand Down