|
| 1 | +import XHRLoader from 'loader/XHRLoader.js'; |
| 2 | +import XHRSettings from 'loader/XHRSettings.js'; |
| 3 | + |
| 4 | +// File level consts |
| 5 | + |
| 6 | +export const PENDING = 0; // file is in the load queue but not yet started |
| 7 | +export const LOADING = 1; // file has been started to load by the loader (onLoad called) |
| 8 | +export const LOADED = 2; // file has loaded successfully, awaiting processing |
| 9 | +export const FAILED = 3; // file failed to load |
| 10 | +export const PROCESSING = 4; // file is being processed (onProcess callback) |
| 11 | +export const COMPLETE = 5; // file has finished processing |
| 12 | +export const DESTROYED = 6; // file has been destroyed |
| 13 | + |
| 14 | +// Our base File object (from which all File Types extend) |
| 15 | + |
| 16 | +// key = user level file key (can be filename or other string based value) |
| 17 | +// url = the URL to load the file from, doesn't include baseURL or Path (which are both set by the Loader) |
| 18 | +// type = a user-level value that can control which cache the file is added to |
| 19 | + |
| 20 | +var File = function (key, url, type) |
| 21 | +{ |
| 22 | + if (!key) |
| 23 | + { |
| 24 | + console.warn('Invalid File key'); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + return { |
| 29 | + |
| 30 | + key: key, |
| 31 | + |
| 32 | + url: url, |
| 33 | + |
| 34 | + // Both of these are overridden by the BaseLoader (if being used) |
| 35 | + path: '', |
| 36 | + src: url, |
| 37 | + |
| 38 | + tag: '', // Tag this file, this is a non-unique string. For example you could tag a collection of files as 'level1', or 'mainmenu'. |
| 39 | + |
| 40 | + type: type, // the file type, i.e. 'image', 'json', etc which can be used to control which cache it gets added to |
| 41 | + |
| 42 | + state: PENDING, |
| 43 | + |
| 44 | + // Multipart file? (i.e. an atlas and its json) |
| 45 | + multipart: undefined, |
| 46 | + linkFile: undefined, |
| 47 | + |
| 48 | + // The actual processed file data |
| 49 | + data: undefined, |
| 50 | + |
| 51 | + // For CORs based loading. |
| 52 | + // If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set) |
| 53 | + crossOrigin: undefined, |
| 54 | + |
| 55 | + // Optionally set by the Promise returned from BaseLoader.addFile. |
| 56 | + resolve: undefined, |
| 57 | + reject: undefined, |
| 58 | + |
| 59 | + // maybe you have to set it in the Promise? |
| 60 | + processCallback: undefined, |
| 61 | + |
| 62 | + // xhr specific settings (ignored by TagLoaded files) |
| 63 | + xhr: XHRSettings('text'), |
| 64 | + |
| 65 | + onStateChange: function (value) { |
| 66 | + |
| 67 | + // console.log('onStateChange', this.url, 'from', this.state, 'to', value); |
| 68 | + |
| 69 | + if (this.state !== value) |
| 70 | + { |
| 71 | + this.state = value; |
| 72 | + |
| 73 | + // Loaded AND Processed |
| 74 | + if (value === COMPLETE) |
| 75 | + { |
| 76 | + // Part of a multipart load? |
| 77 | + if (this.multipart) |
| 78 | + { |
| 79 | + // Has the linked file loaded too? |
| 80 | + if (this.linkFile.state === COMPLETE && this.multipart.resolve) |
| 81 | + { |
| 82 | + // Send the Promise for the multipart file |
| 83 | + this.multipart.resolve(this.multipart); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Send the Promise for this file |
| 88 | + if (this.resolve) |
| 89 | + { |
| 90 | + this.resolve(this); |
| 91 | + } |
| 92 | + } |
| 93 | + else if (value === FAILED) |
| 94 | + { |
| 95 | + // Part of a multipart load? |
| 96 | + if (this.multipart) |
| 97 | + { |
| 98 | + if (this.multipart.reject) |
| 99 | + { |
| 100 | + // Send the Promise for the multipart file |
| 101 | + this.multipart.reject(this.multipart, error); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Send the Promise for this file |
| 106 | + if (this.reject) |
| 107 | + { |
| 108 | + this.reject(this); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + }, |
| 114 | + |
| 115 | + // These functions are usually overridden by the custom file types |
| 116 | + |
| 117 | + load: function (globalXHRSettings) { |
| 118 | + |
| 119 | + this.onStateChange(LOADING); |
| 120 | + |
| 121 | + // Returns a Promise from the XHRLoader |
| 122 | + return XHRLoader(this, globalXHRSettings); |
| 123 | + |
| 124 | + }, |
| 125 | + |
| 126 | + onLoad: function () { |
| 127 | + |
| 128 | + // If overridden it must set `state` to LOADED |
| 129 | + this.onStateChange(LOADED); |
| 130 | + |
| 131 | + }, |
| 132 | + |
| 133 | + onError: function () { |
| 134 | + |
| 135 | + // If overridden it must set `state` to FAILED |
| 136 | + this.onStateChange(FAILED); |
| 137 | + |
| 138 | + }, |
| 139 | + |
| 140 | + onProcess: function () { |
| 141 | + |
| 142 | + // If overridden it must set `state` to PROCESSING |
| 143 | + this.onStateChange(PROCESSING); |
| 144 | + |
| 145 | + }, |
| 146 | + |
| 147 | + onComplete: function () { |
| 148 | + |
| 149 | + // If overridden it must set `state` to COMPLETE |
| 150 | + this.onStateChange(COMPLETE); |
| 151 | + |
| 152 | + }, |
| 153 | + |
| 154 | + onDestroy: function () { |
| 155 | + |
| 156 | + // If overridden it must set `state` to DESTROYED |
| 157 | + this.onStateChange(DESTROYED); |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + }; |
| 162 | + |
| 163 | +}; |
| 164 | + |
| 165 | +module.exports = File; |
0 commit comments