Skip to content

Commit 49771e3

Browse files
committed
ImageBitmapLoader: Make error handling more robust.
1 parent f895904 commit 49771e3

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/loaders/ImageBitmapLoader.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Cache } from './Cache.js';
22
import { Loader } from './Loader.js';
33

4+
const _errorMap = new WeakMap();
5+
46
/**
57
* A loader for loading images as an [ImageBitmap]{@link https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap}.
68
* An `ImageBitmap` provides an asynchronous and resource efficient pathway to prepare
@@ -111,13 +113,27 @@ class ImageBitmapLoader extends Loader {
111113

112114
cached.then( imageBitmap => {
113115

114-
if ( onLoad ) onLoad( imageBitmap );
116+
// The code checks if there is an error for the cached promise. If so, throw
117+
// a new error to force the code flow into the catch() block.
115118

116-
scope.manager.itemEnd( url );
119+
if ( _errorMap.has( cached ) === true ) {
120+
121+
throw new Error();
122+
123+
} else {
124+
125+
if ( onLoad ) onLoad( imageBitmap );
117126

118-
} ).catch( e => {
127+
scope.manager.itemEnd( url );
119128

120-
if ( onError ) onError( e );
129+
}
130+
131+
} ).catch( () => {
132+
133+
if ( onError ) onError( _errorMap.get( cached ) );
134+
135+
scope.manager.itemError( url );
136+
scope.manager.itemEnd( url );
121137

122138
} );
123139
return;
@@ -163,6 +179,8 @@ class ImageBitmapLoader extends Loader {
163179

164180
if ( onError ) onError( e );
165181

182+
_errorMap.set( promise, e );
183+
166184
Cache.remove( url );
167185

168186
scope.manager.itemError( url );

0 commit comments

Comments
 (0)