|
| 1 | +// |
| 2 | +// AssignOwnership.swift |
| 3 | +// CombineExt |
| 4 | +// |
| 5 | +// Created by Dmitry Kuznetsov on 08/05/2020. |
| 6 | +// Copyright © 2020 Combine Community. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#if canImport(Combine) |
| 10 | +import Combine |
| 11 | + |
| 12 | +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 13 | +public extension Publisher where Self.Failure == Never { |
| 14 | + /// Assigns a publisher’s output to a property of an object. |
| 15 | + /// |
| 16 | + /// - parameter keyPath: A key path that indicates the property to assign. |
| 17 | + /// - parameter object: The object that contains the property. |
| 18 | + /// The subscriber assigns the object’s property every time |
| 19 | + /// it receives a new value. |
| 20 | + /// - parameter ownership: The retainment / ownership strategy for the object, defaults to `strong`. |
| 21 | + /// |
| 22 | + /// - returns: An AnyCancellable instance. Call cancel() on this instance when you no longer want |
| 23 | + /// the publisher to automatically assign the property. Deinitializing this instance |
| 24 | + /// will also cancel automatic assignment. |
| 25 | + func assign<Root: AnyObject>(to keyPath: ReferenceWritableKeyPath<Root, Self.Output>, |
| 26 | + on object: Root, |
| 27 | + ownership: ObjectOwnership = .strong) -> AnyCancellable { |
| 28 | + switch ownership { |
| 29 | + case .strong: |
| 30 | + return assign(to: keyPath, on: object) |
| 31 | + case .weak: |
| 32 | + return sink { [weak object] value in |
| 33 | + object?[keyPath: keyPath] = value |
| 34 | + } |
| 35 | + case .unowned: |
| 36 | + return sink { [unowned object] value in |
| 37 | + object[keyPath: keyPath] = value |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Assigns each element from a Publisher to properties of the provided objects |
| 43 | + /// |
| 44 | + /// - Parameters: |
| 45 | + /// - keyPath1: The key path of the first property to assign. |
| 46 | + /// - object1: The first object on which to assign the value. |
| 47 | + /// - keyPath2: The key path of the second property to assign. |
| 48 | + /// - object2: The second object on which to assign the value. |
| 49 | + /// - ownership: The retainment / ownership strategy for the object, defaults to `strong`. |
| 50 | + /// |
| 51 | + /// - Returns: A cancellable instance; used when you end assignment of the received value. |
| 52 | + /// Deallocation of the result will tear down the subscription stream. |
| 53 | + func assign<Root1: AnyObject, Root2: AnyObject>( |
| 54 | + to keyPath1: ReferenceWritableKeyPath<Root1, Output>, on object1: Root1, |
| 55 | + and keyPath2: ReferenceWritableKeyPath<Root2, Output>, on object2: Root2, |
| 56 | + ownership: ObjectOwnership = .strong |
| 57 | + ) -> AnyCancellable { |
| 58 | + switch ownership { |
| 59 | + case .strong: |
| 60 | + return assign(to: keyPath1, on: object1, and: keyPath2, on: object2) |
| 61 | + case .weak: |
| 62 | + return sink { [weak object1, weak object2] value in |
| 63 | + object1?[keyPath: keyPath1] = value |
| 64 | + object2?[keyPath: keyPath2] = value |
| 65 | + } |
| 66 | + case .unowned: |
| 67 | + return sink { [unowned object1, unowned object2] value in |
| 68 | + object1[keyPath: keyPath1] = value |
| 69 | + object2[keyPath: keyPath2] = value |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Assigns each element from a Publisher to properties of the provided objects |
| 75 | + /// |
| 76 | + /// - Parameters: |
| 77 | + /// - keyPath1: The key path of the first property to assign. |
| 78 | + /// - object1: The first object on which to assign the value. |
| 79 | + /// - keyPath2: The key path of the second property to assign. |
| 80 | + /// - object2: The second object on which to assign the value. |
| 81 | + /// - keyPath3: The key path of the third property to assign. |
| 82 | + /// - object3: The third object on which to assign the value. |
| 83 | + /// - ownership: The retainment / ownership strategy for the object, defaults to `strong`. |
| 84 | + /// |
| 85 | + /// - Returns: A cancellable instance; used when you end assignment of the received value. |
| 86 | + /// Deallocation of the result will tear down the subscription stream. |
| 87 | + func assign<Root1: AnyObject, Root2: AnyObject, Root3: AnyObject>( |
| 88 | + to keyPath1: ReferenceWritableKeyPath<Root1, Output>, on object1: Root1, |
| 89 | + and keyPath2: ReferenceWritableKeyPath<Root2, Output>, on object2: Root2, |
| 90 | + and keyPath3: ReferenceWritableKeyPath<Root3, Output>, on object3: Root3, |
| 91 | + ownership: ObjectOwnership = .strong |
| 92 | + ) -> AnyCancellable { |
| 93 | + switch ownership { |
| 94 | + case .strong: |
| 95 | + return assign(to: keyPath1, on: object1, |
| 96 | + and: keyPath2, on: object2, |
| 97 | + and: keyPath3, on: object3) |
| 98 | + case .weak: |
| 99 | + return sink { [weak object1, weak object2, weak object3] value in |
| 100 | + object1?[keyPath: keyPath1] = value |
| 101 | + object2?[keyPath: keyPath2] = value |
| 102 | + object3?[keyPath: keyPath3] = value |
| 103 | + } |
| 104 | + case .unowned: |
| 105 | + return sink { [unowned object1, unowned object2, unowned object3] value in |
| 106 | + object1[keyPath: keyPath1] = value |
| 107 | + object2[keyPath: keyPath2] = value |
| 108 | + object3[keyPath: keyPath3] = value |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +#endif |
0 commit comments