Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down