From 620f1bf874f5ab0cf3e8c7d643e114904e2c85a3 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 28 Oct 2020 16:13:58 +0800 Subject: [PATCH 1/2] Fix issue 370 --- src/listener.js | 5 ++++- src/util.js | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) 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 c47d821..da558d7 100644 --- a/src/util.js +++ b/src/util.js @@ -311,7 +311,8 @@ class ImageCache { } has (key) { - return this._caches.indexOf(key) > -1 + const img = this.get(key) + return !!img } add (key) { @@ -322,6 +323,11 @@ class ImageCache { } } + get (key) { + const imgs = this._caches.filter(t => t.src === key) + return imgs.find(t => t.src === key) + } + free () { this._caches.shift() } From abc7fb4cc032dca9076e2646bd5660b2a419cb32 Mon Sep 17 00:00:00 2001 From: dota17 Date: Tue, 29 Dec 2020 15:27:50 +0800 Subject: [PATCH 2/2] Modify the add function. Use src as the unique identifier. --- src/util.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util.js b/src/util.js index a52422a..9f7253c 100644 --- a/src/util.js +++ b/src/util.js @@ -315,9 +315,9 @@ class ImageCache { 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() }