Last active
October 25, 2017 06:31
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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