|
| 1 | +import Foundation |
| 2 | + |
| 3 | +// MARK: - Infinite Query Types |
| 4 | + |
| 5 | +/// Function signature for infinite query functions |
| 6 | +public typealias InfiniteQueryFunction<TData: Sendable, TKey: QueryKey, TPageParam: Sendable & Codable> = |
| 7 | + @Sendable ( |
| 8 | + TKey, |
| 9 | + TPageParam? |
| 10 | + ) async throws -> TData |
| 11 | + |
| 12 | +/// Function to get the next page parameter |
| 13 | +public typealias GetNextPageParamFunction<TData: Sendable, TPageParam: Sendable & Codable> = |
| 14 | + @Sendable ([TData]) -> TPageParam? |
| 15 | + |
| 16 | +/// Function to get the previous page parameter |
| 17 | +public typealias GetPreviousPageParamFunction<TData: Sendable, TPageParam: Sendable & Codable> = |
| 18 | + @Sendable ([TData]) -> TPageParam? |
| 19 | + |
| 20 | +/// Configuration options for infinite queries |
| 21 | +/// Equivalent to TanStack Query's InfiniteQueryOptions |
| 22 | +public struct InfiniteQueryOptions< |
| 23 | + TData: Sendable, |
| 24 | + TError: Error & Sendable & Codable, |
| 25 | + TKey: QueryKey, |
| 26 | + TPageParam: Sendable & Codable & Equatable |
| 27 | +>: Sendable, Equatable { |
| 28 | + /// The query key that uniquely identifies this query |
| 29 | + public let queryKey: TKey |
| 30 | + /// The function that will be called to fetch data pages |
| 31 | + public let queryFn: InfiniteQueryFunction<TData, TKey, TPageParam> |
| 32 | + /// Function to determine the next page parameter |
| 33 | + public let getNextPageParam: GetNextPageParamFunction<TData, TPageParam>? |
| 34 | + /// Function to determine the previous page parameter |
| 35 | + public let getPreviousPageParam: GetPreviousPageParamFunction<TData, TPageParam>? |
| 36 | + /// Initial page parameter for the first page |
| 37 | + public let initialPageParam: TPageParam? |
| 38 | + /// Maximum number of pages to retain |
| 39 | + public let maxPages: Int? |
| 40 | + /// Configuration for retry behavior |
| 41 | + public let retryConfig: RetryConfig |
| 42 | + /// Network behavior configuration |
| 43 | + public let networkMode: NetworkMode |
| 44 | + /// Time after which data is considered stale (in seconds) |
| 45 | + public let staleTime: TimeInterval |
| 46 | + /// Time after which inactive queries are garbage collected (in seconds) |
| 47 | + public let gcTime: TimeInterval |
| 48 | + /// Configuration for automatic refetching triggers |
| 49 | + public let refetchTriggers: RefetchTriggers |
| 50 | + /// Specific behavior for view appear events |
| 51 | + public let refetchOnAppear: RefetchOnAppear |
| 52 | + /// Whether to use structural sharing for performance |
| 53 | + public let structuralSharing: Bool |
| 54 | + /// Arbitrary metadata for this query |
| 55 | + public let meta: QueryMeta? |
| 56 | + /// Whether this query is enabled (will fetch automatically) |
| 57 | + public let enabled: Bool |
| 58 | + |
| 59 | + public static func == ( |
| 60 | + lhs: InfiniteQueryOptions<TData, TError, TKey, TPageParam>, |
| 61 | + rhs: InfiniteQueryOptions<TData, TError, TKey, TPageParam> |
| 62 | + ) -> Bool { |
| 63 | + lhs.queryKey == rhs.queryKey && |
| 64 | + lhs.initialPageParam == rhs.initialPageParam && |
| 65 | + lhs.maxPages == rhs.maxPages && |
| 66 | + lhs.staleTime == rhs.staleTime && |
| 67 | + lhs.gcTime == rhs.gcTime && |
| 68 | + lhs.refetchTriggers == rhs.refetchTriggers && |
| 69 | + lhs.refetchOnAppear == rhs.refetchOnAppear && |
| 70 | + lhs.enabled == rhs.enabled |
| 71 | + } |
| 72 | + |
| 73 | + public init( |
| 74 | + queryKey: TKey, |
| 75 | + queryFn: @escaping InfiniteQueryFunction<TData, TKey, TPageParam>, |
| 76 | + getNextPageParam: GetNextPageParamFunction<TData, TPageParam>? = nil, |
| 77 | + getPreviousPageParam: GetPreviousPageParamFunction<TData, TPageParam>? = nil, |
| 78 | + initialPageParam: TPageParam? = nil, |
| 79 | + maxPages: Int? = nil, |
| 80 | + retryConfig: RetryConfig = RetryConfig(), |
| 81 | + networkMode: NetworkMode = .online, |
| 82 | + staleTime: TimeInterval = 0, |
| 83 | + gcTime: TimeInterval = defaultGcTime, |
| 84 | + refetchTriggers: RefetchTriggers = .default, |
| 85 | + refetchOnAppear: RefetchOnAppear = .always, |
| 86 | + structuralSharing: Bool = true, |
| 87 | + meta: QueryMeta? = nil, |
| 88 | + enabled: Bool = true |
| 89 | + ) { |
| 90 | + self.queryKey = queryKey |
| 91 | + self.queryFn = queryFn |
| 92 | + self.getNextPageParam = getNextPageParam |
| 93 | + self.getPreviousPageParam = getPreviousPageParam |
| 94 | + self.initialPageParam = initialPageParam |
| 95 | + self.maxPages = maxPages |
| 96 | + self.retryConfig = retryConfig |
| 97 | + self.networkMode = networkMode |
| 98 | + self.staleTime = staleTime |
| 99 | + self.gcTime = gcTime |
| 100 | + self.refetchTriggers = refetchTriggers |
| 101 | + self.refetchOnAppear = refetchOnAppear |
| 102 | + self.structuralSharing = structuralSharing |
| 103 | + self.meta = meta |
| 104 | + self.enabled = enabled |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// Container for infinite query data with pagination support |
| 109 | +/// Equivalent to TanStack Query's InfiniteData |
| 110 | +public struct InfiniteData<TData: Sendable, TPageParam: Sendable & Codable>: Sendable { |
| 111 | + /// Array of pages containing the actual data |
| 112 | + public let pages: [TData] |
| 113 | + /// Array of page parameters used to fetch each page |
| 114 | + public let pageParams: [TPageParam?] |
| 115 | + |
| 116 | + public init(pages: [TData] = [], pageParams: [TPageParam?] = []) { |
| 117 | + self.pages = pages |
| 118 | + self.pageParams = pageParams |
| 119 | + } |
| 120 | + |
| 121 | + /// Add a new page to the end |
| 122 | + public func appendPage(_ page: TData, param: TPageParam?) -> InfiniteData<TData, TPageParam> { |
| 123 | + var newPages = pages |
| 124 | + var newParams = pageParams |
| 125 | + newPages.append(page) |
| 126 | + newParams.append(param) |
| 127 | + return Self(pages: newPages, pageParams: newParams) |
| 128 | + } |
| 129 | + |
| 130 | + /// Add a new page to the beginning |
| 131 | + public func prependPage(_ page: TData, param: TPageParam?) -> InfiniteData<TData, TPageParam> { |
| 132 | + var newPages = pages |
| 133 | + var newParams = pageParams |
| 134 | + newPages.insert(page, at: 0) |
| 135 | + newParams.insert(param, at: 0) |
| 136 | + return Self(pages: newPages, pageParams: newParams) |
| 137 | + } |
| 138 | + |
| 139 | + /// Remove pages beyond the specified maximum |
| 140 | + public func limitPages(to maxPages: Int) -> InfiniteData<TData, TPageParam> { |
| 141 | + guard maxPages > 0, pages.count > maxPages else { return self } |
| 142 | + |
| 143 | + let limitedPages = Array(pages.prefix(maxPages)) |
| 144 | + let limitedParams = Array(pageParams.prefix(maxPages)) |
| 145 | + return Self(pages: limitedPages, pageParams: limitedParams) |
| 146 | + } |
| 147 | + |
| 148 | + /// Get the total number of pages |
| 149 | + public var pageCount: Int { |
| 150 | + pages.count |
| 151 | + } |
| 152 | + |
| 153 | + /// Check if there are any pages |
| 154 | + public var isEmpty: Bool { |
| 155 | + pages.isEmpty |
| 156 | + } |
| 157 | + |
| 158 | + /// Get the last page parameter (for next page fetching) |
| 159 | + public var lastPageParam: TPageParam? { |
| 160 | + pageParams.last.flatMap(\.self) |
| 161 | + } |
| 162 | + |
| 163 | + /// Get the first page parameter (for previous page fetching) |
| 164 | + public var firstPageParam: TPageParam? { |
| 165 | + pageParams.first.flatMap(\.self) |
| 166 | + } |
| 167 | + |
| 168 | + /// Flatten all pages into a single array if TData is a collection |
| 169 | + public func flatMap<Element>() -> [Element] where TData == [Element] { |
| 170 | + pages.flatMap(\.self) |
| 171 | + } |
| 172 | +} |
0 commit comments