|
| 1 | +/* |
| 2 | + * Copyright 2023 Square Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import Combine |
| 18 | +import Workflow |
| 19 | + |
| 20 | +@dynamicMemberLookup |
| 21 | +public final class ObservableValue<Value>: ObservableObject { |
| 22 | + private var internalValue: Value |
| 23 | + private let subject = PassthroughSubject<Value, Never>() |
| 24 | + private var cancellable: AnyCancellable? |
| 25 | + private let isEquivalent: ((Value, Value) -> Bool)? |
| 26 | + |
| 27 | + public private(set) var value: Value { |
| 28 | + get { |
| 29 | + return internalValue |
| 30 | + } |
| 31 | + set { |
| 32 | + subject.send(newValue) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public private(set) lazy var objectWillChange = ObservableObjectPublisher() |
| 37 | + private var parentCancellable: AnyCancellable? |
| 38 | + |
| 39 | + public static func makeObservableValue( |
| 40 | + _ value: Value, |
| 41 | + isEquivalent: ((Value, Value) -> Bool)? = nil |
| 42 | + ) -> (ObservableValue, Sink<Value>) { |
| 43 | + let observableValue = ObservableValue(value: value, isEquivalent: isEquivalent) |
| 44 | + let sink = Sink { newValue in |
| 45 | + observableValue.value = newValue |
| 46 | + } |
| 47 | + |
| 48 | + return (observableValue, sink) |
| 49 | + } |
| 50 | + |
| 51 | + private init(value: Value, isEquivalent: ((Value, Value) -> Bool)?) { |
| 52 | + self.internalValue = value |
| 53 | + self.isEquivalent = isEquivalent |
| 54 | + self.cancellable = valuePublisher() |
| 55 | + .dropFirst() |
| 56 | + .sink { [weak self] newValue in |
| 57 | + guard let self = self else { return } |
| 58 | + self.objectWillChange.send() |
| 59 | + self.internalValue = newValue |
| 60 | + } |
| 61 | + // Allows removeDuplicates operator to have the initial value. |
| 62 | + subject.send(value) |
| 63 | + } |
| 64 | + |
| 65 | + //// Scopes the ObservableValue to a subset of Value to LocalValue given the supplied closure while allowing to optionally remove duplicates. |
| 66 | + /// - Parameters: |
| 67 | + /// - toLocalValue: A closure that takes a Value and returns a LocalValue. |
| 68 | + /// - isEquivalent: An optional closure that checks to see if a LocalValue is equivalent. |
| 69 | + /// - Returns: a scoped ObservableValue of LocalValue. |
| 70 | + public func scope<LocalValue>(_ toLocalValue: @escaping (Value) -> LocalValue, isEquivalent: ((LocalValue, LocalValue) -> Bool)? = nil) -> ObservableValue<LocalValue> { |
| 71 | + return scopeToLocalValue(toLocalValue, isEquivalent: isEquivalent) |
| 72 | + } |
| 73 | + |
| 74 | + /// Scopes the ObservableValue to a subset of Value to LocalValue given the supplied closure and removes duplicate values using Equatable. |
| 75 | + /// - Parameter toLocalValue: A closure that takes a Value and returns a LocalValue. |
| 76 | + /// - Returns: a scoped ObservableValue of LocalValue. |
| 77 | + public func scope<LocalValue>(_ toLocalValue: @escaping (Value) -> LocalValue) -> ObservableValue<LocalValue> where LocalValue: Equatable { |
| 78 | + return scopeToLocalValue(toLocalValue, isEquivalent: { $0 == $1 }) |
| 79 | + } |
| 80 | + |
| 81 | + /// Returns the value at the given keypath of ``Value``. |
| 82 | + /// |
| 83 | + /// In combination with `@dynamicMemberLookup`, this allows us to write `model.myProperty` instead of |
| 84 | + /// `model.value.myProperty` where `model` has type `ObservableValue<T>`. |
| 85 | + public subscript<T>(dynamicMember keyPath: KeyPath<Value, T>) -> T { |
| 86 | + internalValue[keyPath: keyPath] |
| 87 | + } |
| 88 | + |
| 89 | + private func scopeToLocalValue<LocalValue>(_ toLocalValue: @escaping (Value) -> LocalValue, isEquivalent: ((LocalValue, LocalValue) -> Bool)? = nil) -> ObservableValue<LocalValue> { |
| 90 | + let localObservableValue = ObservableValue<LocalValue>( |
| 91 | + value: toLocalValue(internalValue), |
| 92 | + isEquivalent: isEquivalent |
| 93 | + ) |
| 94 | + localObservableValue.parentCancellable = valuePublisher().sink(receiveValue: { newValue in |
| 95 | + localObservableValue.value = toLocalValue(newValue) |
| 96 | + }) |
| 97 | + return localObservableValue |
| 98 | + } |
| 99 | + |
| 100 | + private func valuePublisher() -> AnyPublisher<Value, Never> { |
| 101 | + guard let isEquivalent = isEquivalent else { |
| 102 | + return subject.eraseToAnyPublisher() |
| 103 | + } |
| 104 | + |
| 105 | + return subject.removeDuplicates(by: isEquivalent).eraseToAnyPublisher() |
| 106 | + } |
| 107 | +} |
0 commit comments