Skip to content

Commit

Permalink
Add sink method to quickly attach a MMMLoadableObserver
Browse files Browse the repository at this point in the history
So it now passes the actual loadable subclass instead of a generic
MMMPureLoadableProtocol inside the closure.
  • Loading branch information
terwanerik committed Mar 28, 2022
1 parent fcd8b65 commit 1c6c4da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion MMMLoadable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Pod::Spec.new do |s|

s.name = "MMMLoadable"
s.version = "1.6.4"
s.version = "1.7.0"
s.summary = "A simple model for async calculations"
s.description = "#{s.summary}."
s.homepage = "https://github.com/mediamonks/#{s.name}"
Expand Down
15 changes: 15 additions & 0 deletions Sources/MMMLoadable/MMMLoadable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ extension MMMLoadableState: CustomDebugStringConvertible {

public var debugDescription: String { NSStringFromMMMLoadableState(self) }
}

extension MMMPureLoadableProtocol {

/// Observe changes in this loadable. Will stop listening to changes when
/// ``MMMLoadableObserver/remove()`` is called or the observer deallocates.
///
/// - Parameter block: Get's called every time the loadable changes.
/// - Returns: The observer, you usually want to store this outside of the scope, e.g.
/// in a private property so it doesn't deallocate right away.
public func sink(_ block: @escaping (Self) -> Void) -> MMMLoadableObserver? {
return MMMLoadableObserver(loadable: self) { loadable in
block(loadable as! Self)
}
}
}

0 comments on commit 1c6c4da

Please sign in to comment.