-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathCreate.swift
More file actions
184 lines (165 loc) · 6.57 KB
/
Copy pathCreate.swift
File metadata and controls
184 lines (165 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// Create.swift
// CombineExt
//
// Created by Shai Mishali on 13/03/2020.
// Copyright © 2020 Combine Community. All rights reserved.
//
#if canImport(Combine)
import Combine
import Foundation
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension AnyPublisher {
/// Create a publisher which accepts a closure with a subscriber argument,
/// to which you can dynamically send value or completion events.
///
/// You should return a `Cancelable`-conforming object from the closure in
/// which you can define any cleanup actions to execute when the pubilsher
/// completes or the subscription to the publisher is canceled.
///
/// - parameter factory: A factory with a closure to which you can
/// dynamically send value or completion events.
/// You should return a `Cancelable`-conforming object
/// from it to encapsulate any cleanup-logic for your work.
///
/// An example usage could look as follows:
///
/// ```
/// AnyPublisher<String, MyError>.create { subscriber in
/// // Values
/// subscriber.send("Hello")
/// subscriber.send("World!")
///
/// // Complete with error
/// subscriber.send(completion: .failure(MyError.someError))
///
/// // Or, complete successfully
/// subscriber.send(completion: .finished)
///
/// return AnyCancellable {
/// // Perform clean-up
/// }
/// }
///
init(_ factory: @escaping Publishers.Create<Output, Failure>.SubscriberHandler) {
self = Publishers.Create(factory: factory).eraseToAnyPublisher()
}
/// Create a publisher which accepts a closure with a subscriber argument,
/// to which you can dynamically send value or completion events.
///
/// You should return a `Cancelable`-conforming object from the closure in
/// which you can define any cleanup actions to execute when the pubilsher
/// completes or the subscription to the publisher is canceled.
///
/// - parameter factory: A factory with a closure to which you can
/// dynamically send value or completion events.
/// You should return a `Cancelable`-conforming object
/// from it to encapsulate any cleanup-logic for your work.
///
/// An example usage could look as follows:
///
/// ```
/// AnyPublisher<String, MyError>.create { subscriber in
/// // Values
/// subscriber.send("Hello")
/// subscriber.send("World!")
///
/// // Complete with error
/// subscriber.send(completion: .failure(MyError.someError))
///
/// // Or, complete successfully
/// subscriber.send(completion: .finished)
///
/// return AnyCancellable {
/// // Perform clean-up
/// }
/// }
///
static func create(_ factory: @escaping Publishers.Create<Output, Failure>.SubscriberHandler)
-> AnyPublisher<Output, Failure>
{
AnyPublisher(factory)
}
}
// MARK: - Publisher
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension Publishers {
/// A publisher which accepts a closure with a subscriber argument,
/// to which you can dynamically send value or completion events.
///
/// You should return a `Cancelable`-conforming object from the closure in
/// which you can define any cleanup actions to execute when the pubilsher
/// completes or the subscription to the publisher is canceled.
struct Create<Output, Failure: Swift.Error>: Publisher {
public typealias SubscriberHandler = (Subscriber) -> Cancellable
private let factory: SubscriberHandler
/// Initialize the publisher with a provided factory
///
/// - parameter factory: A factory with a closure to which you can
/// dynamically push value or completion events
public init(factory: @escaping SubscriberHandler) {
self.factory = factory
}
public func receive<S: Combine.Subscriber>(subscriber: S) where Failure == S.Failure, Output == S.Input {
subscriber.receive(subscription: Subscription(factory: factory, downstream: subscriber))
}
}
}
// MARK: - Subscription
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
private extension Publishers.Create {
class Subscription<Downstream: Combine.Subscriber>: Combine.Subscription where Output == Downstream.Input, Failure == Downstream.Failure {
private let buffer: DemandBuffer<Downstream>
private var cancelable: Cancellable?
init(
factory: @escaping SubscriberHandler,
downstream: Downstream
) {
buffer = DemandBuffer(subscriber: downstream)
let subscriber = Subscriber(
onValue: { [weak self] in _ = self?.buffer.buffer(value: $0) },
onCompletion: { [weak self] in self?.buffer.complete(completion: $0) }
)
cancelable = factory(subscriber)
}
func request(_ demand: Subscribers.Demand) {
_ = buffer.demand(demand)
}
func cancel() {
cancelable?.cancel()
}
}
}
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publishers.Create.Subscription: CustomStringConvertible {
var description: String {
"Create.Subscription<\(Output.self), \(Failure.self)>"
}
}
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension Publishers.Create {
struct Subscriber {
private let onValue: (Output) -> Void
private let onCompletion: (Subscribers.Completion<Failure>) -> Void
fileprivate init(
onValue: @escaping (Output) -> Void,
onCompletion: @escaping (Subscribers.Completion<Failure>) -> Void
) {
self.onValue = onValue
self.onCompletion = onCompletion
}
/// Sends a value to the subscriber.
///
/// - Parameter value: The value to send.
public func send(_ input: Output) {
onValue(input)
}
/// Sends a completion event to the subscriber.
///
/// - Parameter completion: A `Completion` instance which indicates whether publishing has finished normally or failed with an error.
public func send(completion: Subscribers.Completion<Failure>) {
onCompletion(completion)
}
}
}
#endif