Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 370. Add the naturalHeight and naturalWidth values of the image to the image cache. #442

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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