RVS_GeneralObservableProtocol
public protocol RVS_GeneralObservableProtocol : AnyObject
This protocol makes any entity observable, by giving it the infrastructure to subscribe and track observers. It’s up to the implementor to actually use this to send messages.
Observables have to be classes. Observers don’t need to be classes.
There are two required components: a UUID, which needs to remain constant during the lifetime of the instance, and an Array of observers.
-
This is a unique ID for this Observer.
If you are an observer, then you MUST supply a unique ID that remains constant throughout the lifetime of the instance.
It must be declared as a stored property, and initialized with UUID(). After that, leave it alone.
It should look like this, in your implementation:
let uuid = UUID()
Declaration
Swift
var uuid: UUID { get }
-
This is an Array of observers that subscribe to the observable instance.
It should be noted that this would be a strong reference, if the observers were classes.
Declaration
Swift
var observers: [RVS_GeneralObserverProtocol] { get set }
-
subscribe(_:
Default implementation) This is how an observer subscribes to an Observable.
Default Implementation
The default will simply append the observer to our list.
Declaration
Swift
@discardableResult func subscribe(_: RVS_GeneralObserverProtocol) -> RVS_GeneralObserverProtocol?
Return Value
The subscribed element, if the subscription worked (or if the observer is already subscribed). Nil, otherwise. Can be ignored.
-
unsubscribe(_:
Default implementation) This is how an observer unsubscribes from an Observable.
Default Implementation
The default will simply remove the observer from our list.
Declaration
Swift
@discardableResult func unsubscribe(_: RVS_GeneralObserverProtocol) -> RVS_GeneralObserverProtocol?
Return Value
The unsubscribed element, if the unsubscription worked (or if the observer is already unsubscribed). Nil, otherwise. Can be ignored.
-
unsubscribeAll()
Default implementationUnsubscribes all currently subscribed entities.
Default Implementation
Default simply unsubscribes all the elements.
Declaration
Swift
@discardableResult func unsubscribeAll() -> [RVS_GeneralObserverProtocol]
Return Value
The subscribers that were removed. Can be ignored.
-
amISubscribed(_:
Default implementation) An Observer can use this to see if they are already subscribed to an Observable.
In the default implementation, this is called in the subscription/unsubscritption execution context, so that will be the thread used for the callback.
Default Implementation
The default will run through the Array, looking at UUIDs.
Declaration
Swift
func amISubscribed(_: RVS_GeneralObserverProtocol) -> Bool
Return Value
True, if subscribed.
-
observer(_:
Default implementationdidSubscribe: ) This is method that is called when observers subscribe or unsubscribe. The default does nothing. This is called AFTER the fact, and only if the subscription status changed.
Default Implementation
The default does nothing.
Declaration
Swift
func observer(_: RVS_GeneralObserverProtocol, didSubscribe: Bool)
Parameters
didSubscribe
True, if this was a subscription. False, if not.