|
1 | 1 | import Combine
|
2 | 2 |
|
3 | 3 | extension Publisher where Self.Failure==Never {
|
4 |
| - /// Assigns a publisher's output to a property of an object. |
5 |
| - /// |
6 |
| - /// The difference between `assign(to:onWeak:)` and Combine's `assign(to:on:)` is two-fold: |
7 |
| - /// - `assign(to:onWeak:)` doesn't set a _strong bond_ to `object`. |
8 |
| - /// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
9 |
| - /// - `assign(to:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized. |
10 |
| - /// |
11 |
| - /// The difference between is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
12 |
| - /// - parameter keyPath: A key path that indicates the property to assign. |
13 |
| - /// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value. |
14 |
| - /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment. |
15 |
| - @_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onWeak object: Root) -> AnyCancellable where Root:AnyObject { |
16 |
| - weak var cancellable: AnyCancellable? = nil |
17 |
| - let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in |
18 |
| - cancellable?.cancel() |
19 |
| - cancellable = nil |
20 |
| - } |
21 |
| - |
22 |
| - let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in |
23 |
| - guard let object = object else { return cleanup(.finished) } |
24 |
| - object[keyPath: keyPath] = value |
25 |
| - }) |
26 |
| - |
27 |
| - let result = AnyCancellable(subscriber) |
28 |
| - cancellable = result |
29 |
| - self.subscribe(subscriber) |
30 |
| - return result |
31 |
| - } |
32 |
| - |
33 |
| - /// Assigns a publisher's output to a property of an object. |
34 |
| - /// |
35 |
| - /// The difference between `assign(to:onUnowned:)` and Combine's `assign(to:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
36 |
| - /// - parameter keyPath: A key path that indicates the property to assign. |
37 |
| - /// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value. |
38 |
| - /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment. |
39 |
| - @_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onUnowned object: Root) -> AnyCancellable where Root:AnyObject { |
40 |
| - self.sink(receiveValue: { [unowned object] (value) in |
41 |
| - object[keyPath: keyPath] = value |
42 |
| - }) |
| 4 | + /// Assigns a publisher's output to a property of an object. |
| 5 | + /// |
| 6 | + /// The difference between `assign(to:onWeak:)` and Combine's `assign(to:on:)` is two-fold: |
| 7 | + /// - `assign(to:onWeak:)` doesn't set a _strong bond_ to `object`. |
| 8 | + /// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
| 9 | + /// - `assign(to:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized. |
| 10 | + /// |
| 11 | + /// The difference between is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
| 12 | + /// - parameter keyPath: A key path that indicates the property to assign. |
| 13 | + /// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value. |
| 14 | + /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment. |
| 15 | + @_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onWeak object: Root) -> AnyCancellable where Root:AnyObject { |
| 16 | + weak var cancellable: AnyCancellable? = nil |
| 17 | + let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in |
| 18 | + cancellable?.cancel() |
| 19 | + cancellable = nil |
43 | 20 | }
|
| 21 | + |
| 22 | + let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in |
| 23 | + guard let object = object else { return cleanup(.finished) } |
| 24 | + object[keyPath: keyPath] = value |
| 25 | + }) |
| 26 | + |
| 27 | + let result = AnyCancellable(subscriber) |
| 28 | + cancellable = result |
| 29 | + self.subscribe(subscriber) |
| 30 | + return result |
| 31 | + } |
| 32 | + |
| 33 | + /// Assigns a publisher's output to a property of an object. |
| 34 | + /// |
| 35 | + /// The difference between `assign(to:onUnowned:)` and Combine's `assign(to:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
| 36 | + /// - parameter keyPath: A key path that indicates the property to assign. |
| 37 | + /// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value. |
| 38 | + /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment. |
| 39 | + @_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onUnowned object: Root) -> AnyCancellable where Root:AnyObject { |
| 40 | + self.sink(receiveValue: { [unowned object] (value) in |
| 41 | + object[keyPath: keyPath] = value |
| 42 | + }) |
| 43 | + } |
44 | 44 | }
|
45 | 45 |
|
46 | 46 | extension Publisher where Self.Failure==Never {
|
47 |
| - /// Invoke on the given instance the specified method. |
48 |
| - /// - remark: A strong bond is set to `instance`. If you store the cancellable in the same instance as `instance`, a memory cycle will be created. |
49 |
| - /// - parameter method: A method/function metatype. |
50 |
| - /// - parameter instance: The instance defining the specified method. |
51 |
| - /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation. |
52 |
| - @_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, on instance: Root) -> AnyCancellable { |
53 |
| - return self.sink(receiveValue: { (value) in |
54 |
| - method(instance)(value) |
55 |
| - }) |
56 |
| - } |
57 |
| - |
58 |
| - /// Invoke on the given instance the specified method. |
59 |
| - /// |
60 |
| - /// The difference between `invoke(_:onWeak:)` and Combine's `invoke(_:on:)` is two-fold: |
61 |
| - /// - `invoke(_:onWeak:)` doesn't set a _strong bond_ to `object`. |
62 |
| - /// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
63 |
| - /// - `invoke(_:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized. |
64 |
| - /// |
65 |
| - /// - parameter method: A method/function metatype. |
66 |
| - /// - parameter instance: The instance defining the specified method. |
67 |
| - /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation. |
68 |
| - @_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onWeak object: Root) -> AnyCancellable where Root:AnyObject { |
69 |
| - weak var cancellable: AnyCancellable? = nil |
70 |
| - let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in |
71 |
| - cancellable?.cancel() |
72 |
| - cancellable = nil |
73 |
| - } |
74 |
| - |
75 |
| - let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in |
76 |
| - guard let object = object else { return cleanup(.finished) } |
77 |
| - method(object)(value) |
78 |
| - }) |
79 |
| - |
80 |
| - let result = AnyCancellable(subscriber) |
81 |
| - cancellable = result |
82 |
| - self.subscribe(subscriber) |
83 |
| - return result |
84 |
| - } |
85 |
| - |
86 |
| - /// Invoke on the given instance the specified method. |
87 |
| - /// |
88 |
| - /// The difference between `invoke(_:onUnowned:)` and Combine's `invoke(_:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
89 |
| - /// - parameter method: A method/function metatype. |
90 |
| - /// - parameter instance: The instance defining the specified method. |
91 |
| - /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation. |
92 |
| - @_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onUnowned object: Root) -> AnyCancellable where Root:AnyObject { |
93 |
| - return self.sink(receiveValue: { [unowned object] (value) in |
94 |
| - method(object)(value) |
95 |
| - }) |
| 47 | + /// Invoke on the given instance the specified method. |
| 48 | + /// - remark: A strong bond is set to `instance`. If you store the cancellable in the same instance as `instance`, a memory cycle will be created. |
| 49 | + /// - parameter method: A method/function metatype. |
| 50 | + /// - parameter instance: The instance defining the specified method. |
| 51 | + /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation. |
| 52 | + @_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, on instance: Root) -> AnyCancellable { |
| 53 | + self.sink(receiveValue: { (value) in |
| 54 | + method(instance)(value) |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + /// Invoke on the given instance the specified method. |
| 59 | + /// |
| 60 | + /// The difference between `invoke(_:onWeak:)` and Combine's `invoke(_:on:)` is two-fold: |
| 61 | + /// - `invoke(_:onWeak:)` doesn't set a _strong bond_ to `object`. |
| 62 | + /// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
| 63 | + /// - `invoke(_:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized. |
| 64 | + /// |
| 65 | + /// - parameter method: A method/function metatype. |
| 66 | + /// - parameter instance: The instance defining the specified method. |
| 67 | + /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation. |
| 68 | + @_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onWeak object: Root) -> AnyCancellable where Root:AnyObject { |
| 69 | + weak var cancellable: AnyCancellable? = nil |
| 70 | + let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in |
| 71 | + cancellable?.cancel() |
| 72 | + cancellable = nil |
96 | 73 | }
|
| 74 | + |
| 75 | + let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in |
| 76 | + guard let object = object else { return cleanup(.finished) } |
| 77 | + method(object)(value) |
| 78 | + }) |
| 79 | + |
| 80 | + let result = AnyCancellable(subscriber) |
| 81 | + cancellable = result |
| 82 | + self.subscribe(subscriber) |
| 83 | + return result |
| 84 | + } |
| 85 | + |
| 86 | + /// Invoke on the given instance the specified method. |
| 87 | + /// |
| 88 | + /// The difference between `invoke(_:onUnowned:)` and Combine's `invoke(_:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case). |
| 89 | + /// - parameter method: A method/function metatype. |
| 90 | + /// - parameter instance: The instance defining the specified method. |
| 91 | + /// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation. |
| 92 | + @_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onUnowned object: Root) -> AnyCancellable where Root:AnyObject { |
| 93 | + self.sink(receiveValue: { [unowned object] (value) in |
| 94 | + method(object)(value) |
| 95 | + }) |
| 96 | + } |
97 | 97 | }
|
0 commit comments