@@ -104,18 +104,18 @@ public struct LaTeX: View {
104104
105105 /// The package's shared data cache.
106106 public static var dataCache : NSCache < NSString , NSData > {
107- Renderer . shared. dataCache
107+ Renderer . shared. cache . dataCache
108108 }
109109
110110#if os(macOS)
111111 /// The package's shared image cache.
112112 public static var imageCache : NSCache < NSString , NSImage > {
113- Renderer . shared. imageCache
113+ Renderer . shared. cache . imageCache
114114 }
115115#else
116116 /// The package's shared image cache.
117117 public static var imageCache : NSCache < NSString , UIImage > {
118- Renderer . shared. imageCache
118+ Renderer . shared. cache . imageCache
119119 }
120120#endif
121121
@@ -159,22 +159,6 @@ public struct LaTeX: View {
159159 /// The view's render state.
160160 @StateObject private var renderState : LaTeXRenderState
161161
162- /// Renders the blocks synchronously.
163- ///
164- /// This will block whatever thread you call it on.
165- private var syncBlocks : [ ComponentBlock ] {
166- Renderer . shared. render (
167- blocks: Parser . parse ( unencodeHTML ? latex. htmlUnescape ( ) : latex, mode: parsingMode) ,
168- font: font ?? . body,
169- displayScale: displayScale,
170- texOptions: texOptions)
171- }
172-
173- /// The TeX options to use when submitting requests to the renderer.
174- private var texOptions : TeXInputProcessorOptions {
175- TeXInputProcessorOptions ( processEscapes: processEscapes, errorMode: errorMode)
176- }
177-
178162 // MARK: Initializers
179163
180164 /// Initializes a view with a LaTeX input string.
@@ -190,21 +174,22 @@ public struct LaTeX: View {
190174 public var body : some View {
191175 VStack ( spacing: 0 ) {
192176 if renderState. rendered {
177+ // If our blocks have been rendered, display them
193178 bodyWithBlocks ( renderState. blocks)
194179 }
180+ else if isCached ( ) {
181+ // If our blocks are cached, display them
182+ bodyWithBlocks ( renderSync ( ) )
183+ }
195184 else {
185+ // The view is not rendered nor cached
196186 switch renderingStyle {
197- case . empty:
198- Text ( " " )
199- . task ( render)
200- case . original:
201- Text ( latex)
202- . task ( render)
203- case . progress:
204- ProgressView ( )
205- . task ( render)
187+ case . empty, . original, . progress:
188+ // Render the components asynchronously
189+ loadingView ( ) . task ( renderAsync)
206190 case . wait:
207- bodyWithBlocks ( syncBlocks)
191+ // Render the components synchronously
192+ bodyWithBlocks ( renderSync ( ) )
208193 }
209194 }
210195 }
@@ -220,7 +205,7 @@ extension LaTeX {
220205 /// Preloads the view's SVG and image data.
221206 public func preload( ) {
222207 Task {
223- await render ( )
208+ await renderAsync ( )
224209 }
225210 }
226211}
@@ -229,14 +214,45 @@ extension LaTeX {
229214
230215extension LaTeX {
231216
217+ /// Checks the renderer's caches for the current view.
218+ ///
219+ /// If this method returns `true`, then there is no need to do an async
220+ /// render.
221+ ///
222+ /// - Returns: A boolean indicating whether the components to the view are
223+ /// cached.
224+ private func isCached( ) -> Bool {
225+ renderState. isCached (
226+ unencodeHTML: unencodeHTML,
227+ parsingMode: parsingMode,
228+ processEscapes: processEscapes,
229+ errorMode: errorMode,
230+ font: font ?? . body,
231+ displayScale: displayScale)
232+ }
233+
232234 /// Renders the view's components.
233- @Sendable private func render ( ) async {
235+ @Sendable private func renderAsync ( ) async {
234236 await renderState. render (
235237 unencodeHTML: unencodeHTML,
236238 parsingMode: parsingMode,
237- font: font,
238- displayScale: displayScale,
239- texOptions: texOptions)
239+ processEscapes: processEscapes,
240+ errorMode: errorMode,
241+ font: font ?? . body,
242+ displayScale: displayScale)
243+ }
244+
245+ /// Renders the view's components synchronously.
246+ ///
247+ /// - Returns: The rendered components.
248+ private func renderSync( ) -> [ ComponentBlock ] {
249+ renderState. renderSync (
250+ unencodeHTML: unencodeHTML,
251+ parsingMode: parsingMode,
252+ processEscapes: processEscapes,
253+ errorMode: errorMode,
254+ font: font ?? . body,
255+ displayScale: displayScale)
240256 }
241257
242258 /// Creates the view's body based on its block mode.
@@ -254,6 +270,22 @@ extension LaTeX {
254270 }
255271 }
256272
273+ @MainActor @ViewBuilder private func loadingView( ) -> some View {
274+ switch renderingStyle {
275+ case . empty:
276+ Text ( " " )
277+ . task ( renderAsync)
278+ case . original:
279+ Text ( latex)
280+ . task ( renderAsync)
281+ case . progress:
282+ ProgressView ( )
283+ . task ( renderAsync)
284+ default :
285+ EmptyView ( )
286+ }
287+ }
288+
257289}
258290
259291@available ( iOS 16 . 1 , * )
0 commit comments