0001    //
0002    //  Variable.swift
0003    //  Rx
0004    //
0005    //  Created by Krunoslav Zaher on 3/28/15.
0006    //  Copyright © 2015 Krunoslav Zaher. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    
0011    /**
0012    Variable is a wrapper for `BehaviorSubject`.
0013    
0014    Unlike `BehaviorSubject` it can't terminate with error, and when variable is deallocated
0015     it will complete it's observable sequence (`asObservable`).
0016    */
0017    public class Variable<Element
Variable.swift:19
    public typealias E = Element
Variable.swift:21
    private let _subject: BehaviorSubject<Element>
Variable.swift:54
    public init(_ value: Element) {
> { 0018 0019 public typealias E
Variable.swift:26
    private var _value: E
Variable.swift:35
    public var value: E {
Variable.swift:62
    public func asObservable() -> Observable<E> {
= Element 0020 0021 private let _subject
Variable.swift:45
            _subject.on(.Next(newValue))
Variable.swift:56
        _subject = BehaviorSubject(value: value)
Variable.swift:63
        return _subject
Variable.swift:67
        _subject.on(.Completed)
: BehaviorSubject<Element> 0022 0023 private var _lock
Variable.swift:37
            _lock.lock(); defer { _lock.unlock() }
Variable.swift:37
            _lock.lock(); defer { _lock.unlock() }
Variable.swift:41
            _lock.lock()
Variable.swift:43
            _lock.unlock()
= SpinLock() 0024 0025 // state 0026 private var _value
Variable.swift:38
            return _value
Variable.swift:42
            _value = newValue
Variable.swift:55
        _value = value
: E 0027 0028 /** 0029 Gets or sets current value of variable. 0030 0031 If case new value is set, all observers are notified of that change. 0032 0033 Even is case equal value is set, observers will still be notified. 0034 */ 0035 public var value: E { 0036 get { 0037 _lock.lock(); defer { _lock.unlock() } 0038 return _value 0039 } 0040 set(newValue) { 0041 _lock.lock() 0042 _value = newValue 0043 _lock.unlock() 0044 0045 _subject.on(.Next(newValue)) 0046 } 0047 } 0048 0049 /** 0050 Initializes variable with initial value. 0051 0052 - parameter value: Initial variable value. 0053 */ 0054 public init(_ value: Element) { 0055 _value = value 0056 _subject = BehaviorSubject(value: value) 0057 } 0058 0059 /** 0060 - returns: Canonical interface for push style sequence 0061 */ 0062 public func asObservable() -> Observable<E> { 0063 return _subject 0064 } 0065 0066 deinit { 0067 _subject.on(.Completed) 0068 } 0069 }