Factory
public struct Factory<Output, Failure> : Publisher where Failure : Error
Creates a simple publisher inline from a provided closure
This publisher can be used to turn any arbitrary source of values (such as a timer or a user authorization request) into a new publisher sequence.
From within the scope of the closure passed into the initializer, it is possible to call the methods of the
Dispatcher
object – which is passed in as a parameter – to send values down stream.
Example
import Entwine
let photoKitAuthorizationStatus = Publishers.Factory<PHAuthorizationStatus, Never> { dispatcher in
let status = PHPhotoLibrary.authorizationStatus()
dispatcher.forward(status)
switch status {
case .notDetermined:
PHPhotoLibrary.requestAuthorization { newStatus in
dispatcher.forward(newStatus)
dispatcher.forward(completion: .finished)
}
default:
dispatcher.forward(completion: .finished)
}
return AnyCancellable {}
}
Warning
Developers should be aware that aDispatcher
has an unbounded buffer that stores values
yet to be requested by the downstream subscriber.
When creating a publisher from a source with an unbounded rate of production that cannot be influenced,
developers should consider following this operator with a Publishers.Buffer
operator to prevent a
strain on resources
-
Declaration
Swift
public func receive<S>(subscriber: S) where Output == S.Input, Failure == S.Failure, S : Subscriber