0001    //
0002    //  Observable+Creation.swift
0003    //  Rx
0004    //
0005    //  Created by Krunoslav Zaher on 3/21/15.
0006    //  Copyright © 2015 Krunoslav Zaher. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    
0011    extension Observable {
0012        // MARK: create
0013    
0014        /**
0015        Creates an observable sequence from a specified subscribe method implementation.
0016    
0017        - seealso: [create operator on reactivex.io](http://reactivex.io/documentation/operators/create.html)
0018    
0019        - parameter subscribe: Implementation of the resulting observable sequence's `subscribe` method.
0020        - returns: The observable sequence with the specified implementation for the `subscribe` method.
0021        */
0022        @warn_unused_result(message="http://git.io/rxs.uo")
0023        public static func create
ObservableType.swift:55
        return Observable.create(self.subscribe)
(subscribe: (AnyObserver<E>) -> Disposable) -> Observable<E> { 0024 return AnonymousObservable(subscribe) 0025 } 0026 0027 // MARK: empty 0028 0029 /** 0030 Returns an empty observable sequence, using the specified scheduler to send out the single `Completed` message. 0031 0032 - seealso: [empty operator on reactivex.io](http://reactivex.io/documentation/operators/empty-never-throw.html) 0033 0034 - returns: An observable sequence with no elements. 0035 */ 0036 @warn_unused_result(message="http://git.io/rxs.uo") 0037 public static func empty
Observable+StandardSequenceOperators.swift:81
            return Observable.empty()
() -> Observable<E> { 0038 return Empty<E>() 0039 } 0040 0041 // MARK: never 0042 0043 /** 0044 Returns a non-terminating observable sequence, which can be used to denote an infinite duration. 0045 0046 - seealso: [never operator on reactivex.io](http://reactivex.io/documentation/operators/empty-never-throw.html) 0047 0048 - returns: An observable sequence whose observers will never get called. 0049 */ 0050 @warn_unused_result(message="http://git.io/rxs.uo") 0051 public static func never
Observable+Multiple.swift:296
        return self.reduce(Observable.never()) { a, o in
() -> Observable<E> { 0052 return Never() 0053 } 0054 0055 // MARK: just 0056 0057 /** 0058 Returns an observable sequence that contains a single element. 0059 0060 - seealso: [just operator on reactivex.io](http://reactivex.io/documentation/operators/just.html) 0061 0062 - parameter element: Single element in the resulting observable sequence. 0063 - returns: An observable sequence containing the single specified element. 0064 */ 0065 @warn_unused_result(message="http://git.io/rxs.uo") 0066 public static func just
Observable+Multiple.swift:206
        return Catch(source: asObservable(), handler: { _ in Observable.just(element) })
(element: E) -> Observable<E> { 0067 return Just(element: element) 0068 } 0069 0070 /** 0071 Returns an observable sequence that contains a single element. 0072 0073 - seealso: [just operator on reactivex.io](http://reactivex.io/documentation/operators/just.html) 0074 0075 - parameter element: Single element in the resulting observable sequence. 0076 - parameter: Scheduler to send the single element on. 0077 - returns: An observable sequence containing the single specified element. 0078 */ 0079 @warn_unused_result(message="http://git.io/rxs.uo") 0080 public static func just(element: E, scheduler: ImmediateSchedulerType) -> Observable<E> { 0081 return JustScheduled(element: element, scheduler: scheduler) 0082 } 0083 0084 // MARK: fail 0085 0086 /** 0087 Returns an observable sequence that terminates with an `error`. 0088 0089 - seealso: [throw operator on reactivex.io](http://reactivex.io/documentation/operators/empty-never-throw.html) 0090 0091 - returns: The observable sequence that terminates with specified error. 0092 */ 0093 @warn_unused_result(message="http://git.io/rxs.uo") 0094 public static func error
Observable+Time.swift:256
            return Timeout(source: self.asObservable(), dueTime: dueTime, other: Observable.error(RxError.Timeout), scheduler: scheduler)
Using.swift:37
                Observable.error(error).subscribe(self),
(error: ErrorType) -> Observable<E> { 0095 return Error(error: error) 0096 } 0097 0098 // MARK: of 0099 0100 /** 0101 This method creates a new Observable instance with a variable number of elements. 0102 0103 - seealso: [from operator on reactivex.io](http://reactivex.io/documentation/operators/from.html) 0104 0105 - parameter elements: Elements to generate. 0106 - parameter scheduler: Scheduler to send elements on. If `nil`, elements are sent immediatelly on subscription. 0107 - returns: The observable sequence whose elements are pulled from the given arguments. 0108 */ 0109 @warn_unused_result(message="http://git.io/rxs.uo") 0110 public static func of(elements: E ..., scheduler: ImmediateSchedulerType? = nil) -> Observable<E> { 0111 return Sequence(elements: elements, scheduler: scheduler) 0112 } 0113 0114 // MARK: defer 0115 0116 /** 0117 Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes. 0118 0119 - seealso: [defer operator on reactivex.io](http://reactivex.io/documentation/operators/defer.html) 0120 0121 - parameter observableFactory: Observable factory function to invoke for each observer that subscribes to the resulting sequence. 0122 - returns: An observable sequence whose observers trigger an invocation of the given observable factory function. 0123 */ 0124 @warn_unused_result(message="http://git.io/rxs.uo") 0125 public static func deferred(observableFactory: () throws -> Observable<E>) 0126 -> Observable<E> { 0127 return Deferred(observableFactory: observableFactory) 0128 } 0129 0130 /** 0131 Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler 0132 to run the loop send out observer messages. 0133 0134 - seealso: [create operator on reactivex.io](http://reactivex.io/documentation/operators/create.html) 0135 0136 - parameter initialState: Initial state. 0137 - parameter condition: Condition to terminate generation (upon returning `false`). 0138 - parameter iterate: Iteration step function. 0139 - parameter scheduler: Scheduler on which to run the generator loop. 0140 - returns: The generated sequence. 0141 */ 0142 @warn_unused_result(message="http://git.io/rxs.uo") 0143 public static func generate(initialState initialState: E, condition: E throws -> Bool, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance, iterate: E throws -> E) -> Observable<E> { 0144 return Generate(initialState: initialState, condition: condition, iterate: iterate, resultSelector: { $0 }, scheduler: scheduler) 0145 } 0146 0147 /** 0148 Generates an observable sequence that repeats the given element infinitely, using the specified scheduler to send out observer messages. 0149 0150 - seealso: [repeat operator on reactivex.io](http://reactivex.io/documentation/operators/repeat.html) 0151 0152 - parameter element: Element to repeat. 0153 - parameter scheduler: Scheduler to run the producer loop on. 0154 - returns: An observable sequence that repeats the given element infinitely. 0155 */ 0156 @warn_unused_result(message="http://git.io/rxs.uo") 0157 public static func repeatElement(element: E, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance) -> Observable<E> { 0158 return RepeatElement(element: element, scheduler: scheduler) 0159 } 0160 0161 /** 0162 Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime. 0163 0164 - seealso: [using operator on reactivex.io](http://reactivex.io/documentation/operators/using.html) 0165 0166 - parameter resourceFactory: Factory function to obtain a resource object. 0167 - parameter observableFactory: Factory function to obtain an observable sequence that depends on the obtained resource. 0168 - returns: An observable sequence whose lifetime controls the lifetime of the dependent resource object. 0169 */ 0170 @warn_unused_result(message="http://git.io/rxs.uo") 0171 public static func using<R: Disposable>(resourceFactory: () throws -> R, observableFactory: R throws -> Observable<E>) -> Observable<E> { 0172 return Using(resourceFactory: resourceFactory, observableFactory: observableFactory) 0173 } 0174 } 0175 0176 extension Observable where Element : SignedIntegerType { 0177 /** 0178 Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to generate and send out observer messages. 0179 0180 - seealso: [range operator on reactivex.io](http://reactivex.io/documentation/operators/range.html) 0181 0182 - parameter start: The value of the first integer in the sequence. 0183 - parameter count: The number of sequential integers to generate. 0184 - parameter scheduler: Scheduler to run the generator loop on. 0185 - returns: An observable sequence that contains a range of sequential integral numbers. 0186 */ 0187 @warn_unused_result(message="http://git.io/rxs.uo") 0188 public static func range(start start: E, count: E, scheduler: ImmediateSchedulerType = CurrentThreadScheduler.instance) -> Observable<E> { 0189 return RangeProducer<E>(start: start, count: count, scheduler: scheduler) 0190 } 0191 } 0192 0193 extension SequenceType { 0194 /** 0195 Converts a sequence to an observable sequence. 0196 0197 - seealso: [from operator on reactivex.io](http://reactivex.io/documentation/operators/from.html) 0198 0199 - returns: The observable sequence whose elements are pulled from the given enumerable sequence. 0200 */ 0201 @warn_unused_result(message="http://git.io/rxs.uo") 0202 public func toObservable(scheduler: ImmediateSchedulerType? = nil) -> Observable<Generator.Element> { 0203 return Sequence(elements: Array(self), scheduler: scheduler) 0204 } 0205 } 0206 0207 extension Array { 0208 /** 0209 Converts a sequence to an observable sequence. 0210 0211 - seealso: [from operator on reactivex.io](http://reactivex.io/documentation/operators/from.html) 0212 0213 - returns: The observable sequence whose elements are pulled from the given enumerable sequence. 0214 */ 0215 @warn_unused_result(message="http://git.io/rxs.uo") 0216 public func toObservable(scheduler: ImmediateSchedulerType? = nil) -> Observable<Generator.Element> { 0217 return Sequence(elements: self, scheduler: scheduler) 0218 } 0219 }