PersistenceKeyDefault eagerly evaluates its defaultValue. This means every declaration of a Shared value creates a new copy of this value when it's evaluated. Since the normal pattern for persisting shared values is to successfully load from its source, the default value is generated and thrown away without being used.
Additionally, creating this default value can have side effects. Consider a testing context where the default value uses the uuid dependency. Here's a persistence key whose default value creates a Folder value whose initializer generates an id from the current uuid dependency.
extension PersistenceKey where Self == PersistenceKeyDefault<FileStorageKey<Folder>> {
public static var rootFolder: Self {
PersistenceKeyDefault(
.fileStorage(.documentsDirectory.appendingPathComponent("rootFolder", conformingTo: .json)),
Folder()
)
}
}
Now when running a test that overrides the default with a predetermined value, the creation of the default value has incremented the uuid dependency, even though the value isn't used.
This request modifies PersistenceKeyDefault.defaultValue to lazily evaluate only when the load action fails. It also includes a couple tests to verify the change.
PersistenceKeyDefault
eagerly evaluates itsdefaultValue
. This means every declaration of aShared
value creates a new copy of this value when it's evaluated. Since the normal pattern for persisting shared values is to successfully load from its source, the default value is generated and thrown away without being used.Additionally, creating this default value can have side effects. Consider a testing context where the default value uses the uuid dependency. Here's a persistence key whose default value creates a
Folder
value whose initializer generates an id from the current uuid dependency.Now when running a test that overrides the default with a predetermined value, the creation of the default value has incremented the uuid dependency, even though the value isn't used.
This request modifies
PersistenceKeyDefault.defaultValue
to lazily evaluate only when theload
action fails. It also includes a couple tests to verify the change.