sideeffect-io / AsyncExtensions

AsyncExtensions aims to mimic Swift Combine operators for async sequences.
MIT License
324 stars 26 forks source link

[Question] How to resolve an issue I ran into from the current changes. #7

Closed Prince2k3 closed 2 years ago

Prince2k3 commented 2 years ago

First off let me start of saying that I like the changes you've made recently it helps clean things up and make the code more maintainable.

I have a property wrapper that wraps UserDefaults. And before I had Combine as a way to listen to changes, via CurrentValueSubject, on UserDefaults. Since I'm in the process of removing Combine and using strictly AsyncSequence I started using CurrentValue version.

@propertyWrapper
public struct Defaults<T: Codable> {
    private let key: String

    public let currentValue: AsyncStreams.CurrentValue<T>

    public var projectedValue: Defaults<T> { self }

    public var wrappedValue: T {
        get { currentValue.element }  <-- Here is my problem
        set {
            currentValue.send(newValue) <-- Here is my problem
            userDefaults.set(object: newValue, forKey: key)
        }
    }

    public init(wrappedValue defaultValue: T, _ key: String) {
        self.key = key
        self.currentValue = AsyncStreams.CurrentValue<T>(defaultValue)
        self.wrappedValue = userDefaults.object(T.self, with: key) ?? defaultValue
    }
}

element property now requires I use async/await here. My question is ... Do you know how I can resolve this and still meet the property wrappers need for wrappedValue?

Thanks!