forked from AvdLee/CombineSwiftPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rules of a subscription have been added
- Loading branch information
Showing
2 changed files
with
30 additions
and
2 deletions.
There are no files selected for viewing
30 changes: 29 additions & 1 deletion
30
Combine.playground/Pages/Rules of subscriptions.xcplaygroundpage/Contents.swift
This file contains 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
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) |
This file contains 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