How do I implement an event emitter using RxSwift? (An object that can emit data that is consumed by other objects that are subscribed to it.)
After going through the Rx docs and examples, I feel like a complete idiot and am still extremely confused on how to manually emit events from Observers to Observables. My understanding that we have some Observable that can emit events with data to all Observers that are subscribed to that Observable. However, I have zero idea on how this is actually implemented in Swift.
Here's an example of something I'm trying to implement:
class VendingMachine {
let dispenser = Observable<Drink>
// Notify all subscribed Observers that this machine dispensed a drink.
func dispenseDrink(item: Drink) {
dispenser.onNext(item)
}
}
And a second file:
class MachineReporter: Observer {
let dispenser = VendingMachine().dispenser
init() {
dispenser.subscribe(self)
}
onNext { drink in
print("Vending machine dispensed a drink: \(drink)"
}
}
My brain is fried. I'm just going to switch to a specialized library like EmitterKit for now because I'm clearly misunderstanding how this works.
But I need to figure out how Rx works or I will go crazy. Help!