Skip to content

Commit 946a8be

Browse files
Prachi Gauriarprachigauriar
authored andcommitted
Add dateProvider to ExpiringValue lifetime duration initializers
1 parent 17cc739 commit 946a8be

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# DevFoundation Changelog
22

33

4+
## 1.7.0: October 27, 2025
5+
6+
This is a small release that updates `ExpiringValue` to work better with `DateProvider`.
7+
Specifically, the two initializers spelled `ExpiringValue.init(_:lifetimeDuration:)` have been
8+
updated to include an `any DateProvider` parameter that defaults to `DateProviders.current`. The new
9+
initializers are spelled `ExpiringValue.init(_:dateProvider:lifetimeDuration:)`.
10+
11+
412
## 1.6.0: October 24, 2025
513

614
This release introduces the `LiveQuery` subsystem, a set of types for managing search-as-you-type

Sources/DevFoundation/Utility Types/ExpiringValue.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ public struct ExpiringValue<Value> {
3838
///
3939
/// - Parameters:
4040
/// - value: The value.
41+
/// - dateProvider: The date provider to use to determine the current date.
4142
/// - lifetimeDuration: The length of the value’s lifetime.
42-
public init(_ value: Value, lifetimeDuration: Duration) {
43-
let now = DateProviders.current.now
43+
public init(
44+
_ value: Value,
45+
dateProvider: any DateProvider = DateProviders.current,
46+
lifetimeDuration: Duration
47+
) {
48+
let now = dateProvider.now
4449
self.value = value
4550
self.lifetimeRange = now ... (now + lifetimeDuration.timeInterval)
4651
}
@@ -52,9 +57,14 @@ public struct ExpiringValue<Value> {
5257
///
5358
/// - Parameters:
5459
/// - value: The value.
60+
/// - dateProvider: The date provider to use to determine the current date.
5561
/// - lifetimeDuration: The length of the value’s lifetime.
56-
public init(_ value: Value, lifetimeDuration: TimeInterval) {
57-
self.init(value, lifetimeDuration: .seconds(lifetimeDuration))
62+
public init(
63+
_ value: Value,
64+
dateProvider: any DateProvider = DateProviders.current,
65+
lifetimeDuration: TimeInterval
66+
) {
67+
self.init(value, dateProvider: dateProvider, lifetimeDuration: .seconds(lifetimeDuration))
5868
}
5969

6070

Tests/DevFoundationTests/Utility Types/ExpiringValueTests.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,31 @@ struct ExpiringValueTests: RandomValueGenerating {
2828

2929
@Test
3030
mutating func durationInitSetsValuesCorrectly() {
31+
let value = randomUUID()
32+
let duration = Duration.milliseconds(randomInt(in: 0 ... 10_000))
33+
34+
let now = randomDate()
35+
let dateProvider = MockDateProvider(now: now)
36+
let expiringValue = ExpiringValue(value, dateProvider: dateProvider, lifetimeDuration: duration)
37+
38+
#expect(expiringValue.value == value)
39+
#expect(expiringValue.lifetimeRange.lowerBound == now)
40+
#expect(expiringValue.lifetimeRange.upperBound == now + duration.timeInterval)
41+
}
42+
43+
44+
@Test
45+
mutating func timeIntervalDurationInitSetsValuesCorrectly() {
3146
let value = randomUUID()
3247
let duration = random(TimeInterval.self, in: 1 ... 10_000)
3348

34-
let date = Date()
35-
let expiringValue = ExpiringValue(value, lifetimeDuration: duration)
49+
let now = randomDate()
50+
let dateProvider = MockDateProvider(now: now)
51+
let expiringValue = ExpiringValue(value, dateProvider: dateProvider, lifetimeDuration: duration)
3652

3753
#expect(expiringValue.value == value)
38-
#expect(expiringValue.lifetimeRange.lowerBound.isApproximatelyEqual(to: date, absoluteTolerance: 0.01))
39-
#expect(
40-
expiringValue.lifetimeRange.upperBound.isApproximatelyEqual(to: date + duration, absoluteTolerance: 0.01)
41-
)
54+
#expect(expiringValue.lifetimeRange.lowerBound == now)
55+
#expect(expiringValue.lifetimeRange.upperBound == now + duration)
4256
}
4357

4458

0 commit comments

Comments
 (0)