-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Unify dimensions cache between lightbox and feed #6047
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c4c0195
Remove useless memo
gaearon 9b07603
Use explicit values when useImageAspectRatio doesn't know
gaearon fc0f7bb
Check cache early
gaearon 71c4add
Handle src change
gaearon 33f4ab2
Rewrite image-sizes.fetch to avoid mixing async styles
gaearon af11271
Make image-sizes LRU
gaearon 56740a3
Rm unused fields
gaearon 412426b
Derive aspect on the fly
gaearon fe33a21
Factor useImageDimensions out of useImageAspectRatio
gaearon 6b47864
Move useImageDimensions into image-sizes
gaearon 0ae5033
Make lightbox use the same cache
gaearon 79a61ad
Wire up known dimensions to the lightbox
gaearon c95f8e8
Handle division by zero in the hook
gaearon 5e1a2fc
Use safe aspect for lightbox calculations
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,93 @@ | ||
import {useEffect, useState} from 'react' | ||
import {Image} from 'react-native' | ||
|
||
import type {Dimensions} from '#/lib/media/types' | ||
|
||
const sizes: Map<string, Dimensions> = new Map() | ||
type CacheStorageItem<T> = {key: string; value: T} | ||
const createCache = <T>(cacheSize: number) => ({ | ||
_storage: [] as CacheStorageItem<T>[], | ||
get(key: string) { | ||
const {value} = | ||
this._storage.find(({key: storageKey}) => storageKey === key) || {} | ||
return value | ||
}, | ||
set(key: string, value: T) { | ||
if (this._storage.length >= cacheSize) { | ||
this._storage.shift() | ||
} | ||
this._storage.push({key, value}) | ||
}, | ||
}) | ||
|
||
const sizes = createCache<Dimensions>(50) | ||
const activeRequests: Map<string, Promise<Dimensions>> = new Map() | ||
|
||
export function get(uri: string): Dimensions | undefined { | ||
return sizes.get(uri) | ||
} | ||
|
||
export async function fetch(uri: string): Promise<Dimensions> { | ||
const Dimensions = sizes.get(uri) | ||
if (Dimensions) { | ||
return Dimensions | ||
export function fetch(uri: string): Promise<Dimensions> { | ||
const dims = sizes.get(uri) | ||
if (dims) { | ||
return Promise.resolve(dims) | ||
} | ||
const activeRequest = activeRequests.get(uri) | ||
if (activeRequest) { | ||
return activeRequest | ||
} | ||
const prom = new Promise<Dimensions>((resolve, reject) => { | ||
Image.getSize( | ||
uri, | ||
(width: number, height: number) => { | ||
const size = {width, height} | ||
sizes.set(uri, size) | ||
resolve(size) | ||
}, | ||
(err: any) => { | ||
console.error('Failed to fetch image dimensions for', uri, err) | ||
reject(new Error('Could not fetch dimensions')) | ||
}, | ||
) | ||
}).finally(() => { | ||
activeRequests.delete(uri) | ||
}) | ||
activeRequests.set(uri, prom) | ||
return prom | ||
} | ||
|
||
export function useImageDimensions({ | ||
src, | ||
knownDimensions, | ||
}: { | ||
src: string | ||
knownDimensions: Dimensions | null | ||
}): [number | undefined, Dimensions | undefined] { | ||
const [dims, setDims] = useState(() => knownDimensions ?? get(src)) | ||
const [prevSrc, setPrevSrc] = useState(src) | ||
if (src !== prevSrc) { | ||
setDims(knownDimensions ?? get(src)) | ||
setPrevSrc(src) | ||
} | ||
|
||
const prom = | ||
activeRequests.get(uri) || | ||
new Promise<Dimensions>(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}) | ||
}, | ||
) | ||
useEffect(() => { | ||
let aborted = false | ||
if (dims !== undefined) return | ||
fetch(src).then(newDims => { | ||
if (aborted) return | ||
setDims(newDims) | ||
}) | ||
activeRequests.set(uri, prom) | ||
const res = await prom | ||
activeRequests.delete(uri) | ||
sizes.set(uri, res) | ||
return res | ||
return () => { | ||
aborted = true | ||
} | ||
}, [dims, setDims, src]) | ||
|
||
let aspectRatio: number | undefined | ||
if (dims) { | ||
aspectRatio = dims.width / dims.height | ||
if (Number.isNaN(aspectRatio)) { | ||
aspectRatio = undefined | ||
} | ||
} | ||
|
||
return [aspectRatio, dims] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 0 additions & 93 deletions
93
src/view/com/lightbox/ImageViewing/hooks/useImageDimensions.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is copypasta from deleted file