* Loads the SMAA data images. * * @param {Function} [onLoad] - A callback that receives the search image and area image as a pair. * @param {Function} [onError] - An error callback that receives the URL of the image that failed to load. * @return {Promise } A promise that returns the
(onLoad = () => {}, onError = null)
| 20 | */ |
| 21 | |
| 22 | load(onLoad = () => {}, onError = null) { |
| 23 | |
| 24 | // Conform to the signature (url, onLoad, onProgress, onError). |
| 25 | if(arguments.length === 4) { |
| 26 | |
| 27 | onLoad = arguments[1]; |
| 28 | onError = arguments[3]; |
| 29 | |
| 30 | } else if(arguments.length === 3 || typeof arguments[0] !== "function") { |
| 31 | |
| 32 | onLoad = arguments[1]; |
| 33 | onError = null; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | const externalManager = this.manager; |
| 38 | const internalManager = new LoadingManager(); |
| 39 | |
| 40 | return new Promise((resolve, reject) => { |
| 41 | |
| 42 | const searchImage = new Image(); |
| 43 | const areaImage = new Image(); |
| 44 | |
| 45 | internalManager.onError = (url) => { |
| 46 | |
| 47 | externalManager.itemError(url); |
| 48 | |
| 49 | if(onError !== null) { |
| 50 | |
| 51 | onError(`Failed to load ${url}`); |
| 52 | resolve(); |
| 53 | |
| 54 | } else { |
| 55 | |
| 56 | reject(`Failed to load ${url}`); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | }; |
| 61 | |
| 62 | internalManager.onLoad = () => { |
| 63 | |
| 64 | const result = [searchImage, areaImage]; |
| 65 | onLoad(result); |
| 66 | resolve(result); |
| 67 | |
| 68 | }; |
| 69 | |
| 70 | searchImage.addEventListener("error", (e) => { |
| 71 | |
| 72 | internalManager.itemError("smaa-search"); |
| 73 | |
| 74 | }); |
| 75 | |
| 76 | areaImage.addEventListener("error", (e) => { |
| 77 | |
| 78 | internalManager.itemError("smaa-area"); |
| 79 |