diff --git a/src/listener.js b/src/listener.js index 2944ef3..3b92ccf 100644 --- a/src/listener.js +++ b/src/listener.js @@ -149,6 +149,9 @@ export default class ReactiveListener { } if (this.state.rendered && this.state.loaded) return if (this._imageCache.has(this.src)) { + const { naturalHeight, naturalWidth } = this._imageCache.get(this.src) + this.naturalHeight = naturalHeight + this.naturalWidth = naturalWidth this.state.loaded = true this.render('loaded', true) this.state.rendered = true @@ -172,7 +175,7 @@ export default class ReactiveListener { this.record('loadEnd') this.render('loaded', false) this.state.rendered = true - this._imageCache.add(this.src) + this._imageCache.add(data) onFinish() }, err => { !this.options.silent && console.error(err) diff --git a/src/util.js b/src/util.js index 42a20ec..9f7253c 100644 --- a/src/util.js +++ b/src/util.js @@ -311,17 +311,23 @@ class ImageCache { } has (key) { - return this._caches.indexOf(key) > -1 + const img = this.get(key) + return !!img } - add (key) { - if (this.has(key)) return - this._caches.push(key) + add (data) { + if (this.has(data.src)) return + this._caches.push(data) if (this._caches.length > this.options.max) { this.free() } } + get (key) { + const imgs = this._caches.filter(t => t.src === key) + return imgs.find(t => t.src === key) + } + free () { this._caches.shift() }