expo-asset 8.4.3 → 8.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -5
- package/README.md +1 -1
- package/build/Asset.d.ts +68 -4
- package/build/Asset.js +75 -2
- package/build/Asset.js.map +1 -1
- package/build/AssetHooks.d.ts +9 -4
- package/build/AssetHooks.js +10 -4
- package/build/AssetHooks.js.map +1 -1
- package/build/AssetSources.js.map +1 -1
- package/package.json +3 -3
- package/src/Asset.ts +79 -4
- package/src/AssetHooks.ts +10 -4
- package/src/AssetSources.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -10,16 +10,12 @@
|
|
|
10
10
|
|
|
11
11
|
### 💡 Others
|
|
12
12
|
|
|
13
|
-
## 8.4.
|
|
13
|
+
## 8.4.4 — 2021-11-17
|
|
14
14
|
|
|
15
15
|
### 🐛 Bug fixes
|
|
16
16
|
|
|
17
17
|
- Fix `fromModule` on restrictive (Snack) web environments. ([#14435](https://github.com/expo/expo/pull/14435) by [@IjzerenHein](https://github.com/IjzerenHein))
|
|
18
18
|
|
|
19
|
-
## 8.4.2 — 2021-10-15
|
|
20
|
-
|
|
21
|
-
_This version does not introduce any user-facing changes._
|
|
22
|
-
|
|
23
19
|
## 8.4.1 — 2021-10-01
|
|
24
20
|
|
|
25
21
|
### 💡 Others
|
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ For managed [managed](https://docs.expo.io/versions/latest/introduction/managed-
|
|
|
13
13
|
|
|
14
14
|
# Installation in bare React Native projects
|
|
15
15
|
|
|
16
|
-
For bare React Native projects, you must ensure that you have [installed and configured the `
|
|
16
|
+
For bare React Native projects, you must ensure that you have [installed and configured the `expo` package](https://docs.expo.dev/bare/installing-expo-modules/) before continuing.
|
|
17
17
|
|
|
18
18
|
### Add the package to your npm dependencies
|
|
19
19
|
|
package/build/Asset.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
declare type AssetDescriptor = {
|
|
1
|
+
import { AssetMetadata } from './AssetSources';
|
|
2
|
+
export declare type AssetDescriptor = {
|
|
3
3
|
name: string;
|
|
4
4
|
type: string;
|
|
5
5
|
hash?: string | null;
|
|
@@ -11,25 +11,89 @@ declare type DownloadPromiseCallbacks = {
|
|
|
11
11
|
resolve: () => void;
|
|
12
12
|
reject: (error: Error) => void;
|
|
13
13
|
};
|
|
14
|
-
export
|
|
14
|
+
export { AssetMetadata };
|
|
15
|
+
/**
|
|
16
|
+
* The `Asset` class represents an asset in your app. It gives metadata about the asset (such as its
|
|
17
|
+
* name and type) and provides facilities to load the asset data.
|
|
18
|
+
*/
|
|
15
19
|
export declare class Asset {
|
|
20
|
+
/**
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
16
23
|
static byHash: {};
|
|
24
|
+
/**
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
17
27
|
static byUri: {};
|
|
28
|
+
/**
|
|
29
|
+
* The name of the asset file without the extension. Also without the part from `@` onward in the
|
|
30
|
+
* filename (used to specify scale factor for images).
|
|
31
|
+
*/
|
|
18
32
|
name: string;
|
|
33
|
+
/**
|
|
34
|
+
* The extension of the asset filename.
|
|
35
|
+
*/
|
|
19
36
|
type: string;
|
|
37
|
+
/**
|
|
38
|
+
* The MD5 hash of the asset's data.
|
|
39
|
+
*/
|
|
20
40
|
hash: string | null;
|
|
41
|
+
/**
|
|
42
|
+
* A URI that points to the asset's data on the remote server. When running the published version
|
|
43
|
+
* of your app, this refers to the location on Expo's asset server where Expo has stored your
|
|
44
|
+
* asset. When running the app from Expo CLI during development, this URI points to Expo CLI's
|
|
45
|
+
* server running on your computer and the asset is served directly from your computer.
|
|
46
|
+
*/
|
|
21
47
|
uri: string;
|
|
48
|
+
/**
|
|
49
|
+
* If the asset has been downloaded (by calling [`downloadAsync()`](#downloadasync)), the
|
|
50
|
+
* `file://` URI pointing to the local file on the device that contains the asset data.
|
|
51
|
+
*/
|
|
22
52
|
localUri: string | null;
|
|
53
|
+
/**
|
|
54
|
+
* If the asset is an image, the width of the image data divided by the scale factor. The scale
|
|
55
|
+
* factor is the number after `@` in the filename, or `1` if not present.
|
|
56
|
+
*/
|
|
23
57
|
width: number | null;
|
|
58
|
+
/**
|
|
59
|
+
* If the asset is an image, the height of the image data divided by the scale factor. The scale factor is the number after `@` in the filename, or `1` if not present.
|
|
60
|
+
*/
|
|
24
61
|
height: number | null;
|
|
25
62
|
downloading: boolean;
|
|
26
63
|
downloaded: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
27
67
|
_downloadCallbacks: DownloadPromiseCallbacks[];
|
|
28
68
|
constructor({ name, type, hash, uri, width, height }: AssetDescriptor);
|
|
69
|
+
/**
|
|
70
|
+
* A helper that wraps `Asset.fromModule(module).downloadAsync` for convenience.
|
|
71
|
+
* @param moduleId An array of `require('path/to/file')` or external network URLs. Can also be
|
|
72
|
+
* just one module or URL without an Array.
|
|
73
|
+
* @return Returns a Promise that fulfills with an array of `Asset`s when the asset(s) has been
|
|
74
|
+
* saved to disk.
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const [{ localUri }] = await Asset.loadAsync(require('./assets/snack-icon.png'));
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
29
80
|
static loadAsync(moduleId: number | number[] | string | string[]): Promise<Asset[]>;
|
|
81
|
+
/**
|
|
82
|
+
* Returns the [`Asset`](#asset) instance representing an asset given its module or URL.
|
|
83
|
+
* @param virtualAssetModule The value of `require('path/to/file')` for the asset or external
|
|
84
|
+
* network URL
|
|
85
|
+
* @return The [`Asset`](#asset) instance for the asset.
|
|
86
|
+
*/
|
|
30
87
|
static fromModule(virtualAssetModule: number | string): Asset;
|
|
31
88
|
static fromMetadata(meta: AssetMetadata): Asset;
|
|
32
89
|
static fromURI(uri: string): Asset;
|
|
90
|
+
/**
|
|
91
|
+
* Downloads the asset data to a local file in the device's cache directory. Once the returned
|
|
92
|
+
* promise is fulfilled without error, the [`localUri`](#assetlocaluri) field of this asset points
|
|
93
|
+
* to a local file containing the asset data. The asset is only downloaded if an up-to-date local
|
|
94
|
+
* file for the asset isn't already present due to an earlier download. The downloaded `Asset`
|
|
95
|
+
* will be returned when the promise is resolved.
|
|
96
|
+
* @return Returns a Promise which fulfills with an `Asset` instance.
|
|
97
|
+
*/
|
|
33
98
|
downloadAsync(): Promise<this>;
|
|
34
99
|
}
|
|
35
|
-
export {};
|
package/build/Asset.js
CHANGED
|
@@ -1,23 +1,66 @@
|
|
|
1
1
|
import { Platform } from 'expo-modules-core';
|
|
2
2
|
import { getAssetByID } from './AssetRegistry';
|
|
3
|
-
import
|
|
3
|
+
import { selectAssetSource } from './AssetSources';
|
|
4
4
|
import * as AssetUris from './AssetUris';
|
|
5
5
|
import * as ImageAssets from './ImageAssets';
|
|
6
6
|
import { getLocalAssetUri } from './LocalAssets';
|
|
7
7
|
import { downloadAsync, IS_ENV_WITH_UPDATES_ENABLED } from './PlatformUtils';
|
|
8
8
|
import resolveAssetSource from './resolveAssetSource';
|
|
9
|
+
// @needsAudit
|
|
10
|
+
/**
|
|
11
|
+
* The `Asset` class represents an asset in your app. It gives metadata about the asset (such as its
|
|
12
|
+
* name and type) and provides facilities to load the asset data.
|
|
13
|
+
*/
|
|
9
14
|
export class Asset {
|
|
15
|
+
/**
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
10
18
|
static byHash = {};
|
|
19
|
+
/**
|
|
20
|
+
* @private
|
|
21
|
+
*/
|
|
11
22
|
static byUri = {};
|
|
23
|
+
/**
|
|
24
|
+
* The name of the asset file without the extension. Also without the part from `@` onward in the
|
|
25
|
+
* filename (used to specify scale factor for images).
|
|
26
|
+
*/
|
|
12
27
|
name;
|
|
28
|
+
/**
|
|
29
|
+
* The extension of the asset filename.
|
|
30
|
+
*/
|
|
13
31
|
type;
|
|
32
|
+
/**
|
|
33
|
+
* The MD5 hash of the asset's data.
|
|
34
|
+
*/
|
|
14
35
|
hash = null;
|
|
36
|
+
/**
|
|
37
|
+
* A URI that points to the asset's data on the remote server. When running the published version
|
|
38
|
+
* of your app, this refers to the location on Expo's asset server where Expo has stored your
|
|
39
|
+
* asset. When running the app from Expo CLI during development, this URI points to Expo CLI's
|
|
40
|
+
* server running on your computer and the asset is served directly from your computer.
|
|
41
|
+
*/
|
|
15
42
|
uri;
|
|
43
|
+
/**
|
|
44
|
+
* If the asset has been downloaded (by calling [`downloadAsync()`](#downloadasync)), the
|
|
45
|
+
* `file://` URI pointing to the local file on the device that contains the asset data.
|
|
46
|
+
*/
|
|
16
47
|
localUri = null;
|
|
48
|
+
/**
|
|
49
|
+
* If the asset is an image, the width of the image data divided by the scale factor. The scale
|
|
50
|
+
* factor is the number after `@` in the filename, or `1` if not present.
|
|
51
|
+
*/
|
|
17
52
|
width = null;
|
|
53
|
+
/**
|
|
54
|
+
* If the asset is an image, the height of the image data divided by the scale factor. The scale factor is the number after `@` in the filename, or `1` if not present.
|
|
55
|
+
*/
|
|
18
56
|
height = null;
|
|
57
|
+
// @docsMissing
|
|
19
58
|
downloading = false;
|
|
59
|
+
// @docsMissing
|
|
20
60
|
downloaded = false;
|
|
61
|
+
/**
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
21
64
|
_downloadCallbacks = [];
|
|
22
65
|
constructor({ name, type, hash = null, uri, width, height }) {
|
|
23
66
|
this.name = name;
|
|
@@ -45,10 +88,29 @@ export class Asset {
|
|
|
45
88
|
}
|
|
46
89
|
}
|
|
47
90
|
}
|
|
91
|
+
// @needsAudit
|
|
92
|
+
/**
|
|
93
|
+
* A helper that wraps `Asset.fromModule(module).downloadAsync` for convenience.
|
|
94
|
+
* @param moduleId An array of `require('path/to/file')` or external network URLs. Can also be
|
|
95
|
+
* just one module or URL without an Array.
|
|
96
|
+
* @return Returns a Promise that fulfills with an array of `Asset`s when the asset(s) has been
|
|
97
|
+
* saved to disk.
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const [{ localUri }] = await Asset.loadAsync(require('./assets/snack-icon.png'));
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
48
103
|
static loadAsync(moduleId) {
|
|
49
104
|
const moduleIds = Array.isArray(moduleId) ? moduleId : [moduleId];
|
|
50
105
|
return Promise.all(moduleIds.map((moduleId) => Asset.fromModule(moduleId).downloadAsync()));
|
|
51
106
|
}
|
|
107
|
+
// @needsAudit
|
|
108
|
+
/**
|
|
109
|
+
* Returns the [`Asset`](#asset) instance representing an asset given its module or URL.
|
|
110
|
+
* @param virtualAssetModule The value of `require('path/to/file')` for the asset or external
|
|
111
|
+
* network URL
|
|
112
|
+
* @return The [`Asset`](#asset) instance for the asset.
|
|
113
|
+
*/
|
|
52
114
|
static fromModule(virtualAssetModule) {
|
|
53
115
|
if (typeof virtualAssetModule === 'string') {
|
|
54
116
|
return Asset.fromURI(virtualAssetModule);
|
|
@@ -82,6 +144,7 @@ export class Asset {
|
|
|
82
144
|
}
|
|
83
145
|
return Asset.fromMetadata(meta);
|
|
84
146
|
}
|
|
147
|
+
// @docsMissing
|
|
85
148
|
static fromMetadata(meta) {
|
|
86
149
|
// The hash of the whole asset, not to be confused with the hash of a specific file returned
|
|
87
150
|
// from `selectAssetSource`
|
|
@@ -89,7 +152,7 @@ export class Asset {
|
|
|
89
152
|
if (Asset.byHash[metaHash]) {
|
|
90
153
|
return Asset.byHash[metaHash];
|
|
91
154
|
}
|
|
92
|
-
const { uri, hash } =
|
|
155
|
+
const { uri, hash } = selectAssetSource(meta);
|
|
93
156
|
const asset = new Asset({
|
|
94
157
|
name: meta.name,
|
|
95
158
|
type: meta.type,
|
|
@@ -101,6 +164,7 @@ export class Asset {
|
|
|
101
164
|
Asset.byHash[metaHash] = asset;
|
|
102
165
|
return asset;
|
|
103
166
|
}
|
|
167
|
+
// @docsMissing
|
|
104
168
|
static fromURI(uri) {
|
|
105
169
|
if (Asset.byUri[uri]) {
|
|
106
170
|
return Asset.byUri[uri];
|
|
@@ -123,6 +187,15 @@ export class Asset {
|
|
|
123
187
|
Asset.byUri[uri] = asset;
|
|
124
188
|
return asset;
|
|
125
189
|
}
|
|
190
|
+
// @needsAudit
|
|
191
|
+
/**
|
|
192
|
+
* Downloads the asset data to a local file in the device's cache directory. Once the returned
|
|
193
|
+
* promise is fulfilled without error, the [`localUri`](#assetlocaluri) field of this asset points
|
|
194
|
+
* to a local file containing the asset data. The asset is only downloaded if an up-to-date local
|
|
195
|
+
* file for the asset isn't already present due to an earlier download. The downloaded `Asset`
|
|
196
|
+
* will be returned when the promise is resolved.
|
|
197
|
+
* @return Returns a Promise which fulfills with an `Asset` instance.
|
|
198
|
+
*/
|
|
126
199
|
async downloadAsync() {
|
|
127
200
|
if (this.downloaded) {
|
|
128
201
|
return this;
|
package/build/Asset.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Asset.js","sourceRoot":"","sources":["../src/Asset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,YAAY,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,KAAK,WAAW,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AAC7E,OAAO,kBAAkB,MAAM,sBAAsB,CAAC;AAkBtD,MAAM,OAAO,KAAK;IAChB,MAAM,CAAC,MAAM,GAAG,EAAE,CAAC;IACnB,MAAM,CAAC,KAAK,GAAG,EAAE,CAAC;IAElB,IAAI,CAAS;IACb,IAAI,CAAS;IACb,IAAI,GAAkB,IAAI,CAAC;IAC3B,GAAG,CAAS;IACZ,QAAQ,GAAkB,IAAI,CAAC;IAC/B,KAAK,GAAkB,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AAG,CAAC,CAAC;iBAC7C;aACF;YACD,IAAI,CAAC,QAAQ,GAAG,MAAM,aAAa,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YAE/E,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;YACvB,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;SAC7D;QAAC,OAAO,CAAC,EAAE;YACV,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YAC3D,MAAM,CAAC,CAAC;SACT;gBAAS;YACR,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;YACzB,IAAI,CAAC,kBAAkB,GAAG,EAAE,CAAC;SAC9B;QACD,OAAO,IAAI,CAAC;IACd,CAAC","sourcesContent":["import { Platform } from 'expo-modules-core';\n\nimport { getAssetByID } from './AssetRegistry';\nimport * as AssetSources from './AssetSources';\nimport * as AssetUris from './AssetUris';\nimport * as ImageAssets from './ImageAssets';\nimport { getLocalAssetUri } from './LocalAssets';\nimport { downloadAsync, IS_ENV_WITH_UPDATES_ENABLED } from './PlatformUtils';\nimport resolveAssetSource from './resolveAssetSource';\n\ntype AssetDescriptor = {\n name: string;\n type: string;\n hash?: string | null;\n uri: string;\n width?: number | null;\n height?: number | null;\n};\n\ntype DownloadPromiseCallbacks = {\n resolve: () => void;\n reject: (error: Error) => void;\n};\n\nexport type AssetMetadata = AssetSources.AssetMetadata;\n\nexport class Asset {\n static byHash = {};\n static byUri = {};\n\n name: string;\n type: string;\n hash: string | null = null;\n uri: string;\n localUri: string | null = null;\n width: number | null = null;\n height: number | null = null;\n downloading: boolean = false;\n downloaded: boolean = false;\n _downloadCallbacks: DownloadPromiseCallbacks[] = [];\n\n constructor({ name, type, hash = null, uri, width, height }: AssetDescriptor) {\n this.name = name;\n this.type = type;\n this.hash = hash;\n this.uri = uri;\n\n if (typeof width === 'number') {\n this.width = width;\n }\n if (typeof height === 'number') {\n this.height = height;\n }\n\n if (hash) {\n this.localUri = getLocalAssetUri(hash, type);\n if (this.localUri) {\n this.downloaded = true;\n }\n }\n\n if (Platform.OS === 'web') {\n if (!name) {\n this.name = AssetUris.getFilename(uri);\n }\n if (!type) {\n this.type = AssetUris.getFileExtension(uri);\n }\n }\n }\n\n static loadAsync(moduleId: number | number[] | string | string[]): Promise<Asset[]> {\n const moduleIds = Array.isArray(moduleId) ? moduleId : [moduleId];\n return Promise.all(moduleIds.map((moduleId) => Asset.fromModule(moduleId).downloadAsync()));\n }\n\n static fromModule(virtualAssetModule: number | string): Asset {\n if (typeof virtualAssetModule === 'string') {\n return Asset.fromURI(virtualAssetModule);\n }\n\n const meta = getAssetByID(virtualAssetModule);\n if (!meta) {\n throw new Error(`Module \"${virtualAssetModule}\" is missing from the asset registry`);\n }\n\n // Outside of the managed env we need the moduleId to initialize the asset\n // because resolveAssetSource depends on it\n if (!IS_ENV_WITH_UPDATES_ENABLED) {\n const { uri } = resolveAssetSource(virtualAssetModule);\n const asset = new Asset({\n name: meta.name,\n type: meta.type,\n hash: meta.hash,\n uri,\n width: meta.width,\n height: meta.height,\n });\n\n // TODO: FileSystem should probably support 'downloading' from drawable\n // resources But for now it doesn't (it only supports raw resources) and\n // React Native's Image works fine with drawable resource names for\n // images.\n if (Platform.OS === 'android' && !uri.includes(':') && (meta.width || meta.height)) {\n asset.localUri = asset.uri;\n asset.downloaded = true;\n }\n\n Asset.byHash[meta.hash] = asset;\n return asset;\n }\n\n return Asset.fromMetadata(meta);\n }\n\n static fromMetadata(meta: AssetMetadata): Asset {\n // The hash of the whole asset, not to be confused with the hash of a specific file returned\n // from `selectAssetSource`\n const metaHash = meta.hash;\n if (Asset.byHash[metaHash]) {\n return Asset.byHash[metaHash];\n }\n\n const { uri, hash } = AssetSources.selectAssetSource(meta);\n const asset = new Asset({\n name: meta.name,\n type: meta.type,\n hash,\n uri,\n width: meta.width,\n height: meta.height,\n });\n Asset.byHash[metaHash] = asset;\n return asset;\n }\n\n static fromURI(uri: string): Asset {\n if (Asset.byUri[uri]) {\n return Asset.byUri[uri];\n }\n\n // Possibly a Base64-encoded URI\n let type = '';\n if (uri.indexOf(';base64') > -1) {\n type = uri.split(';')[0].split('/')[1];\n } else {\n const extension = AssetUris.getFileExtension(uri);\n type = extension.startsWith('.') ? extension.substring(1) : extension;\n }\n\n const asset = new Asset({\n name: '',\n type,\n hash: null,\n uri,\n });\n\n Asset.byUri[uri] = asset;\n\n return asset;\n }\n\n async downloadAsync(): Promise<this> {\n if (this.downloaded) {\n return this;\n }\n if (this.downloading) {\n await new Promise<void>((resolve, reject) => {\n this._downloadCallbacks.push({ resolve, reject });\n });\n return this;\n }\n this.downloading = true;\n\n try {\n if (Platform.OS === 'web') {\n if (ImageAssets.isImageType(this.type)) {\n const { width, height, name } = await ImageAssets.getImageInfoAsync(this.uri);\n this.width = width;\n this.height = height;\n this.name = name;\n } else {\n this.name = AssetUris.getFilename(this.uri);\n }\n }\n this.localUri = await downloadAsync(this.uri, this.hash, this.type, this.name);\n\n this.downloaded = true;\n this._downloadCallbacks.forEach(({ resolve }) => resolve());\n } catch (e) {\n this._downloadCallbacks.forEach(({ reject }) => reject(e));\n throw e;\n } finally {\n this.downloading = false;\n this._downloadCallbacks = [];\n }\n return this;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"Asset.js","sourceRoot":"","sources":["../src/Asset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAiB,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC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gBAAS;YACR,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;YACzB,IAAI,CAAC,kBAAkB,GAAG,EAAE,CAAC;SAC9B;QACD,OAAO,IAAI,CAAC;IACd,CAAC","sourcesContent":["import { Platform } from 'expo-modules-core';\n\nimport { getAssetByID } from './AssetRegistry';\nimport { AssetMetadata, selectAssetSource } from './AssetSources';\nimport * as AssetUris from './AssetUris';\nimport * as ImageAssets from './ImageAssets';\nimport { getLocalAssetUri } from './LocalAssets';\nimport { downloadAsync, IS_ENV_WITH_UPDATES_ENABLED } from './PlatformUtils';\nimport resolveAssetSource from './resolveAssetSource';\n\n// @docsMissing\nexport type AssetDescriptor = {\n name: string;\n type: string;\n hash?: string | null;\n uri: string;\n width?: number | null;\n height?: number | null;\n};\n\ntype DownloadPromiseCallbacks = {\n resolve: () => void;\n reject: (error: Error) => void;\n};\n\nexport { AssetMetadata };\n\n// @needsAudit\n/**\n * The `Asset` class represents an asset in your app. It gives metadata about the asset (such as its\n * name and type) and provides facilities to load the asset data.\n */\nexport class Asset {\n /**\n * @private\n */\n static byHash = {};\n /**\n * @private\n */\n static byUri = {};\n\n /**\n * The name of the asset file without the extension. Also without the part from `@` onward in the\n * filename (used to specify scale factor for images).\n */\n name: string;\n /**\n * The extension of the asset filename.\n */\n type: string;\n /**\n * The MD5 hash of the asset's data.\n */\n hash: string | null = null;\n /**\n * A URI that points to the asset's data on the remote server. When running the published version\n * of your app, this refers to the location on Expo's asset server where Expo has stored your\n * asset. When running the app from Expo CLI during development, this URI points to Expo CLI's\n * server running on your computer and the asset is served directly from your computer.\n */\n uri: string;\n /**\n * If the asset has been downloaded (by calling [`downloadAsync()`](#downloadasync)), the\n * `file://` URI pointing to the local file on the device that contains the asset data.\n */\n localUri: string | null = null;\n /**\n * If the asset is an image, the width of the image data divided by the scale factor. The scale\n * factor is the number after `@` in the filename, or `1` if not present.\n */\n width: number | null = null;\n /**\n * If the asset is an image, the height of the image data divided by the scale factor. The scale factor is the number after `@` in the filename, or `1` if not present.\n */\n height: number | null = null;\n // @docsMissing\n downloading: boolean = false;\n // @docsMissing\n downloaded: boolean = false;\n\n /**\n * @private\n */\n _downloadCallbacks: DownloadPromiseCallbacks[] = [];\n\n constructor({ name, type, hash = null, uri, width, height }: AssetDescriptor) {\n this.name = name;\n this.type = type;\n this.hash = hash;\n this.uri = uri;\n\n if (typeof width === 'number') {\n this.width = width;\n }\n if (typeof height === 'number') {\n this.height = height;\n }\n\n if (hash) {\n this.localUri = getLocalAssetUri(hash, type);\n if (this.localUri) {\n this.downloaded = true;\n }\n }\n\n if (Platform.OS === 'web') {\n if (!name) {\n this.name = AssetUris.getFilename(uri);\n }\n if (!type) {\n this.type = AssetUris.getFileExtension(uri);\n }\n }\n }\n\n // @needsAudit\n /**\n * A helper that wraps `Asset.fromModule(module).downloadAsync` for convenience.\n * @param moduleId An array of `require('path/to/file')` or external network URLs. Can also be\n * just one module or URL without an Array.\n * @return Returns a Promise that fulfills with an array of `Asset`s when the asset(s) has been\n * saved to disk.\n * @example\n * ```ts\n * const [{ localUri }] = await Asset.loadAsync(require('./assets/snack-icon.png'));\n * ```\n */\n static loadAsync(moduleId: number | number[] | string | string[]): Promise<Asset[]> {\n const moduleIds = Array.isArray(moduleId) ? moduleId : [moduleId];\n return Promise.all(moduleIds.map((moduleId) => Asset.fromModule(moduleId).downloadAsync()));\n }\n\n // @needsAudit\n /**\n * Returns the [`Asset`](#asset) instance representing an asset given its module or URL.\n * @param virtualAssetModule The value of `require('path/to/file')` for the asset or external\n * network URL\n * @return The [`Asset`](#asset) instance for the asset.\n */\n static fromModule(virtualAssetModule: number | string): Asset {\n if (typeof virtualAssetModule === 'string') {\n return Asset.fromURI(virtualAssetModule);\n }\n\n const meta = getAssetByID(virtualAssetModule);\n if (!meta) {\n throw new Error(`Module \"${virtualAssetModule}\" is missing from the asset registry`);\n }\n\n // Outside of the managed env we need the moduleId to initialize the asset\n // because resolveAssetSource depends on it\n if (!IS_ENV_WITH_UPDATES_ENABLED) {\n const { uri } = resolveAssetSource(virtualAssetModule);\n const asset = new Asset({\n name: meta.name,\n type: meta.type,\n hash: meta.hash,\n uri,\n width: meta.width,\n height: meta.height,\n });\n\n // TODO: FileSystem should probably support 'downloading' from drawable\n // resources But for now it doesn't (it only supports raw resources) and\n // React Native's Image works fine with drawable resource names for\n // images.\n if (Platform.OS === 'android' && !uri.includes(':') && (meta.width || meta.height)) {\n asset.localUri = asset.uri;\n asset.downloaded = true;\n }\n\n Asset.byHash[meta.hash] = asset;\n return asset;\n }\n\n return Asset.fromMetadata(meta);\n }\n\n // @docsMissing\n static fromMetadata(meta: AssetMetadata): Asset {\n // The hash of the whole asset, not to be confused with the hash of a specific file returned\n // from `selectAssetSource`\n const metaHash = meta.hash;\n if (Asset.byHash[metaHash]) {\n return Asset.byHash[metaHash];\n }\n\n const { uri, hash } = selectAssetSource(meta);\n const asset = new Asset({\n name: meta.name,\n type: meta.type,\n hash,\n uri,\n width: meta.width,\n height: meta.height,\n });\n Asset.byHash[metaHash] = asset;\n return asset;\n }\n\n // @docsMissing\n static fromURI(uri: string): Asset {\n if (Asset.byUri[uri]) {\n return Asset.byUri[uri];\n }\n\n // Possibly a Base64-encoded URI\n let type = '';\n if (uri.indexOf(';base64') > -1) {\n type = uri.split(';')[0].split('/')[1];\n } else {\n const extension = AssetUris.getFileExtension(uri);\n type = extension.startsWith('.') ? extension.substring(1) : extension;\n }\n\n const asset = new Asset({\n name: '',\n type,\n hash: null,\n uri,\n });\n\n Asset.byUri[uri] = asset;\n\n return asset;\n }\n\n // @needsAudit\n /**\n * Downloads the asset data to a local file in the device's cache directory. Once the returned\n * promise is fulfilled without error, the [`localUri`](#assetlocaluri) field of this asset points\n * to a local file containing the asset data. The asset is only downloaded if an up-to-date local\n * file for the asset isn't already present due to an earlier download. The downloaded `Asset`\n * will be returned when the promise is resolved.\n * @return Returns a Promise which fulfills with an `Asset` instance.\n */\n async downloadAsync(): Promise<this> {\n if (this.downloaded) {\n return this;\n }\n if (this.downloading) {\n await new Promise<void>((resolve, reject) => {\n this._downloadCallbacks.push({ resolve, reject });\n });\n return this;\n }\n this.downloading = true;\n\n try {\n if (Platform.OS === 'web') {\n if (ImageAssets.isImageType(this.type)) {\n const { width, height, name } = await ImageAssets.getImageInfoAsync(this.uri);\n this.width = width;\n this.height = height;\n this.name = name;\n } else {\n this.name = AssetUris.getFilename(this.uri);\n }\n }\n this.localUri = await downloadAsync(this.uri, this.hash, this.type, this.name);\n\n this.downloaded = true;\n this._downloadCallbacks.forEach(({ resolve }) => resolve());\n } catch (e) {\n this._downloadCallbacks.forEach(({ reject }) => reject(e));\n throw e;\n } finally {\n this.downloading = false;\n this._downloadCallbacks = [];\n }\n return this;\n }\n}\n"]}
|
package/build/AssetHooks.d.ts
CHANGED
|
@@ -4,14 +4,19 @@ import { Asset } from './Asset';
|
|
|
4
4
|
* After the assets are loaded, this hook returns a list of asset instances.
|
|
5
5
|
* If something went wrong when loading the assets, an error is returned.
|
|
6
6
|
*
|
|
7
|
-
* Note, the assets are not "reloaded" when you dynamically change the asset list.
|
|
7
|
+
* > Note, the assets are not "reloaded" when you dynamically change the asset list.
|
|
8
|
+
*
|
|
9
|
+
* @return Returns an array containing:
|
|
10
|
+
* - on the first position, a list of all loaded assets. If they aren't loaded yet, this value is
|
|
11
|
+
* `undefined`.
|
|
12
|
+
* - on the second position, an error which encountered when loading the assets. If there was no
|
|
13
|
+
* error, this value is `undefined`.
|
|
8
14
|
*
|
|
9
|
-
* @see https://docs.expo.io/versions/latest/sdk/asset/
|
|
10
15
|
* @example
|
|
11
16
|
* ```tsx
|
|
12
|
-
* const [assets, error] = useAssets(require('path/to/asset.jpg'));
|
|
17
|
+
* const [assets, error] = useAssets([require('path/to/asset.jpg'), require('path/to/other.png')]);
|
|
13
18
|
*
|
|
14
|
-
* return
|
|
19
|
+
* return assets ? <Image source={assets[0]} /> : null;
|
|
15
20
|
* ```
|
|
16
21
|
*/
|
|
17
22
|
export declare function useAssets(moduleIds: number | number[]): [Asset[] | undefined, Error | undefined];
|
package/build/AssetHooks.js
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
2
|
import { Asset } from './Asset';
|
|
3
|
+
// @needsAudit
|
|
3
4
|
/**
|
|
4
5
|
* Downloads and stores one or more assets locally.
|
|
5
6
|
* After the assets are loaded, this hook returns a list of asset instances.
|
|
6
7
|
* If something went wrong when loading the assets, an error is returned.
|
|
7
8
|
*
|
|
8
|
-
* Note, the assets are not "reloaded" when you dynamically change the asset list.
|
|
9
|
+
* > Note, the assets are not "reloaded" when you dynamically change the asset list.
|
|
10
|
+
*
|
|
11
|
+
* @return Returns an array containing:
|
|
12
|
+
* - on the first position, a list of all loaded assets. If they aren't loaded yet, this value is
|
|
13
|
+
* `undefined`.
|
|
14
|
+
* - on the second position, an error which encountered when loading the assets. If there was no
|
|
15
|
+
* error, this value is `undefined`.
|
|
9
16
|
*
|
|
10
|
-
* @see https://docs.expo.io/versions/latest/sdk/asset/
|
|
11
17
|
* @example
|
|
12
18
|
* ```tsx
|
|
13
|
-
* const [assets, error] = useAssets(require('path/to/asset.jpg'));
|
|
19
|
+
* const [assets, error] = useAssets([require('path/to/asset.jpg'), require('path/to/other.png')]);
|
|
14
20
|
*
|
|
15
|
-
* return
|
|
21
|
+
* return assets ? <Image source={assets[0]} /> : null;
|
|
16
22
|
* ```
|
|
17
23
|
*/
|
|
18
24
|
export function useAssets(moduleIds) {
|
package/build/AssetHooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssetHooks.js","sourceRoot":"","sources":["../src/AssetHooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE5C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC
|
|
1
|
+
{"version":3,"file":"AssetHooks.js","sourceRoot":"","sources":["../src/AssetHooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE5C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,cAAc;AACd;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,SAAS,CAAC,SAA4B;IACpD,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,EAAW,CAAC;IAChD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAAS,CAAC;IAE5C,SAAS,CAAC,GAAG,EAAE;QACb,KAAK,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC7D,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AACzB,CAAC","sourcesContent":["import { useEffect, useState } from 'react';\n\nimport { Asset } from './Asset';\n\n// @needsAudit\n/**\n * Downloads and stores one or more assets locally.\n * After the assets are loaded, this hook returns a list of asset instances.\n * If something went wrong when loading the assets, an error is returned.\n *\n * > Note, the assets are not \"reloaded\" when you dynamically change the asset list.\n *\n * @return Returns an array containing:\n * - on the first position, a list of all loaded assets. If they aren't loaded yet, this value is\n * `undefined`.\n * - on the second position, an error which encountered when loading the assets. If there was no\n * error, this value is `undefined`.\n *\n * @example\n * ```tsx\n * const [assets, error] = useAssets([require('path/to/asset.jpg'), require('path/to/other.png')]);\n *\n * return assets ? <Image source={assets[0]} /> : null;\n * ```\n */\nexport function useAssets(moduleIds: number | number[]): [Asset[] | undefined, Error | undefined] {\n const [assets, setAssets] = useState<Asset[]>();\n const [error, setError] = useState<Error>();\n\n useEffect(() => {\n Asset.loadAsync(moduleIds).then(setAssets).catch(setError);\n }, []);\n\n return [assets, error];\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssetSources.js","sourceRoot":"","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":["import { Platform } from 'expo-modules-core';\nimport path from 'path-browserify';\nimport { PixelRatio } from 'react-native';\nimport URL from 'url-parse';\n\nimport AssetSourceResolver from './AssetSourceResolver';\nimport { manifestBaseUrl, getManifest } from './PlatformUtils';\n\n// @docsMissing\nexport type AssetMetadata = {\n hash: string;\n name: string;\n type: string;\n width?: number;\n height?: number;\n scales: number[];\n httpServerLocation: string;\n uri?: string;\n fileHashes?: string[];\n fileUris?: string[];\n};\n\nexport type AssetSource = {\n uri: string;\n hash: string;\n};\n\n// Fast lookup check if asset map has any overrides in the manifest\nconst assetMapOverride = getManifest().assetMapOverride;\n\n/**\n * Selects the best file for the given asset (ex: choosing the best scale for images) and returns\n * a { uri, hash } pair for the specific asset file.\n *\n * If the asset isn't an image with multiple scales, the first file is selected.\n */\nexport function selectAssetSource(meta: AssetMetadata): AssetSource {\n // Override with the asset map in manifest if available\n if (assetMapOverride && assetMapOverride.hasOwnProperty(meta.hash)) {\n meta = { ...meta, ...assetMapOverride[meta.hash] };\n }\n\n // This logic is based on that of AssetSourceResolver, with additional support for file hashes and\n // explicitly provided URIs\n const scale = AssetSourceResolver.pickScale(meta.scales, PixelRatio.get());\n const index = meta.scales.findIndex((s) => s === scale);\n const hash = meta.fileHashes ? meta.fileHashes[index] || meta.fileHashes[0] : meta.hash;\n\n // Allow asset processors to directly provide the URL to load\n const uri = meta.fileUris ? meta.fileUris[index] || meta.fileUris[0] : meta.uri;\n if (uri) {\n return { uri: resolveUri(uri), hash };\n }\n\n // Check if the assetUrl was overridden in the manifest\n const assetUrlOverride = getManifest().assetUrlOverride;\n if (assetUrlOverride) {\n const uri = path.join(assetUrlOverride, hash);\n return { uri: resolveUri(uri), hash };\n }\n\n const fileScale = scale === 1 ? '' : `@${scale}x`;\n const fileExtension = meta.type ? `.${encodeURIComponent(meta.type)}` : '';\n const suffix = `/${encodeURIComponent(\n meta.name\n )}${fileScale}${fileExtension}?platform=${encodeURIComponent(\n Platform.OS\n )}&hash=${encodeURIComponent(meta.hash)}`;\n\n // For assets with a specified absolute URL, we use the existing origin instead of prepending the\n // development server or production CDN URL origin\n if (/^https?:\\/\\//.test(meta.httpServerLocation)) {\n const uri = meta.httpServerLocation + suffix;\n return { uri, hash };\n }\n\n // For assets during development, we use the development server's URL origin\n if (getManifest().developer) {\n const baseUrl = new URL(getManifest().bundleUrl);\n baseUrl.set('pathname', meta.httpServerLocation + suffix);\n return { uri: baseUrl.href, hash };\n }\n\n // Production CDN URIs are based on each asset file hash\n return {\n uri: `https://d1wp6m56sqw74a.cloudfront.net/~assets/${encodeURIComponent(hash)}`,\n hash,\n };\n}\n\n/**\n * Resolves the given URI to an absolute URI. If the given URI is already an absolute URI, it is\n * simply returned. Otherwise, if it is a relative URI, it is resolved relative to the manifest's\n * base URI.\n */\nexport function resolveUri(uri: string): string {\n if (!manifestBaseUrl) {\n return uri;\n }\n\n const { protocol } = new URL(uri);\n if (protocol !== '') {\n return uri;\n }\n\n const baseUrl = new URL(manifestBaseUrl);\n const resolvedPath = uri.startsWith('/') ? uri : path.join(baseUrl.pathname, uri);\n baseUrl.set('pathname', resolvedPath);\n return baseUrl.href;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expo-asset",
|
|
3
|
-
"version": "8.4.
|
|
3
|
+
"version": "8.4.4",
|
|
4
4
|
"description": "An Expo universal module to download assets and pass them into other APIs",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"url-parse": "^1.4.4"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@testing-library/react-hooks": "^
|
|
47
|
+
"@testing-library/react-hooks": "^7.0.1",
|
|
48
48
|
"@types/url-parse": "^1.4.1",
|
|
49
49
|
"expo-module-scripts": "^2.0.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "9faa58818454ba59dbff95077b9025a1c1cbd9fd"
|
|
52
52
|
}
|
package/src/Asset.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { Platform } from 'expo-modules-core';
|
|
2
2
|
|
|
3
3
|
import { getAssetByID } from './AssetRegistry';
|
|
4
|
-
import
|
|
4
|
+
import { AssetMetadata, selectAssetSource } from './AssetSources';
|
|
5
5
|
import * as AssetUris from './AssetUris';
|
|
6
6
|
import * as ImageAssets from './ImageAssets';
|
|
7
7
|
import { getLocalAssetUri } from './LocalAssets';
|
|
8
8
|
import { downloadAsync, IS_ENV_WITH_UPDATES_ENABLED } from './PlatformUtils';
|
|
9
9
|
import resolveAssetSource from './resolveAssetSource';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
// @docsMissing
|
|
12
|
+
export type AssetDescriptor = {
|
|
12
13
|
name: string;
|
|
13
14
|
type: string;
|
|
14
15
|
hash?: string | null;
|
|
@@ -22,21 +23,65 @@ type DownloadPromiseCallbacks = {
|
|
|
22
23
|
reject: (error: Error) => void;
|
|
23
24
|
};
|
|
24
25
|
|
|
25
|
-
export
|
|
26
|
+
export { AssetMetadata };
|
|
26
27
|
|
|
28
|
+
// @needsAudit
|
|
29
|
+
/**
|
|
30
|
+
* The `Asset` class represents an asset in your app. It gives metadata about the asset (such as its
|
|
31
|
+
* name and type) and provides facilities to load the asset data.
|
|
32
|
+
*/
|
|
27
33
|
export class Asset {
|
|
34
|
+
/**
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
28
37
|
static byHash = {};
|
|
38
|
+
/**
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
29
41
|
static byUri = {};
|
|
30
42
|
|
|
43
|
+
/**
|
|
44
|
+
* The name of the asset file without the extension. Also without the part from `@` onward in the
|
|
45
|
+
* filename (used to specify scale factor for images).
|
|
46
|
+
*/
|
|
31
47
|
name: string;
|
|
48
|
+
/**
|
|
49
|
+
* The extension of the asset filename.
|
|
50
|
+
*/
|
|
32
51
|
type: string;
|
|
52
|
+
/**
|
|
53
|
+
* The MD5 hash of the asset's data.
|
|
54
|
+
*/
|
|
33
55
|
hash: string | null = null;
|
|
56
|
+
/**
|
|
57
|
+
* A URI that points to the asset's data on the remote server. When running the published version
|
|
58
|
+
* of your app, this refers to the location on Expo's asset server where Expo has stored your
|
|
59
|
+
* asset. When running the app from Expo CLI during development, this URI points to Expo CLI's
|
|
60
|
+
* server running on your computer and the asset is served directly from your computer.
|
|
61
|
+
*/
|
|
34
62
|
uri: string;
|
|
63
|
+
/**
|
|
64
|
+
* If the asset has been downloaded (by calling [`downloadAsync()`](#downloadasync)), the
|
|
65
|
+
* `file://` URI pointing to the local file on the device that contains the asset data.
|
|
66
|
+
*/
|
|
35
67
|
localUri: string | null = null;
|
|
68
|
+
/**
|
|
69
|
+
* If the asset is an image, the width of the image data divided by the scale factor. The scale
|
|
70
|
+
* factor is the number after `@` in the filename, or `1` if not present.
|
|
71
|
+
*/
|
|
36
72
|
width: number | null = null;
|
|
73
|
+
/**
|
|
74
|
+
* If the asset is an image, the height of the image data divided by the scale factor. The scale factor is the number after `@` in the filename, or `1` if not present.
|
|
75
|
+
*/
|
|
37
76
|
height: number | null = null;
|
|
77
|
+
// @docsMissing
|
|
38
78
|
downloading: boolean = false;
|
|
79
|
+
// @docsMissing
|
|
39
80
|
downloaded: boolean = false;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
40
85
|
_downloadCallbacks: DownloadPromiseCallbacks[] = [];
|
|
41
86
|
|
|
42
87
|
constructor({ name, type, hash = null, uri, width, height }: AssetDescriptor) {
|
|
@@ -69,11 +114,30 @@ export class Asset {
|
|
|
69
114
|
}
|
|
70
115
|
}
|
|
71
116
|
|
|
117
|
+
// @needsAudit
|
|
118
|
+
/**
|
|
119
|
+
* A helper that wraps `Asset.fromModule(module).downloadAsync` for convenience.
|
|
120
|
+
* @param moduleId An array of `require('path/to/file')` or external network URLs. Can also be
|
|
121
|
+
* just one module or URL without an Array.
|
|
122
|
+
* @return Returns a Promise that fulfills with an array of `Asset`s when the asset(s) has been
|
|
123
|
+
* saved to disk.
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const [{ localUri }] = await Asset.loadAsync(require('./assets/snack-icon.png'));
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
72
129
|
static loadAsync(moduleId: number | number[] | string | string[]): Promise<Asset[]> {
|
|
73
130
|
const moduleIds = Array.isArray(moduleId) ? moduleId : [moduleId];
|
|
74
131
|
return Promise.all(moduleIds.map((moduleId) => Asset.fromModule(moduleId).downloadAsync()));
|
|
75
132
|
}
|
|
76
133
|
|
|
134
|
+
// @needsAudit
|
|
135
|
+
/**
|
|
136
|
+
* Returns the [`Asset`](#asset) instance representing an asset given its module or URL.
|
|
137
|
+
* @param virtualAssetModule The value of `require('path/to/file')` for the asset or external
|
|
138
|
+
* network URL
|
|
139
|
+
* @return The [`Asset`](#asset) instance for the asset.
|
|
140
|
+
*/
|
|
77
141
|
static fromModule(virtualAssetModule: number | string): Asset {
|
|
78
142
|
if (typeof virtualAssetModule === 'string') {
|
|
79
143
|
return Asset.fromURI(virtualAssetModule);
|
|
@@ -113,6 +177,7 @@ export class Asset {
|
|
|
113
177
|
return Asset.fromMetadata(meta);
|
|
114
178
|
}
|
|
115
179
|
|
|
180
|
+
// @docsMissing
|
|
116
181
|
static fromMetadata(meta: AssetMetadata): Asset {
|
|
117
182
|
// The hash of the whole asset, not to be confused with the hash of a specific file returned
|
|
118
183
|
// from `selectAssetSource`
|
|
@@ -121,7 +186,7 @@ export class Asset {
|
|
|
121
186
|
return Asset.byHash[metaHash];
|
|
122
187
|
}
|
|
123
188
|
|
|
124
|
-
const { uri, hash } =
|
|
189
|
+
const { uri, hash } = selectAssetSource(meta);
|
|
125
190
|
const asset = new Asset({
|
|
126
191
|
name: meta.name,
|
|
127
192
|
type: meta.type,
|
|
@@ -134,6 +199,7 @@ export class Asset {
|
|
|
134
199
|
return asset;
|
|
135
200
|
}
|
|
136
201
|
|
|
202
|
+
// @docsMissing
|
|
137
203
|
static fromURI(uri: string): Asset {
|
|
138
204
|
if (Asset.byUri[uri]) {
|
|
139
205
|
return Asset.byUri[uri];
|
|
@@ -160,6 +226,15 @@ export class Asset {
|
|
|
160
226
|
return asset;
|
|
161
227
|
}
|
|
162
228
|
|
|
229
|
+
// @needsAudit
|
|
230
|
+
/**
|
|
231
|
+
* Downloads the asset data to a local file in the device's cache directory. Once the returned
|
|
232
|
+
* promise is fulfilled without error, the [`localUri`](#assetlocaluri) field of this asset points
|
|
233
|
+
* to a local file containing the asset data. The asset is only downloaded if an up-to-date local
|
|
234
|
+
* file for the asset isn't already present due to an earlier download. The downloaded `Asset`
|
|
235
|
+
* will be returned when the promise is resolved.
|
|
236
|
+
* @return Returns a Promise which fulfills with an `Asset` instance.
|
|
237
|
+
*/
|
|
163
238
|
async downloadAsync(): Promise<this> {
|
|
164
239
|
if (this.downloaded) {
|
|
165
240
|
return this;
|
package/src/AssetHooks.ts
CHANGED
|
@@ -2,19 +2,25 @@ import { useEffect, useState } from 'react';
|
|
|
2
2
|
|
|
3
3
|
import { Asset } from './Asset';
|
|
4
4
|
|
|
5
|
+
// @needsAudit
|
|
5
6
|
/**
|
|
6
7
|
* Downloads and stores one or more assets locally.
|
|
7
8
|
* After the assets are loaded, this hook returns a list of asset instances.
|
|
8
9
|
* If something went wrong when loading the assets, an error is returned.
|
|
9
10
|
*
|
|
10
|
-
* Note, the assets are not "reloaded" when you dynamically change the asset list.
|
|
11
|
+
* > Note, the assets are not "reloaded" when you dynamically change the asset list.
|
|
12
|
+
*
|
|
13
|
+
* @return Returns an array containing:
|
|
14
|
+
* - on the first position, a list of all loaded assets. If they aren't loaded yet, this value is
|
|
15
|
+
* `undefined`.
|
|
16
|
+
* - on the second position, an error which encountered when loading the assets. If there was no
|
|
17
|
+
* error, this value is `undefined`.
|
|
11
18
|
*
|
|
12
|
-
* @see https://docs.expo.io/versions/latest/sdk/asset/
|
|
13
19
|
* @example
|
|
14
20
|
* ```tsx
|
|
15
|
-
* const [assets, error] = useAssets(require('path/to/asset.jpg'));
|
|
21
|
+
* const [assets, error] = useAssets([require('path/to/asset.jpg'), require('path/to/other.png')]);
|
|
16
22
|
*
|
|
17
|
-
* return
|
|
23
|
+
* return assets ? <Image source={assets[0]} /> : null;
|
|
18
24
|
* ```
|
|
19
25
|
*/
|
|
20
26
|
export function useAssets(moduleIds: number | number[]): [Asset[] | undefined, Error | undefined] {
|
package/src/AssetSources.ts
CHANGED