Skip to content

Commit

Permalink
Rules of a subscription have been added
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee committed Jun 26, 2019
1 parent d173032 commit b0266cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
/*:
[Previous](@previous)
## The rules of a subscription
- There can only be _one_ subscriber
- A subscriber will receive a _single_ subscription
- _Zero_ or _more_ values can be published
- At most _one_ completion will be called
*/
import Combine
import UIKit

enum ExampleError: Swift.Error {
case somethingWentWrong
}

let subject = PassthroughSubject<String, ExampleError>()

//let publisher = Publisher<String, ExampleError>(subject: subject)
subject.handleEvents(receiveSubscription: { (subscription) in
print("New subscription!")
}, receiveOutput: { _ in
print("Received new value!")
}, receiveCompletion: { _ in
print("A subscription completed")
}, receiveCancel: {
print("A subscription cancelled")
}).sink { (value) in
print("Subscriber one received value: \(value)")
}

subject.send("Hello!")
subject.send("Hello again!")
subject.send("Hello for the last time!")
subject.send(completion: Subscribers.Completion<ExampleError>.failure(ExampleError.somethingWentWrong))
subject.send("Hello?? :(")

//: [Next](@next)
2 changes: 1 addition & 1 deletion Combine.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios' display-mode='rendered'>
<playground version='6.0' target-platform='ios' display-mode='raw'>
<pages>
<page name='What is Combine'/>
<page name='Publishers &amp; Subscribers'/>
Expand Down

0 comments on commit b0266cb

Please sign in to comment.