Last active
July 10, 2018 01:36
-
-
Save operando/2add7bbad535bc30f7340bf8c04660d7 to your computer and use it in GitHub Desktop.
RxSwift Variable like RxJava Variable - https://github.com/ReactiveX/RxSwift/blob/master/RxSwift/Subjects/Variable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rx.playground; | |
import rx.Observable; | |
import rx.subjects.BehaviorSubject; | |
import rx.subjects.SerializedSubject; | |
public class Variable<T> { | |
private T value; | |
private final SerializedSubject<T, T> serializedSubject; | |
public Variable(T value) { | |
this.value = value; | |
serializedSubject = new SerializedSubject<>(BehaviorSubject.create(value)); | |
} | |
public synchronized T get() { | |
return value; | |
} | |
public synchronized void set(T value) { | |
this.value = value; | |
serializedSubject.onNext(this.value); | |
} | |
public Observable<T> asObservable() { | |
return serializedSubject.asObservable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment