@@ -309,17 +309,35 @@ public func withDependencies<Model: AnyObject, R>(
309
309
/// withEscapedDependencies { dependencies in
310
310
/// DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
311
311
/// dependencies.yield {
312
- /// // All code in here will use dependencies at the time of
313
- /// // calling DependencyValues.escape.
312
+ /// // All code in here will use dependencies at the time of calling withEscapedDependencies.
314
313
/// }
315
314
/// }
316
315
/// }
317
316
/// ```
318
317
///
319
318
/// As a general rule, you should surround _all_ escaping code that may access dependencies with
320
- /// this helper. Otherwise you run the risk of the escaped code using the wrong dependencies. But,
321
- /// you should also try your hardest to keep your code in the structured world using Swift's tools
322
- /// of structured concurrency, and should avoid using escaping closures.
319
+ /// this helper, and you should use ``DependencyValues/Continuation/yield(_:)-42ttb`` _immediately_
320
+ /// inside the escaping closure. Otherwise you run the risk of the escaped code using the wrong
321
+ /// dependencies. But, you should also try your hardest to keep your code in the structured world
322
+ /// using Swift's tools of structured concurrency, and should avoid using escaping closures.
323
+ ///
324
+ /// If you need to further override dependencies in the escaped closure, do so inside the
325
+ /// ``DependencyValues/Continuation/yield(_:)-42ttb`` and not outside:
326
+ ///
327
+ /// ```swift
328
+ /// withEscapedDependencies { dependencies in
329
+ /// DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
330
+ /// dependencies.yield {
331
+ /// withDependencies {
332
+ /// $0.apiClient = .mock
333
+ /// } operation: {
334
+ /// // All code in here will use dependencies at the time of calling
335
+ /// // withEscapedDependencies except the API client will be mocked.
336
+ /// }
337
+ /// }
338
+ /// }
339
+ /// }
340
+ /// ```
323
341
///
324
342
/// - Parameter operation: A closure that takes a ``DependencyValues/Continuation`` value for
325
343
/// propagating dependencies past an escaping closure boundary.
@@ -346,13 +364,14 @@ extension DependencyValues {
346
364
///
347
365
/// See the docs of ``withEscapedDependencies(_:)-5xvi3`` for more information.
348
366
public struct Continuation : Sendable {
349
- @ Dependency ( \ . self ) private var dependencies
367
+ let dependencies = DependencyValues . _current
350
368
351
369
/// Access the propagated dependencies in an escaping context.
352
370
///
353
371
/// See the docs of ``withEscapedDependencies(_:)-5xvi3`` for more information.
354
372
/// - Parameter operation: A closure which will have access to the propagated dependencies.
355
373
public func yield< R> ( _ operation: ( ) throws -> R ) rethrows -> R {
374
+ // TODO: Should `yield` be renamed to `restore`?
356
375
try withDependencies {
357
376
$0 = self . dependencies
358
377
} operation: {
0 commit comments