Files
Brandon Williams 7c6fb26700 Address @Shared sendability. (#3329)
* Address @Shared sendability.

* Undo UncheckedSendable<UserDefaults>.

* clean up

* wip

* drop AnySendable.

* wip

* Address `Effect.throttle` sendability (#3325)

* Address effect cancellation sendability (#3326)

* Address effect cancellation sendability

* fix

* wip

* wip

* Separate SendableDefaultSubscript from DefaultSubscript.

* fix test

* drop escaping

* switch on swift 6 language mode

* xcode 16

* update test

* wip

---------

Co-authored-by: Stephen Celis <stephen@stephencelis.com>
2024-09-06 16:28:23 -04:00

62 lines
1.9 KiB
Swift

import Dependencies
import Foundation
extension PersistenceReaderKey {
/// Creates a persistence key for sharing data in-memory for the lifetime of an application.
///
/// For example, one could initialize a key with the date and time at which the application was
/// most recently launched, and access this date from anywhere using the ``Shared`` property
/// wrapper:
///
/// ```swift
/// @Shared(.inMemory("appLaunchedAt")) var appLaunchedAt = Date()
/// ```
///
/// - Parameter key: A string key identifying a value to share in memory.
/// - Returns: An in-memory persistence key.
public static func inMemory<Value>(_ key: String) -> Self
where Self == InMemoryKey<Value> {
InMemoryKey(key)
}
}
/// A type defining an in-memory persistence strategy
///
/// See ``PersistenceReaderKey/inMemory(_:)`` to create values of this type.
public struct InMemoryKey<Value: Sendable>: PersistenceKey, Sendable {
private let key: String
private let store: InMemoryStorage
fileprivate init(_ key: String) {
@Dependency(\.defaultInMemoryStorage) var defaultInMemoryStorage
self.key = key
self.store = defaultInMemoryStorage
}
public var id: AnyHashable {
InMemoryKeyID(key: self.key, store: self.store)
}
public func load(initialValue: Value?) -> Value? { initialValue }
public func save(_ value: Value) {}
}
public struct InMemoryStorage: Hashable, Sendable {
private let id = UUID()
public init() {}
}
private struct InMemoryKeyID: Hashable {
let key: String
let store: InMemoryStorage
}
private enum DefaultInMemoryStorageKey: DependencyKey {
static var liveValue: InMemoryStorage { InMemoryStorage() }
static var testValue: InMemoryStorage { InMemoryStorage() }
}
extension DependencyValues {
public var defaultInMemoryStorage: InMemoryStorage {
get { self[DefaultInMemoryStorageKey.self] }
set { self[DefaultInMemoryStorageKey.self] = newValue }
}
}