* Introduce an image sizes cache to improve feed layouts (close #213) * Clear out resolved promises from the image cache
This commit is contained in:
parent
c1d454b7cf
commit
858d4c8c88
7 changed files with 92 additions and 30 deletions
37
src/state/models/cache/image-sizes.ts
vendored
Normal file
37
src/state/models/cache/image-sizes.ts
vendored
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import {Image} from 'react-native'
|
||||
import {Dim} from 'lib/media/manip'
|
||||
|
||||
export class ImageSizesCache {
|
||||
sizes: Map<string, Dim> = new Map()
|
||||
private activeRequests: Map<string, Promise<Dim>> = new Map()
|
||||
|
||||
constructor() {}
|
||||
|
||||
get(uri: string): Dim | undefined {
|
||||
return this.sizes.get(uri)
|
||||
}
|
||||
|
||||
async fetch(uri: string): Promise<Dim> {
|
||||
const dim = this.sizes.get(uri)
|
||||
if (dim) {
|
||||
return dim
|
||||
}
|
||||
const prom =
|
||||
this.activeRequests.get(uri) ||
|
||||
new Promise<Dim>(resolve => {
|
||||
Image.getSize(
|
||||
uri,
|
||||
(width: number, height: number) => resolve({width, height}),
|
||||
(err: any) => {
|
||||
console.error('Failed to fetch image dimensions for', uri, err)
|
||||
resolve({width: 0, height: 0})
|
||||
},
|
||||
)
|
||||
})
|
||||
this.activeRequests.set(uri, prom)
|
||||
const res = await prom
|
||||
this.activeRequests.delete(uri)
|
||||
this.sizes.set(uri, res)
|
||||
return res
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue