Files
2024-05-17 10:44:28 -07:00

63 lines
1.5 KiB
Swift

import Foundation
extension DependencyValues {
@_spi(Internals) public var stackElementID: StackElementIDGenerator {
get { self[StackElementIDGenerator.self] }
set { self[StackElementIDGenerator.self] = newValue }
}
}
@_spi(Internals) public struct StackElementIDGenerator: DependencyKey, Sendable {
public let next: @Sendable () -> StackElementID
public let peek: @Sendable () -> StackElementID
@_spi(Internals)
public func callAsFunction() -> StackElementID {
self.next()
}
public static var liveValue: Self {
let next = LockIsolated(StackElementID(generation: 0))
return Self(
next: {
defer {
next.withValue { $0 = StackElementID(generation: $0.generation + 1) }
}
return next.value
},
peek: { next.value }
)
}
public static var testValue: Self {
let next = LockIsolated(StackElementID(generation: 0))
return Self(
next: {
defer {
next.withValue {
$0 = StackElementID(generation: $0.generation + 1)
}
}
return next.value
},
peek: { next.value }
)
}
func incrementingCopy() -> Self {
let peek = self.peek()
let next = LockIsolated(StackElementID(generation: peek.generation))
return Self(
next: {
defer {
next.withValue {
$0 = StackElementID(generation: $0.generation + 1)
}
}
return next.value
},
peek: { next.value }
)
}
}