Skip to content

Commit 65b45fd

Browse files
committed
[add] ImageLoader.loadUsingHeaders
1 parent 8f4d952 commit 65b45fd

File tree

1 file changed

+61
-5
lines changed
  • packages/react-native-web/src/modules/ImageLoader

1 file changed

+61
-5
lines changed

packages/react-native-web/src/modules/ImageLoader/index.js

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const ImageLoader = {
9090
) {
9191
let complete = false;
9292
const interval = setInterval(callback, 16);
93-
const requestId = ImageLoader.load(uri, callback, errorCallback);
93+
const { requestId } = ImageLoader.load({ uri }, callback, errorCallback);
9494

9595
function callback() {
9696
const image = requests[`${requestId}`];
@@ -118,7 +118,11 @@ const ImageLoader = {
118118
has(uri: string): boolean {
119119
return ImageUriCache.has(uri);
120120
},
121-
load(uri: string, onLoad: Function, onError: Function): number {
121+
load(
122+
source: { uri: string },
123+
onLoad: Function,
124+
onError: Function
125+
): LoadRequest {
122126
id += 1;
123127
const image = new window.Image();
124128
image.onerror = onError;
@@ -142,14 +146,56 @@ const ImageLoader = {
142146
setTimeout(onDecode, 0);
143147
}
144148
};
145-
image.src = uri;
149+
image.src = source.uri;
146150
requests[`${id}`] = image;
147-
return id;
151+
152+
return {
153+
cancel: () => ImageLoader.abort(id),
154+
requestId: id
155+
};
156+
},
157+
loadWithHeaders(
158+
source: ImageSource,
159+
onLoad: Function,
160+
onError: Function
161+
): LoadRequest {
162+
let loadRequest: LoadRequest;
163+
let uri: string;
164+
const abortCtrl = new AbortController();
165+
const request = new Request(source.uri, {
166+
headers: source.headers,
167+
signal: abortCtrl.signal
168+
});
169+
request.headers.append('accept', 'image/*');
170+
171+
fetch(request)
172+
.then((response) => response.blob())
173+
.then((blob) => {
174+
uri = URL.createObjectURL(blob);
175+
loadRequest = ImageLoader.load({ uri }, onLoad, onError);
176+
})
177+
.catch((error) => {
178+
if (error.name !== 'AbortError' && onError) {
179+
onError({ nativeEvent: error.message });
180+
}
181+
});
182+
183+
return {
184+
get requestId() {
185+
if (loadRequest) return loadRequest.requestId;
186+
return -1;
187+
},
188+
cancel: () => {
189+
abortCtrl.abort();
190+
if (loadRequest) loadRequest.cancel();
191+
URL.revokeObjectURL(uri);
192+
}
193+
};
148194
},
149195
prefetch(uri: string): Promise<void> {
150196
return new Promise((resolve, reject) => {
151197
ImageLoader.load(
152-
uri,
198+
{ uri },
153199
() => {
154200
// Add the uri to the cache so it can be immediately displayed when used
155201
// but also immediately remove it to correctly reflect that it has no active references
@@ -172,4 +218,14 @@ const ImageLoader = {
172218
}
173219
};
174220

221+
export type LoadRequest = {
222+
cancel: Function,
223+
requestId: number
224+
};
225+
226+
type ImageSource = {
227+
uri: string,
228+
headers: { [key: string]: string }
229+
};
230+
175231
export default ImageLoader;

0 commit comments

Comments
 (0)