| 1 | // FrontFrame to manage image loading and rendering |
| 2 | class FrontFrame { |
| 3 | constructor(name) { |
| 4 | this.name = name; |
| 5 | this.blob = null; |
| 6 | this.image = new Image(); |
| 7 | this.url = null; |
| 8 | this.loading = false; |
| 9 | this.loaded = false; |
| 10 | this.fresh = false; |
| 11 | |
| 12 | // Arrow functions automatically bind 'this' to the class instance |
| 13 | this.image.onload = () => { |
| 14 | this.loading = false; |
| 15 | this.loaded = true; |
| 16 | this.fresh = true; |
| 17 | }; |
| 18 | |
| 19 | this.image.onerror = () => { |
| 20 | this.loading = false; |
| 21 | this.loaded = false; |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | load(blob) { |
| 26 | // Reset before loading a new blob |
| 27 | this.reset(); |
| 28 | |
| 29 | if (!blob) return; |
| 30 | |
| 31 | this.blob = blob; |
| 32 | this.url = URL.createObjectURL(this.blob); |
| 33 | this.loading = true; |
| 34 | this.loaded = false; |
| 35 | this.fresh = false; |
| 36 | this.image.src = this.url; |
| 37 | } |
| 38 | |
| 39 | reset() { |
| 40 | this.loading = false; |
| 41 | this.loaded = false; |
| 42 | if (this.blob) { |
| 43 | URL.revokeObjectURL(this.url); |
| 44 | this.blob = null; |
| 45 | this.url = null; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | consume() { |
| 50 | if (!this.fresh) return null; |
| 51 | this.fresh = false; |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | destroy() { |
| 56 | this.reset(); |
| 57 | this.image = null; |
| 58 | } |
| 59 | } |
nothing calls this directly
no outgoing calls
no test coverage detected