@@ -48,3 +48,42 @@ public typealias ReloadingYAMLProvider = ReloadingFileProvider<YAMLSnapshot>
4848@available ( Configuration 1 . 0 , * )
4949@available ( * , deprecated, renamed: " ConfigSnapshot " )
5050public typealias ConfigSnapshotProtocol = ConfigSnapshot
51+
52+ @available ( Configuration 1 . 0 , * )
53+ extension ConfigReader {
54+ /// Provides a snapshot of the current configuration state and passes it to the provided closure.
55+ ///
56+ /// This method creates a snapshot of the current configuration state and passes it to the
57+ /// provided closure. The snapshot reader provides read-only access to the configuration's state
58+ /// at the time the method was called.
59+ ///
60+ /// ```swift
61+ /// let result = config.withSnapshot { snapshot in
62+ /// // Use snapshot to read config values
63+ /// let cert = snapshot.string(forKey: "cert")
64+ /// let privateKey = snapshot.string(forKey: "privateKey")
65+ /// // Ensures that both values are coming from the same underlying snapshot and that a provider
66+ /// // didn't change its internal state between the two `string(...)` calls.
67+ /// return MyCert(cert: cert, privateKey: privateKey)
68+ /// }
69+ /// ```
70+ ///
71+ /// - Parameter body: A closure that takes a `ConfigSnapshotReader` and returns a value.
72+ /// - Returns: The value returned by the closure.
73+ /// - Throws: Rethrows any errors thrown by the provided closure.
74+ @available ( * , deprecated, message: " Renamed to snapshot(). " )
75+ public func withSnapshot< Failure: Error , Return> (
76+ _ body: ( ConfigSnapshotReader ) throws ( Failure ) -> Return
77+ ) throws ( Failure) -> Return {
78+ let multiSnapshot = provider. snapshot ( )
79+ let snapshotReader = ConfigSnapshotReader (
80+ keyPrefix: keyPrefix,
81+ storage: . init(
82+ keyDecoder: keyDecoder,
83+ snapshot: multiSnapshot,
84+ accessReporter: accessReporter
85+ )
86+ )
87+ return try body ( snapshotReader)
88+ }
89+ }
0 commit comments