|
|
@@ -2,6 +2,8 @@ import { Cache } from './Cache.js';
|
|
|
import { Loader } from './Loader.js';
|
|
|
import { createElementNS } from '../utils.js';
|
|
|
|
|
|
+const _loading = new WeakMap();
|
|
|
+
|
|
|
/**
|
|
|
* A loader for loading images. The class loads images with the HTML `Image` API.
|
|
|
*
|
|
|
@@ -52,15 +54,32 @@ class ImageLoader extends Loader {
|
|
|
|
|
|
if ( cached !== undefined ) {
|
|
|
|
|
|
- scope.manager.itemStart( url );
|
|
|
+ if ( cached.complete === true ) {
|
|
|
+
|
|
|
+ scope.manager.itemStart( url );
|
|
|
+
|
|
|
+ setTimeout( function () {
|
|
|
+
|
|
|
+ if ( onLoad ) onLoad( cached );
|
|
|
+
|
|
|
+ scope.manager.itemEnd( url );
|
|
|
+
|
|
|
+ }, 0 );
|
|
|
|
|
|
- setTimeout( function () {
|
|
|
+ } else {
|
|
|
|
|
|
- if ( onLoad ) onLoad( cached );
|
|
|
+ let arr = _loading.get( cached );
|
|
|
|
|
|
- scope.manager.itemEnd( url );
|
|
|
+ if ( arr === undefined ) {
|
|
|
|
|
|
- }, 0 );
|
|
|
+ arr = [];
|
|
|
+ _loading.set( cached, arr );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ arr.push( { onLoad, onError } );
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
return cached;
|
|
|
|
|
|
@@ -72,10 +91,21 @@ class ImageLoader extends Loader {
|
|
|
|
|
|
removeEventListeners();
|
|
|
|
|
|
- Cache.add( url, this );
|
|
|
-
|
|
|
if ( onLoad ) onLoad( this );
|
|
|
|
|
|
+ //
|
|
|
+
|
|
|
+ const callbacks = _loading.get( this ) || [];
|
|
|
+
|
|
|
+ for ( let i = 0; i < callbacks.length; i ++ ) {
|
|
|
+
|
|
|
+ const callback = callbacks[ i ];
|
|
|
+ if ( callback.onLoad ) callback.onLoad( this );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ _loading.delete( this );
|
|
|
+
|
|
|
scope.manager.itemEnd( url );
|
|
|
|
|
|
}
|
|
|
@@ -86,6 +116,22 @@ class ImageLoader extends Loader {
|
|
|
|
|
|
if ( onError ) onError( event );
|
|
|
|
|
|
+ Cache.remove( url );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ const callbacks = _loading.get( this ) || [];
|
|
|
+
|
|
|
+ for ( let i = 0; i < callbacks.length; i ++ ) {
|
|
|
+
|
|
|
+ const callback = callbacks[ i ];
|
|
|
+ if ( callback.onError ) callback.onError( event );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ _loading.delete( this );
|
|
|
+
|
|
|
+
|
|
|
scope.manager.itemError( url );
|
|
|
scope.manager.itemEnd( url );
|
|
|
|
|
|
@@ -107,6 +153,7 @@ class ImageLoader extends Loader {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ Cache.add( url, image );
|
|
|
scope.manager.itemStart( url );
|
|
|
|
|
|
image.src = url;
|