<commit_before>//@flow
import { NativeModules, Image } from "react-native";
import Expo from "expo";
import { TextureLoader } from "gl-react";
import type { DisposablePromise } from "gl-react/lib/helpers/disposable";
import md5 from "./md5";

type Asset = {
  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 = Expo.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)
    ),
    NativeModules.ExponentFileSystem.downloadAsync(
      uri,
      `ExponentAsset-${key}${ext}`,
      {
        cache: true
      }
    )
  ]).then(([size, asset]) => ({ ...size, uri, localUri: asset.uri }));
  remoteAssetCache[key] = promise;
  return promise;
};

export const loadAsset = (module: number | { uri: string }): Promise<Asset> =>
  typeof module === "number" ? localAsset(module) : remoteAsset(module.uri);

export default class ExponentTextureLoader extends TextureLoader<*> {
  loads: Map<number | string, DisposablePromise<*>> = new Map();
  textures: Map<number | string, *> = new Map();
  assetSizes: Map<number | string, *> = new Map();
  dispose() {
    const { loads } = this;
    loads.forEach(d => {
      d.dispose();
    });<commit_msg>[gl-react-expo] Fix for new FileSystem API<commit_after>//@flow
import { Image } from "react-native";
import Expo from "expo";
import { TextureLoader } from "gl-react";
import type { DisposablePromise } from "gl-react/lib/helpers/disposable";
import md5 from "./md5";

type Asset = {
  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 = Expo.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)
    ),
    Expo.FileSystem.downloadAsync(
      uri,
      Expo.FileSystem.documentDirectory + `ExponentAsset-${key}${ext}`,
      {
        cache: true
      }
    )
  ]).then(([size, asset]) => ({ ...size, uri, localUri: asset.uri }));
  remoteAssetCache[key] = promise;
  return promise;
};

export const loadAsset = (module: number | { uri: string }): Promise<Asset> =>
  typeof module === "number" ? localAsset(module) : remoteAsset(module.uri);

export default class ExponentTextureLoader extends TextureLoader<*> {
  loads: Map<number | string, DisposablePromise<*>> = new Map();
  textures: Map<number | string, *> = new Map();
  assetSizes: Map<number | string, *> = new Map();
  dispose() {
    const { loads } = this;
    loads.forEach(d => {
      d.dispose();
    });
{'fraction_non_alphanumeric': 0.16793035664139286, 'fraction_numerical': 0.002246559955068801, 'mean_word_length': 3.6440677966101696, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5006627', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1138, 'n_words': 299}