Files
OrderScheduling/Pods/RxCocoa/RxCocoa/Traits/ControlEvent.swift
DDIsFriend f0e8a1709d initial
2023-08-18 17:28:57 +08:00

69 lines
2.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ControlEvent.swift
// RxCocoa
//
// Created by Krunoslav Zaher on 8/28/15.
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
import RxSwift
/// A protocol that extends `ControlEvent`.
public protocol ControlEventType : ObservableType {
/// - returns: `ControlEvent` interface
func asControlEvent() -> ControlEvent<Element>
}
/**
A trait for `Observable`/`ObservableType` that represents an event on a UI element.
Properties:
- it doesnt send any initial value on subscription,
- it `Complete`s the sequence when the control deallocates,
- it never errors out
- it delivers events on `MainScheduler.instance`.
**The implementation of `ControlEvent` will ensure that sequence of events is being subscribed on main scheduler
(`subscribe(on: ConcurrentMainScheduler.instance)` behavior).**
**It is the implementors responsibility to make sure that all other properties enumerated above are satisfied.**
**If they arent, using this trait will communicate wrong properties, and could potentially break someones code.**
**If the `events` observable sequence passed into the initializer doesnt satisfy all enumerated
properties, dont use this trait.**
*/
public struct ControlEvent<PropertyType> : ControlEventType {
public typealias Element = PropertyType
let events: Observable<PropertyType>
/// Initializes control event with a observable sequence that represents events.
///
/// - parameter events: Observable sequence that represents events.
/// - returns: Control event created with a observable sequence of events.
public init<Ev: ObservableType>(events: Ev) where Ev.Element == Element {
self.events = events.subscribe(on: ConcurrentMainScheduler.instance)
}
/// Subscribes an observer to control events.
///
/// - parameter observer: Observer to subscribe to events.
/// - returns: Disposable object that can be used to unsubscribe the observer from receiving control events.
public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
self.events.subscribe(observer)
}
/// - returns: `Observable` interface.
public func asObservable() -> Observable<Element> {
self.events
}
/// - returns: `ControlEvent` interface.
public func asControlEvent() -> ControlEvent<Element> {
self
}
}