Skip to content

Commit

Permalink
Updating CachedValue to actually cache values
Browse files Browse the repository at this point in the history
  • Loading branch information
mergesort committed Jan 2, 2025
1 parent af13ed2 commit cdce10e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions Sources/Boutique/CachedValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ internal final class CachedValue<Item: Codable> {

init(retrieveValue: @escaping () -> Item) {
self.retrieveValue = retrieveValue
self.cachedValue = self.retrieveValue()
}

func set(_ value: Item) {
self.cachedValue = value
}

var wrappedValue: Item? {
var wrappedValue: Item {
if let cachedValue {
cachedValue
return cachedValue
} else {
self.retrieveValue()
let retrievedValue = self.retrieveValue()
self.cachedValue = retrievedValue
return retrievedValue
}
}
}
2 changes: 1 addition & 1 deletion Sources/Boutique/StoredValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public struct StoredValue<Item: Codable> {

/// The currently stored value
public var wrappedValue: Item {
self.cachedValue.retrieveValue()
self.cachedValue.wrappedValue
}

/// A ``StoredValue`` which exposes ``set(_:)`` and ``reset()`` functions alongside a ``publisher``.
Expand Down

0 comments on commit cdce10e

Please sign in to comment.