Skip to content

Instantly share code, notes, and snippets.

@ahernandezlopez
Last active October 25, 2017 06:31
Show Gist options
  • Save ahernandezlopez/32c3b42a19bbd04873697b667ffa8b16 to your computer and use it in GitHub Desktop.
Save ahernandezlopez/32c3b42a19bbd04873697b667ffa8b16 to your computer and use it in GitHub Desktop.
Example of implementation of Domain Events in Swift. The snippet illustrates Domain Events in a Service with RxSwift
import RxSwift
class LocalListingService: ListingService {
var events: Observable<ListingServiceEvent> {
return eventsPublishSubject.asObservable()
}
private let eventsPublishSubject: PublishSubject<ListingServiceEvent>
// ...
func create(params: ListingServiceCreateParams,
completion: ((Result<Listing, ListingServiceCreateError>) -> ())?) {
let listing = Listing(id: String.makeRandom(length: 5),
title: params.title,
price: params.price)
let result = Result<Listing, ListingServiceCreateError>.success(data: listing)
let deadline: DispatchTime = .now() + .milliseconds(50)
DispatchQueue.main.asyncAfter(deadline: deadline) { [weak self] in
defer { completion?(result) }
self?.eventsPublishSubject.onNext(ListingServiceEvent.create(listing: listing))
}
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment