EventPublisher
public class EventPublisher<T> : EventPublisherType
An event publisher that handles registration and dispatching of events.
Warning: Not thread-safe.
-
Publishes a given value to all event listeners currently subscribed to this event publisher.
Declaration
Swift
public func publish(value: T)
-
Adds a new event listener, tied to a given weakly-held owner object reference, with a listener closure that will be invoked when future events are published on this event publisher.
weakOwner
is used to create a weak ownership link that is verified internally during event processing, and ifweakOwner
is released from memory, the underlying event listener that is registered is removed, as if by callingEventListenerKey.removeListener()
.Declaration
Swift
public func addListener(weakOwner: AnyObject, _ listener: @escaping (T) -> Void) -> EventListenerKey
-
Convenience for:
addListener(weakOwner: owner) { [weak owner] value in guard let owner else { return } ... }
Declaration
Swift
@discardableResult public func addWeakListener<U>(_ weakOwner: U, _ listener: @escaping (U, T) -> Void) -> EventListenerKey where U : AnyObject
-
Requesta that an event listener associated with a given key be unsubscribed from this event publisher.
Declaration
Swift
public func removeListener(forKey key: EventListenerKey)
-
Removes all event subscriptions associated with a given owner object, by verifying the original
weakOwner
argument that was passed toaddListener(weakOwner:_:)
. Matching is done by object identity, i.e. via===
.Declaration
Swift
public func removeAll(fromOwner owner: AnyObject)
-
Creates a wrapping event source on top of this event publisher that can be used to expose an API for event listeners to subscribe to without exposing the event publishing API as well.
Declaration
Swift
public func makeEventSource() -> EventSource<T>