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
| enum Event<Element> { case next(Element) case error(Error) case finish }
protocol PublisherType { associatedtype Element func pub(event: Event<Element>) }
class Publisher<Element>: PublisherType { private let _handler: (Event<Element>) -> Void init(_ handler: @escaping (Event<Element>) -> Void) { _handler = handler } func pub(event: Event<Element>) { _handler(event) } }
protocol SubscriberType { associatedtype Element func subscribe<P: PublisherType>(publisher: P) where P.Element == Element }
class Subscriber<Element>: SubscriberType { private let _eventGenerator: (Publisher<Element>) -> Void init(_ eventGenerator: @escaping (Publisher<Element>) -> Void) { _eventGenerator = eventGenerator } func subscribe<P: PublisherType>(publisher: P) where P.Element == Element { _eventGenerator(publisher as! Publisher<Element>) } }
|