-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
38 lines (31 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {NativeModules, Platform} from 'react-native';
const {RNLocalResourceModule} = NativeModules;
const {OS} = Platform;
import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource";
export default async function loadLocalRawResource(source) {
const uri = getUriFromSource(source);
if (OS == "android" && isUriAnAndroidResourceIdentifier(uri)) {
return loadAndroidRawResource(uri);
} else {
return loadResourceUsingFetch(uri);
}
}
function getUriFromSource(source) {
const resolvedAssetSource = resolveAssetSource(source);
return resolvedAssetSource.uri;
}
async function loadAndroidRawResource(uri) {
try {
return await RNLocalResourceModule.getRawResource(uri);
} catch (e) {
console.error("Error in RawResourceUtils while trying to natively load an Android raw resource: ", e);
return null;
}
}
async function loadResourceUsingFetch(uri) {
const blob = await fetch(uri);
return await blob.text();
}
function isUriAnAndroidResourceIdentifier(uri) {
return typeof uri == "string" && uri.indexOf("/") <= -1;
}