Skip to content

Commit fe5621b

Browse files
committed
feat: make BlockStyle conform to Sendable
This is required for `Theme` to be `Sendable`. Done as its own change so that
1 parent a9c7615 commit fe5621b

File tree

4 files changed

+41
-40
lines changed

4 files changed

+41
-40
lines changed

Sources/MarkdownUI/Theme/BlockStyle/BlockStyle.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ import SwiftUI
3737
/// ```
3838
///
3939
/// ![](CustomBlockquote)
40-
public struct BlockStyle<Configuration> {
41-
private let body: (Configuration) -> AnyView
40+
public struct BlockStyle<Configuration> : Sendable {
41+
private let body: @Sendable @MainActor (Configuration) -> AnyView
4242

4343
/// Creates a block style that customizes a block by applying the given body.
4444
/// - Parameter body: A view builder that returns the customized block.
45-
public init<Body: View>(@ViewBuilder body: @escaping (_ configuration: Configuration) -> Body) {
45+
public init<Body: View>(@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: Configuration) -> Body) {
4646
self.body = { AnyView(body($0)) }
4747
}
4848

49+
@MainActor
4950
func makeBody(configuration: Configuration) -> AnyView {
5051
self.body(configuration)
5152
}
@@ -54,7 +55,7 @@ public struct BlockStyle<Configuration> {
5455
extension BlockStyle where Configuration == Void {
5556
/// Creates a block style for a block with no content, like a thematic break.
5657
/// - Parameter body: A view builder that returns the customized block.
57-
public init<Body: View>(@ViewBuilder body: @escaping () -> Body) {
58+
public init<Body: View>(@ViewBuilder body: @escaping @Sendable @MainActor () -> Body) {
5859
self.init { _ in
5960
body()
6061
}

Sources/MarkdownUI/Theme/Theme.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ extension Theme {
253253
/// Adds a level 1 heading style to the theme.
254254
/// - Parameter body: A view builder that returns a customized level 1 heading.
255255
public func heading1<Body: View>(
256-
@ViewBuilder body: @escaping (_ configuration: BlockConfiguration) -> Body
256+
@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: BlockConfiguration) -> Body
257257
) -> Theme {
258258
var theme = self
259259
theme.heading1 = .init(body: body)
@@ -263,7 +263,7 @@ extension Theme {
263263
/// Adds a level 2 heading style to the theme.
264264
/// - Parameter body: A view builder that returns a customized level 2 heading.
265265
public func heading2<Body: View>(
266-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
266+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
267267
) -> Theme {
268268
var theme = self
269269
theme.heading2 = .init(body: body)
@@ -273,7 +273,7 @@ extension Theme {
273273
/// Adds a level 3 heading style to the theme.
274274
/// - Parameter body: A view builder that returns a customized level 3 heading.
275275
public func heading3<Body: View>(
276-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
276+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
277277
) -> Theme {
278278
var theme = self
279279
theme.heading3 = .init(body: body)
@@ -283,7 +283,7 @@ extension Theme {
283283
/// Adds a level 4 heading style to the theme.
284284
/// - Parameter body: A view builder that returns a customized level 4 heading.
285285
public func heading4<Body: View>(
286-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
286+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
287287
) -> Theme {
288288
var theme = self
289289
theme.heading4 = .init(body: body)
@@ -293,7 +293,7 @@ extension Theme {
293293
/// Adds a level 5 heading style to the theme.
294294
/// - Parameter body: A view builder that returns a customized level 5 heading.
295295
public func heading5<Body: View>(
296-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
296+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
297297
) -> Theme {
298298
var theme = self
299299
theme.heading5 = .init(body: body)
@@ -303,7 +303,7 @@ extension Theme {
303303
/// Adds a level 6 heading style to the theme.
304304
/// - Parameter body: A view builder that returns a customized level 6 heading.
305305
public func heading6<Body: View>(
306-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
306+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
307307
) -> Theme {
308308
var theme = self
309309
theme.heading6 = .init(body: body)
@@ -313,7 +313,7 @@ extension Theme {
313313
/// Adds a paragraph style to the theme.
314314
/// - Parameter body: A view builder that returns a customized paragraph.
315315
public func paragraph<Body: View>(
316-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
316+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
317317
) -> Theme {
318318
var theme = self
319319
theme.paragraph = .init(body: body)
@@ -323,7 +323,7 @@ extension Theme {
323323
/// Adds a blockquote style to the theme.
324324
/// - Parameter body: A view builder that returns a customized blockquote.
325325
public func blockquote<Body: View>(
326-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
326+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
327327
) -> Theme {
328328
var theme = self
329329
theme.blockquote = .init(body: body)
@@ -333,7 +333,7 @@ extension Theme {
333333
/// Adds a code block style to the theme.
334334
/// - Parameter body: A view builder that returns a customized code block.
335335
public func codeBlock<Body: View>(
336-
@ViewBuilder body: @escaping (_ configuration: CodeBlockConfiguration) -> Body
336+
@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: CodeBlockConfiguration) -> Body
337337
) -> Theme {
338338
var theme = self
339339
theme.codeBlock = .init(body: body)
@@ -343,7 +343,7 @@ extension Theme {
343343
/// Adds an image style to the theme.
344344
/// - Parameter body: A view builder that returns a customized image.
345345
public func image<Body: View>(
346-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
346+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
347347
) -> Theme {
348348
var theme = self
349349
theme.image = .init(body: body)
@@ -353,7 +353,7 @@ extension Theme {
353353
/// Adds a list style to the theme.
354354
/// - Parameter body: A view builder that returns a customized list.
355355
public func list<Body: View>(
356-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
356+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
357357
) -> Theme {
358358
var theme = self
359359
theme.list = .init(body: body)
@@ -363,7 +363,7 @@ extension Theme {
363363
/// Adds a list item style to the theme.
364364
/// - Parameter body: A view builder that returns a customized list item.
365365
public func listItem<Body: View>(
366-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
366+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
367367
) -> Theme {
368368
var theme = self
369369
theme.listItem = .init(body: body)
@@ -381,7 +381,7 @@ extension Theme {
381381
/// Adds a task list marker style to the theme.
382382
/// - Parameter body: A view builder that returns a customized task list marker.
383383
public func taskListMarker<Body: View>(
384-
@ViewBuilder body: @escaping (_ configuration: TaskListMarkerConfiguration) -> Body
384+
@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: TaskListMarkerConfiguration) -> Body
385385
) -> Theme {
386386
var theme = self
387387
theme.taskListMarker = .init(body: body)
@@ -401,7 +401,7 @@ extension Theme {
401401
/// Adds a bulleted list marker style to the theme.
402402
/// - Parameter body: A view builder that returns a customized bulleted list marker.
403403
public func bulletedListMarker<Body: View>(
404-
@ViewBuilder body: @escaping (_ configuration: ListMarkerConfiguration) -> Body
404+
@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: ListMarkerConfiguration) -> Body
405405
) -> Theme {
406406
var theme = self
407407
theme.bulletedListMarker = .init(body: body)
@@ -421,7 +421,7 @@ extension Theme {
421421
/// Adds a numbered list marker style to the theme.
422422
/// - Parameter body: A view builder that returns a customized numbered list marker.
423423
public func numberedListMarker<Body: View>(
424-
@ViewBuilder body: @escaping (_ configuration: ListMarkerConfiguration) -> Body
424+
@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: ListMarkerConfiguration) -> Body
425425
) -> Theme {
426426
var theme = self
427427
theme.numberedListMarker = .init(body: body)
@@ -431,7 +431,7 @@ extension Theme {
431431
/// Adds a table style to the theme.
432432
/// - Parameter body: A view builder that returns a customized table.
433433
public func table<Body: View>(
434-
@ViewBuilder body: @escaping (_ label: BlockConfiguration) -> Body
434+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration) -> Body
435435
) -> Theme {
436436
var theme = self
437437
theme.table = .init(body: body)
@@ -441,7 +441,7 @@ extension Theme {
441441
/// Adds a table cell style to the theme.
442442
/// - Parameter body: A view builder that returns a customized table cell.
443443
public func tableCell<Body: View>(
444-
@ViewBuilder body: @escaping (_ configuration: TableCellConfiguration) -> Body
444+
@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: TableCellConfiguration) -> Body
445445
) -> Theme {
446446
var theme = self
447447
theme.tableCell = .init(body: body)
@@ -450,7 +450,7 @@ extension Theme {
450450

451451
/// Adds a thematic break style to the theme.
452452
/// - Parameter body: A view builder that returns a customized thematic break.
453-
public func thematicBreak<Body: View>(@ViewBuilder body: @escaping () -> Body) -> Theme {
453+
public func thematicBreak<Body: View>(@ViewBuilder body: @escaping @Sendable @MainActor () -> Body) -> Theme {
454454
var theme = self
455455
theme.thematicBreak = .init(body: body)
456456
return theme

Sources/MarkdownUI/Utility/Deprecations.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension BlockStyle where Configuration == BlockConfiguration {
2525
message: "Use the initializer that takes a closure receiving a 'Configuration' value."
2626
)
2727
public init<Body: View>(
28-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
28+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
2929
) {
3030
self.init { configuration in
3131
body(configuration.label)
@@ -53,7 +53,7 @@ extension View {
5353
)
5454
public func markdownBlockStyle<Body: View>(
5555
_ keyPath: WritableKeyPath<Theme, BlockStyle<BlockConfiguration>>,
56-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
56+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
5757
) -> some View {
5858
self.environment((\EnvironmentValues.theme).appending(path: keyPath), .init(body: body))
5959
}
@@ -68,7 +68,7 @@ extension View {
6868
)
6969
public func markdownBlockStyle<Body: View>(
7070
_ keyPath: WritableKeyPath<Theme, BlockStyle<CodeBlockConfiguration>>,
71-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
71+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
7272
) -> some View {
7373
self.environment(
7474
(\EnvironmentValues.theme).appending(path: keyPath),
@@ -89,7 +89,7 @@ extension Theme {
8989
"""
9090
)
9191
public func heading1<Body: View>(
92-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
92+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
9393
) -> Theme {
9494
var theme = self
9595
theme.heading1 = .init(body: body)
@@ -105,7 +105,7 @@ extension Theme {
105105
"""
106106
)
107107
public func heading2<Body: View>(
108-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
108+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
109109
) -> Theme {
110110
var theme = self
111111
theme.heading2 = .init(body: body)
@@ -121,7 +121,7 @@ extension Theme {
121121
"""
122122
)
123123
public func heading3<Body: View>(
124-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
124+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
125125
) -> Theme {
126126
var theme = self
127127
theme.heading3 = .init(body: body)
@@ -137,7 +137,7 @@ extension Theme {
137137
"""
138138
)
139139
public func heading4<Body: View>(
140-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
140+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
141141
) -> Theme {
142142
var theme = self
143143
theme.heading4 = .init(body: body)
@@ -153,7 +153,7 @@ extension Theme {
153153
"""
154154
)
155155
public func heading5<Body: View>(
156-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
156+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
157157
) -> Theme {
158158
var theme = self
159159
theme.heading5 = .init(body: body)
@@ -169,7 +169,7 @@ extension Theme {
169169
"""
170170
)
171171
public func heading6<Body: View>(
172-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
172+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
173173
) -> Theme {
174174
var theme = self
175175
theme.heading6 = .init(body: body)
@@ -185,7 +185,7 @@ extension Theme {
185185
"""
186186
)
187187
public func paragraph<Body: View>(
188-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
188+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
189189
) -> Theme {
190190
var theme = self
191191
theme.paragraph = .init(body: body)
@@ -201,7 +201,7 @@ extension Theme {
201201
"""
202202
)
203203
public func blockquote<Body: View>(
204-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
204+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
205205
) -> Theme {
206206
var theme = self
207207
theme.blockquote = .init(body: body)
@@ -217,7 +217,7 @@ extension Theme {
217217
"""
218218
)
219219
public func codeBlock<Body: View>(
220-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
220+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
221221
) -> Theme {
222222
var theme = self
223223
theme.codeBlock = .init { configuration in
@@ -235,7 +235,7 @@ extension Theme {
235235
"""
236236
)
237237
public func image<Body: View>(
238-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
238+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
239239
) -> Theme {
240240
var theme = self
241241
theme.image = .init(body: body)
@@ -251,7 +251,7 @@ extension Theme {
251251
"""
252252
)
253253
public func list<Body: View>(
254-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
254+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
255255
) -> Theme {
256256
var theme = self
257257
theme.list = .init(body: body)
@@ -267,7 +267,7 @@ extension Theme {
267267
"""
268268
)
269269
public func listItem<Body: View>(
270-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
270+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
271271
) -> Theme {
272272
var theme = self
273273
theme.listItem = .init(body: body)
@@ -283,7 +283,7 @@ extension Theme {
283283
"""
284284
)
285285
public func table<Body: View>(
286-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
286+
@ViewBuilder body: @escaping @Sendable @MainActor (_ label: BlockConfiguration.Label) -> Body
287287
) -> Theme {
288288
var theme = self
289289
theme.table = .init(body: body)

Sources/MarkdownUI/Views/Environment/Environment+Theme.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension View {
2424
/// - body: A view builder that returns the customized block.
2525
public func markdownBlockStyle<Body: View>(
2626
_ keyPath: WritableKeyPath<Theme, BlockStyle<Void>>,
27-
@ViewBuilder body: @escaping () -> Body
27+
@ViewBuilder body: @escaping @Sendable @MainActor () -> Body
2828
) -> some View {
2929
self.environment((\EnvironmentValues.theme).appending(path: keyPath), .init(body: body))
3030
}
@@ -35,7 +35,7 @@ extension View {
3535
/// - body: A view builder that receives the block configuration and returns the customized block.
3636
public func markdownBlockStyle<Configuration, Body: View>(
3737
_ keyPath: WritableKeyPath<Theme, BlockStyle<Configuration>>,
38-
@ViewBuilder body: @escaping (_ configuration: Configuration) -> Body
38+
@ViewBuilder body: @escaping @Sendable @MainActor (_ configuration: Configuration) -> Body
3939
) -> some View {
4040
self.environment((\EnvironmentValues.theme).appending(path: keyPath), .init(body: body))
4141
}

0 commit comments

Comments
 (0)