@@ -39,67 +39,45 @@ public func withDependencies<R>(
39
39
}
40
40
}
41
41
42
- #if swift(>=5.7)
43
- /// Updates the current dependencies for the duration of an asynchronous operation.
44
- ///
45
- /// Any mutations made to ``DependencyValues`` inside `updateValuesForOperation` will be visible
46
- /// to everything executed in the operation. For example, if you wanted to force the
47
- /// ``DependencyValues/date`` dependency to be a particular date, you can do:
48
- ///
49
- /// ```swift
50
- /// await withDependencies {
51
- /// $0.date.now = Date(timeIntervalSince1970: 1234567890)
52
- /// } operation: {
53
- /// // References to date in here are pinned to 1234567890.
54
- /// }
55
- /// ```
56
- ///
57
- /// - Parameters:
58
- /// - updateValuesForOperation: A closure for updating the current dependency values for the
59
- /// duration of the operation.
60
- /// - operation: An operation to perform wherein dependencies have been overridden.
61
- /// - Returns: The result returned from `operation`.
62
- @_unsafeInheritExecutor
63
- @discardableResult
64
- public func withDependencies< R> (
65
- _ updateValuesForOperation: ( inout DependencyValues ) async throws -> Void ,
66
- operation: ( ) async throws -> R
67
- ) async rethrows -> R {
68
- try await isSetting ( true ) {
69
- var dependencies = DependencyValues . _current
70
- try await updateValuesForOperation ( & dependencies)
71
- return try await DependencyValues . $_current. withValue ( dependencies) {
72
- try await isSetting ( false ) {
73
- let result = try await operation ( )
74
- if R . self is AnyClass {
75
- dependencyObjects. store ( result as AnyObject )
76
- }
77
- return result
78
- }
79
- }
80
- }
81
- }
82
- #else
83
- @discardableResult
84
- public func withDependencies< R> (
85
- _ updateValuesForOperation: ( inout DependencyValues ) async throws -> Void ,
86
- operation: ( ) async throws -> R
87
- ) async rethrows -> R {
88
- try await isSetting ( true ) {
89
- var dependencies = DependencyValues . _current
90
- try await updateValuesForOperation ( & dependencies)
91
- return try await DependencyValues . $_current. withValue ( dependencies) {
92
- try await isSetting ( false ) {
93
- let result = try await operation ( )
94
- if R . self is AnyClass {
95
- dependencyObjects. store ( result as AnyObject )
96
- }
97
- return result
42
+ /// Updates the current dependencies for the duration of an asynchronous operation.
43
+ ///
44
+ /// Any mutations made to ``DependencyValues`` inside `updateValuesForOperation` will be visible
45
+ /// to everything executed in the operation. For example, if you wanted to force the
46
+ /// ``DependencyValues/date`` dependency to be a particular date, you can do:
47
+ ///
48
+ /// ```swift
49
+ /// await withDependencies {
50
+ /// $0.date.now = Date(timeIntervalSince1970: 1234567890)
51
+ /// } operation: {
52
+ /// // References to date in here are pinned to 1234567890.
53
+ /// }
54
+ /// ```
55
+ ///
56
+ /// - Parameters:
57
+ /// - updateValuesForOperation: A closure for updating the current dependency values for the
58
+ /// duration of the operation.
59
+ /// - operation: An operation to perform wherein dependencies have been overridden.
60
+ /// - Returns: The result returned from `operation`.
61
+ @_unsafeInheritExecutor
62
+ @discardableResult
63
+ public func withDependencies< R> (
64
+ _ updateValuesForOperation: ( inout DependencyValues ) async throws -> Void ,
65
+ operation: ( ) async throws -> R
66
+ ) async rethrows -> R {
67
+ try await isSetting ( true ) {
68
+ var dependencies = DependencyValues . _current
69
+ try await updateValuesForOperation ( & dependencies)
70
+ return try await DependencyValues . $_current. withValue ( dependencies) {
71
+ try await isSetting ( false ) {
72
+ let result = try await operation ( )
73
+ if R . self is AnyClass {
74
+ dependencyObjects. store ( result as AnyObject )
98
75
}
76
+ return result
99
77
}
100
78
}
101
79
}
102
- #endif
80
+ }
103
81
104
82
/// Updates the current dependencies for the duration of a synchronous operation by taking the
105
83
/// dependencies tied to a given object.
@@ -168,129 +146,76 @@ public func withDependencies<Model: AnyObject, R>(
168
146
)
169
147
}
170
148
171
- #if swift(>=5.7)
172
- /// Updates the current dependencies for the duration of an asynchronous operation by taking the
173
- /// dependencies tied to a given object.
174
- ///
175
- /// - Parameters:
176
- /// - model: An object with dependencies. The given model should have at least one `@Dependency`
177
- /// property, or should have been initialized and returned from a `withDependencies`
178
- /// operation.
179
- /// - updateValuesForOperation: A closure for updating the current dependency values for the
180
- /// duration of the operation.
181
- /// - operation: The operation to run with the updated dependencies.
182
- /// - Returns: The result returned from `operation`.
183
- @_unsafeInheritExecutor
184
- @discardableResult
185
- public func withDependencies< Model: AnyObject , R> (
186
- from model: Model ,
187
- _ updateValuesForOperation: ( inout DependencyValues ) async throws -> Void ,
188
- operation: ( ) async throws -> R ,
189
- file: StaticString ? = nil ,
190
- line: UInt ? = nil
191
- ) async rethrows -> R {
192
- guard let values = dependencyObjects. values ( from: model)
193
- else {
194
- runtimeWarn (
195
- """
196
- You are trying to propagate dependencies to a child model from a model with no \
197
- dependencies. To fix this, the given ' \( Model . self) ' must be returned from another \
198
- 'withDependencies' closure, or the class must hold at least one '@Dependency' property.
199
- """ ,
200
- file: file,
201
- line: line
202
- )
203
- return try await operation ( )
204
- }
205
- return try await withDependencies {
206
- $0 = values. merging ( DependencyValues . _current)
207
- try await updateValuesForOperation ( & $0)
208
- } operation: {
209
- let result = try await operation ( )
210
- if R . self is AnyClass {
211
- dependencyObjects. store ( result as AnyObject )
212
- }
213
- return result
214
- }
215
- }
216
- #else
217
- @discardableResult
218
- public func withDependencies< Model: AnyObject , R> (
219
- from model: Model ,
220
- _ updateValuesForOperation: ( inout DependencyValues ) async throws -> Void ,
221
- operation: ( ) async throws -> R ,
222
- file: StaticString ? = nil ,
223
- line: UInt ? = nil
224
- ) async rethrows -> R {
225
- guard let values = dependencyObjects. values ( from: model)
226
- else {
227
- runtimeWarn (
228
- """
229
- You are trying to propagate dependencies to a child model from a model with no \
230
- dependencies. To fix this, the given ' \( Model . self) ' must be returned from another \
231
- 'withDependencies' closure, or the class must hold at least one '@Dependency' property.
232
- """ ,
233
- file: file,
234
- line: line
235
- )
236
- return try await operation ( )
237
- }
238
- return try await withDependencies {
239
- $0 = values. merging ( DependencyValues . _current)
240
- try await updateValuesForOperation ( & $0)
241
- } operation: {
242
- let result = try await operation ( )
243
- if R . self is AnyClass {
244
- dependencyObjects. store ( result as AnyObject )
245
- }
246
- return result
247
- }
248
- }
249
- #endif
250
-
251
- #if swift(>=5.7)
252
- /// Updates the current dependencies for the duration of an asynchronous operation by taking the
253
- /// dependencies tied to a given object.
254
- ///
255
- /// - Parameters:
256
- /// - model: An object with dependencies. The given model should have at least one `@Dependency`
257
- /// property, or should have been initialized and returned from a `withDependencies`
258
- /// operation.
259
- /// - operation: The operation to run with the updated dependencies.
260
- /// - Returns: The result returned from `operation`.
261
- @_unsafeInheritExecutor
262
- @discardableResult
263
- public func withDependencies< Model: AnyObject , R> (
264
- from model: Model ,
265
- operation: ( ) async throws -> R ,
266
- file: StaticString ? = nil ,
267
- line: UInt ? = nil
268
- ) async rethrows -> R {
269
- try await withDependencies (
270
- from: model,
271
- { _ in } ,
272
- operation: operation,
149
+ /// Updates the current dependencies for the duration of an asynchronous operation by taking the
150
+ /// dependencies tied to a given object.
151
+ ///
152
+ /// - Parameters:
153
+ /// - model: An object with dependencies. The given model should have at least one `@Dependency`
154
+ /// property, or should have been initialized and returned from a `withDependencies`
155
+ /// operation.
156
+ /// - updateValuesForOperation: A closure for updating the current dependency values for the
157
+ /// duration of the operation.
158
+ /// - operation: The operation to run with the updated dependencies.
159
+ /// - Returns: The result returned from `operation`.
160
+ @_unsafeInheritExecutor
161
+ @discardableResult
162
+ public func withDependencies< Model: AnyObject , R> (
163
+ from model: Model ,
164
+ _ updateValuesForOperation: ( inout DependencyValues ) async throws -> Void ,
165
+ operation: ( ) async throws -> R ,
166
+ file: StaticString ? = nil ,
167
+ line: UInt ? = nil
168
+ ) async rethrows -> R {
169
+ guard let values = dependencyObjects. values ( from: model)
170
+ else {
171
+ runtimeWarn (
172
+ """
173
+ You are trying to propagate dependencies to a child model from a model with no \
174
+ dependencies. To fix this, the given ' \( Model . self) ' must be returned from another \
175
+ 'withDependencies' closure, or the class must hold at least one '@Dependency' property.
176
+ """ ,
273
177
file: file,
274
178
line: line
275
179
)
180
+ return try await operation ( )
276
181
}
277
- #else
278
- @discardableResult
279
- public func withDependencies< Model: AnyObject , R> (
280
- from model: Model ,
281
- operation: ( ) async throws -> R ,
282
- file: StaticString ? = nil ,
283
- line: UInt ? = nil
284
- ) async rethrows -> R {
285
- try await withDependencies (
286
- from: model,
287
- { _ in } ,
288
- operation: operation,
289
- file: file,
290
- line: line
291
- )
182
+ return try await withDependencies {
183
+ $0 = values. merging ( DependencyValues . _current)
184
+ try await updateValuesForOperation ( & $0)
185
+ } operation: {
186
+ let result = try await operation ( )
187
+ if R . self is AnyClass {
188
+ dependencyObjects. store ( result as AnyObject )
189
+ }
190
+ return result
292
191
}
293
- #endif
192
+ }
193
+
194
+ /// Updates the current dependencies for the duration of an asynchronous operation by taking the
195
+ /// dependencies tied to a given object.
196
+ ///
197
+ /// - Parameters:
198
+ /// - model: An object with dependencies. The given model should have at least one `@Dependency`
199
+ /// property, or should have been initialized and returned from a `withDependencies`
200
+ /// operation.
201
+ /// - operation: The operation to run with the updated dependencies.
202
+ /// - Returns: The result returned from `operation`.
203
+ @_unsafeInheritExecutor
204
+ @discardableResult
205
+ public func withDependencies< Model: AnyObject , R> (
206
+ from model: Model ,
207
+ operation: ( ) async throws -> R ,
208
+ file: StaticString ? = nil ,
209
+ line: UInt ? = nil
210
+ ) async rethrows -> R {
211
+ try await withDependencies (
212
+ from: model,
213
+ { _ in } ,
214
+ operation: operation,
215
+ file: file,
216
+ line: line
217
+ )
218
+ }
294
219
295
220
/// Propagates the current dependencies to an escaping context.
296
221
///
0 commit comments