Skip to content

Commit 5da165a

Browse files
committed
Move PublishSubject usage example from Observable to the Subject
1 parent f5d50c4 commit 5da165a

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

WooCommerce/Classes/Tools/Observables/Observable.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,6 @@ typealias OnNext<Element> = (Element) -> ()
1212
/// This class is a pseudo-abstract class. It does not do anything on its own. Use the
1313
/// subclasses like `PublishSubject` instead.
1414
///
15-
/// This abstract class is meant to be a "readonly" `Observable` to hide the mutable methods
16-
/// of observables like `PublishSubject`. For example, if we use `PublishSubject` and expose it
17-
/// in the `ViewModel` like this:
18-
///
19-
/// ```
20-
/// class ViewModel {
21-
/// let onDataLoaded = PublishSubject<[Items]>()
22-
/// }
23-
/// ```
24-
///
25-
/// The `ViewController` will be able to access the **mutating** methods of `PublishSubject` like:
26-
///
27-
/// ```
28-
/// // Submit new items. This will ultimately call the `subscribe` callbacks.
29-
/// viewModel.onDataLoaded.send([Item]())
30-
/// ```
31-
///
32-
/// Ideally, the `ViewController` should only have the `Observable` _interface_ so it only has
33-
/// access to `subscribe()`. We can do that this way:
34-
///
35-
/// ```
36-
/// class ViewModel {
37-
/// private let onDataLoadedSubject = PublishSubject<[Items]>()
38-
///
39-
/// // Expose a readonly observable
40-
/// var onDataLoaded: Observable<[Items]> {
41-
/// onDataLoadedSubject
42-
/// }
43-
/// }
44-
/// ```
45-
///
4615
/// See here for more info about Observables:
4716
///
4817
/// - https://developer.apple.com/documentation/combine/publisher

WooCommerce/Classes/Tools/Observables/PublishSubject.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,55 @@ import Foundation
1212
/// Multiple observers are allowed which makes this a possible replacement for
1313
/// `NSNotificationCenter` observations.
1414
///
15+
/// ## Example Usage
16+
///
17+
/// In a class that you would like to emit values (or events), add the `PublishSubject` defining
18+
/// the value type:
19+
///
20+
/// ```
21+
/// class PostListViewModel {
22+
/// /// Calls observers/subscribers whenever the list of Post changes.
23+
/// private let postsSubject = PublishSubject<[Post]>()
24+
/// }
25+
/// ```
26+
///
27+
/// Since `PublishSubject` exposes `send()` which makes this a **mutable** Observable, we recommend
28+
/// exposing only the `Observable<[Post]>` interface:
29+
///
30+
/// ```
31+
/// class PostListViewModel {
32+
/// private let postsSubject = PublishSubject<[Post]>()
33+
///
34+
/// /// The public Observable that the ViewController will subscribe to
35+
/// var posts: Observable<[Post]> {
36+
/// postsSubject
37+
/// }
38+
/// }
39+
/// ```
40+
///
41+
/// The `ViewController` can then subscribe to the `posts` Observable:
42+
///
43+
/// ```
44+
/// func viewDidLoad() {
45+
/// viewModel.posts.subscribe { posts in
46+
/// // do something with posts
47+
/// tableView.reloadData()
48+
/// }
49+
/// }
50+
/// ```
51+
///
52+
/// Whenever the list of post changes, like after fetching from the API, the `ViewModel` can
53+
/// _notify_ the `ViewController` by updating `postsSubject`:
54+
///
55+
/// ```
56+
/// fetchFromAPI { fetchedPosts
57+
/// // Notify the observers (e.g. ViewController) that the list of posts have changed
58+
/// postsSubject.send(fetchedPosts)
59+
/// }
60+
/// ```
61+
///
62+
/// ## References
63+
///
1564
/// See here for info about similar observables in other frameworks:
1665
///
1766
/// - https://developer.apple.com/documentation/combine/passthroughsubject

0 commit comments

Comments
 (0)