Support batch image upload (#2597)

This commit is contained in:
bymyself
2025-02-17 11:56:21 -07:00
committed by GitHub
parent 79452ce267
commit 141e64354c
11 changed files with 195 additions and 104 deletions

View File

@@ -0,0 +1,31 @@
/**
* Creates a getter/setter pair that transforms values on access if they have changed.
* Does not observe deep changes.
*
* @example
* const { get, set } = useValueTransform<ResultItem[], string[]>(
* items => items.map(formatPath)
* )
*
* Object.defineProperty(obj, 'value', { get, set })
*/
export function useValueTransform<Internal, External>(
transform: (value: Internal) => External,
initialValue: Internal
) {
let internalValue: Internal = initialValue
let cachedValue: External = transform(initialValue)
let isChanged = false
return {
get: () => {
if (!isChanged) return cachedValue
cachedValue = transform(internalValue)
return cachedValue
},
set: (value: Internal) => {
isChanged = true
internalValue = value
}
}
}