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

345 feature instant resolution for already fetched images in prefetch #349

Merged
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
19 changes: 17 additions & 2 deletions android/src/main/java/com/turboimage/TurboImageModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import okhttp3.Headers

class TurboImageModule(private val context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
private var imageLoader: ImageLoader? = null
private var imageLoader: ImageLoader? = null

override fun getName(): String = REACT_CLASS

@ReactMethod
fun prefetch(sources: ReadableArray, cachePolicy: String, promise: Promise) {
var completedRequestCount = 0
fun handleRequestCount() {
completedRequestCount++
if (sources.size() == completedRequestCount) {
promise.resolve(true)
}
}

val imageRequests = sources.toArrayList().map { source ->
val uri = (source as HashMap<*, *>)["uri"] as String
val headers = source["headers"] as? HashMap<*, *>
Expand All @@ -28,10 +36,18 @@ class TurboImageModule(private val context: ReactApplicationContext) :
ImageRequest.Builder(context)
.headers(headersBuilder.build())
.data(uri)
.listener(
onSuccess = { _, _ -> handleRequestCount() },
onError = { _, _ -> handleRequestCount() }
)
.build()
} else {
ImageRequest.Builder(context)
.data(uri)
.listener(
onSuccess = { _, _ -> handleRequestCount() },
onError = { _, _ -> handleRequestCount() }
)
.build()
}
}
Expand All @@ -41,7 +57,6 @@ class TurboImageModule(private val context: ReactApplicationContext) :
imageRequests.forEach { imageRequest ->
imageLoader?.enqueue(imageRequest)
}
promise.resolve("Success")
}

@ReactMethod
Expand Down
6 changes: 4 additions & 2 deletions ios/TurboImageViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ extension TurboImageViewManager {
prefetcher = ImagePrefetcher()
}
prefetcher?.startPrefetching(with: imageRequests)
resolve("Success")
prefetcher?.didComplete = {
resolve(true)
}
}

@objc
Expand Down Expand Up @@ -69,7 +71,7 @@ extension TurboImageViewManager {
ImageCache.shared.removeAll()
resolve("Success")
}

@objc
func clearDiskCache(_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface TurboImageProps extends AccessibilityProps, ViewProps {
}

export type TurboImageApi = {
prefetch: (sources: Source[], cachePolicy?: CachePolicy) => Promise<void>;
prefetch: (sources: Source[], cachePolicy?: CachePolicy) => Promise<boolean>;
dispose: (sources: Source[]) => Promise<void>;
clearMemoryCache: () => Promise<void>;
clearDiskCache: () => Promise<void>;
Expand Down
Loading