Skip to content

Commit 1e5181a

Browse files
authored
Merge pull request #29 from colinc86/develop
Develop
2 parents 60a526e + 6ab5c51 commit 1e5181a

File tree

6 files changed

+41
-25
lines changed

6 files changed

+41
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ It won't
4848
Add the dependency to your package manifest file.
4949

5050
```swift
51-
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.3.0")
51+
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.3.1")
5252
```
5353

5454
## ⌨️ Usage

Sources/LaTeXSwiftUI/LaTeX.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public struct LaTeX: View {
159159
// MARK: Private properties
160160

161161
/// The view's renderer.
162-
@StateObject private var renderer: Renderer
162+
@StateObject private var renderer = Renderer()
163163

164164
/// The view's preload task, if any.
165165
@State private var preloadTask: Task<(), Never>?
@@ -171,7 +171,6 @@ public struct LaTeX: View {
171171
/// - Parameter latex: The LaTeX input.
172172
public init(_ latex: String) {
173173
self.latex = latex
174-
_renderer = StateObject(wrappedValue: Renderer(latex: latex))
175174
}
176175

177176
// MARK: View body
@@ -239,6 +238,7 @@ extension LaTeX {
239238
/// cached.
240239
private func isCached() -> Bool {
241240
renderer.isCached(
241+
latex: latex,
242242
unencodeHTML: unencodeHTML,
243243
parsingMode: parsingMode,
244244
processEscapes: processEscapes,
@@ -250,6 +250,7 @@ extension LaTeX {
250250
/// Renders the view's components.
251251
@Sendable private func renderAsync() async {
252252
await renderer.render(
253+
latex: latex,
253254
unencodeHTML: unencodeHTML,
254255
parsingMode: parsingMode,
255256
processEscapes: processEscapes,
@@ -263,6 +264,7 @@ extension LaTeX {
263264
/// - Returns: The rendered components.
264265
private func renderSync() -> [ComponentBlock] {
265266
renderer.renderSync(
267+
latex: latex,
266268
unencodeHTML: unencodeHTML,
267269
parsingMode: parsingMode,
268270
processEscapes: processEscapes,

Sources/LaTeXSwiftUI/Models/Renderer.swift

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,22 @@ import Cocoa
3838
/// values.
3939
internal class Renderer: ObservableObject {
4040

41-
// MARK: Public properties
41+
// MARK: Types
42+
43+
/// A set of values used to create an array of parsed component blocks.
44+
struct ParsingSource: Equatable {
45+
46+
/// The LaTeX input.
47+
let latex: String
48+
49+
/// Whether or not the HTML should be unencoded.
50+
let unencodeHTML: Bool
51+
52+
/// The parsing mode.
53+
let parsingMode: LaTeX.ParsingMode
54+
}
4255

43-
/// The view's input string.
44-
let latex: String
56+
// MARK: Public properties
4557

4658
/// Whether or not the view's blocks have been rendered.
4759
@MainActor @Published var rendered: Bool = false
@@ -57,18 +69,12 @@ internal class Renderer: ObservableObject {
5769
/// The LaTeX input's parsed blocks.
5870
private var _parsedBlocks: [ComponentBlock]? = nil
5971

72+
/// The set of values used to create the parsed blocks.
73+
private var _parsingSource: ParsingSource? = nil
74+
6075
/// Semaphore for thread-safe access to `_parsedBlocks`.
6176
private var _parsedBlocksSemaphore = DispatchSemaphore(value: 1)
6277

63-
// MARK: Initializers
64-
65-
/// Initializes a render state with an input string.
66-
///
67-
/// - Parameter latex: The view's input string.
68-
init(latex: String) {
69-
self.latex = latex
70-
}
71-
7278
}
7379

7480
// MARK: Public methods
@@ -78,14 +84,15 @@ extension Renderer {
7884
/// Returns whether the view's components are cached.
7985
///
8086
/// - Parameters:
87+
/// - latex: The LaTeX input string.
8188
/// - unencodeHTML: The `unencodeHTML` environment variable.
8289
/// - parsingMode: The `parsingMode` environment variable.
8390
/// - processEscapes: The `processEscapes` environment variable.
8491
/// - errorMode: The `errorMode` environment variable.
8592
/// - font: The `font environment` variable.
8693
/// - displayScale: The `displayScale` environment variable.
87-
/// - texOptions: The `texOptions` environment variable.
8894
func isCached(
95+
latex: String,
8996
unencodeHTML: Bool,
9097
parsingMode: LaTeX.ParsingMode,
9198
processEscapes: Bool,
@@ -95,7 +102,7 @@ extension Renderer {
95102
) -> Bool {
96103
let texOptions = TeXInputProcessorOptions(processEscapes: processEscapes, errorMode: errorMode)
97104
return blocksExistInCache(
98-
parsedBlocks(unencodeHTML: unencodeHTML, parsingMode: parsingMode),
105+
parsedBlocks(latex: latex, unencodeHTML: unencodeHTML, parsingMode: parsingMode),
99106
font: font,
100107
displayScale: displayScale,
101108
texOptions: texOptions)
@@ -104,14 +111,15 @@ extension Renderer {
104111
/// Renders the view's components synchronously.
105112
///
106113
/// - Parameters:
114+
/// - latex: The LaTeX input string.
107115
/// - unencodeHTML: The `unencodeHTML` environment variable.
108116
/// - parsingMode: The `parsingMode` environment variable.
109117
/// - processEscapes: The `processEscapes` environment variable.
110118
/// - errorMode: The `errorMode` environment variable.
111119
/// - font: The `font environment` variable.
112120
/// - displayScale: The `displayScale` environment variable.
113-
/// - texOptions: The `texOptions` environment variable.
114121
func renderSync(
122+
latex: String,
115123
unencodeHTML: Bool,
116124
parsingMode: LaTeX.ParsingMode,
117125
processEscapes: Bool,
@@ -121,7 +129,7 @@ extension Renderer {
121129
) -> [ComponentBlock] {
122130
let texOptions = TeXInputProcessorOptions(processEscapes: processEscapes, errorMode: errorMode)
123131
return render(
124-
blocks: parsedBlocks(unencodeHTML: unencodeHTML, parsingMode: parsingMode),
132+
blocks: parsedBlocks(latex: latex, unencodeHTML: unencodeHTML, parsingMode: parsingMode),
125133
font: font,
126134
displayScale: displayScale,
127135
texOptions: texOptions)
@@ -130,14 +138,15 @@ extension Renderer {
130138
/// Renders the view's components asynchronously.
131139
///
132140
/// - Parameters:
141+
/// - latex: The LaTeX input string.
133142
/// - unencodeHTML: The `unencodeHTML` environment variable.
134143
/// - parsingMode: The `parsingMode` environment variable.
135144
/// - processEscapes: The `processEscapes` environment variable.
136145
/// - errorMode: The `errorMode` environment variable.
137146
/// - font: The `font environment` variable.
138147
/// - displayScale: The `displayScale` environment variable.
139-
/// - texOptions: The `texOptions` environment variable.
140148
func render(
149+
latex: String,
141150
unencodeHTML: Bool,
142151
parsingMode: LaTeX.ParsingMode,
143152
processEscapes: Bool,
@@ -156,7 +165,7 @@ extension Renderer {
156165

157166
let texOptions = TeXInputProcessorOptions(processEscapes: processEscapes, errorMode: errorMode)
158167
let renderedBlocks = await render(
159-
blocks: parsedBlocks(unencodeHTML: unencodeHTML, parsingMode: parsingMode),
168+
blocks: parsedBlocks(latex: latex, unencodeHTML: unencodeHTML, parsingMode: parsingMode),
160169
font: font,
161170
displayScale: displayScale,
162171
texOptions: texOptions)
@@ -313,21 +322,26 @@ extension Renderer {
313322
/// Gets the LaTeX input's parsed blocks.
314323
///
315324
/// - Parameters:
325+
/// - latex: The LaTeX input string.
316326
/// - unencodeHTML: The `unencodeHTML` environment variable.
317327
/// - parsingMode: The `parsingMode` environment variable.
318328
/// - Returns: The parsed blocks.
319329
private func parsedBlocks(
330+
latex: String,
320331
unencodeHTML: Bool,
321332
parsingMode: LaTeX.ParsingMode
322333
) -> [ComponentBlock] {
323334
_parsedBlocksSemaphore.wait()
324335
defer { _parsedBlocksSemaphore.signal() }
325-
if let _parsedBlocks {
336+
337+
let currentSource = ParsingSource(latex: latex, unencodeHTML: unencodeHTML, parsingMode: parsingMode)
338+
if let _parsedBlocks, _parsingSource == currentSource {
326339
return _parsedBlocks
327340
}
328341

329342
let blocks = Parser.parse(unencodeHTML ? latex.htmlUnescape() : latex, mode: parsingMode)
330343
_parsedBlocks = blocks
344+
_parsingSource = currentSource
331345
return blocks
332346
}
333347

Sources/LaTeXSwiftUI/Views/ComponentBlockText.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ struct ComponentBlockTextPreviews: PreviewProvider {
7272
static var previews: some View {
7373
ComponentBlockText(block: ComponentBlock(components: [
7474
Component(text: "Hello, World!", type: .text)
75-
]), renderer: Renderer(latex: "Hello, World!"))
75+
]), renderer: Renderer())
7676
}
7777
}

Sources/LaTeXSwiftUI/Views/ComponentBlocksText.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ struct ComponentBlocksTextPreviews: PreviewProvider {
5757
ComponentBlocksText(blocks: [ComponentBlock(components: [
5858
Component(text: "Hello, World!", type: .text)
5959
])], forceInline: false)
60-
.environmentObject(Renderer(latex: "Hello, World!"))
60+
.environmentObject(Renderer())
6161
}
6262
}

Sources/LaTeXSwiftUI/Views/ComponentBlocksViews.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ struct ComponentBlocksViewsPreviews: PreviewProvider {
9797
ComponentBlocksViews(blocks: [ComponentBlock(components: [
9898
Component(text: "Hello, World!", type: .text)
9999
])])
100-
.environmentObject(Renderer(latex: "Hello, World!"))
100+
.environmentObject(Renderer())
101101
}
102102
}

0 commit comments

Comments
 (0)