You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

42 lines
1.2 KiB

//
// once.swift
// RxSwiftExt
//
// Created by Florent Pillet on 12/04/16.
// Copyright © 2016 RxSwift Community. All rights reserved.
//
import Foundation
import RxSwift
extension Observable {
/**
Returns an observable sequence that contains a single element. This element will be delivered to the first observer
that subscribes. Further subscriptions to the same observable will get an empty sequence.
In most cases, you want to use `Observable.just()`. This one is really for specific cases where you need to guarantee
unique delivery of a value.
- seealso: [just operator on reactivex.io](http://reactivex.io/documentation/operators/just.html)
- parameter element: Single element in the resulting observable sequence.
- returns: An observable sequence containing the single specified element delivered once.
*/
public static func once(_ element: Element) -> Observable<Element> {
let lock = NSRecursiveLock()
var isDelivered = false
return create { observer in
lock.lock()
if !isDelivered {
observer.onNext(element)
}
isDelivered = true
lock.unlock()
observer.onCompleted()
return Disposables.create()
}
}
}