ruk·si

UniRx
Create Operators

Updated at 2017-07-27 19:41

Observable.Empty Create a stream that completes.

var source = Observable.Empty<bool>();
source.Subscribe(CreateShoutObserver<bool>());
// OnCompleted
     vvv empty() vvv
S.1: -|------------>

Observable.Never Create a stream that never emits or notifies.

var source = Observable.Never<bool>();
source.Subscribe(CreateShoutObserver<bool>());
// * never prints anything *
     vvv never() vvv
S.1: -------------->

Observable.Throw Create a stream that notifies an error.

var source = Observable.Throw<bool>(new Exception("Boo!"));
source.Subscribe(CreateShoutObserver<bool>());
// OnError: System.Exception: Boo!
     vvv error() vvv
S.1: -X------------>

Observable.Return Create a stream that emits a single variable. Optimized for booleans so that it doesn't allocate memory.

var source = Observable.Return(True);
source.Subscribe(CreateShoutObserver<bool>());
// OnNext: True
// OnCompleted
     vvv return(a) vvv
S.1: -a-|------------>

Observable.Start Create a strean that executes a function on subscription and emits return of that function.

var source = Observable.Start(() =>
{
    Debug.Log("Started!");
    return true;
});
source.Subscribe(CreateShoutObserver<bool>());
source.Subscribe(CreateShoutObserver<bool>());
// Started!
// Started!
// OnNext: True
// OnNext: True
// OnCompleted
// OnCompleted
     vvv start(func()) vvv
S.1: -A-|---------------->

Observable.Range Creates a stream that generates Y integers starting from X.

var source = Observable.Range(5, 3);
source.Subscribe(CreateShoutObserver<int>());
// OnNext: 5
// OnNext: 6
// OnNext: 7
// OnCompleted
     vvv range(5, 3) vvv
S.1: -5--6--7--|------->

Observable.Interval Create a stream that emits integers spaced by a time interval.

var source = Observable.Interval(TimeSpan.FromMilliseconds(500));
source.Subscribe(CreateShoutObserver<long>());
// OnNext: 0
// OnNext: 1
// OnNext: 2
// ... until end of time
     vvvvvvvv interval(500) vvvvvvvv
S.1: -0--1--2--3--4--5--6--7--8--9->

Observable.Timer Create a stream that emits 0 after a given delay.

var source = Observable.Timer(TimeSpan.FromSeconds(1));
source.Subscribe(CreateShoutObserver<long>());
// OnNext: 0
// OnCompleted

// Given two timespans, the first is initial timer and second
// is the interval.
var infiniteSource = Observable.Timer(
    TimeSpan.FromSeconds(1),
    TimeSpan.FromSeconds(5)
);
infiniteSource.Subscribe(CreateShoutObserver<long>());
// OnNext: 0 (after 1s)
// OnNext: 1 (after 6s)
// OnNext: 2 (after 11s)
// ... until end of time
     vvvvvvvv timer(1) vvvvvvvv
S.1: ------------0--|----------->

Observable.Repeat Create a stream that repeatedly emits the supplied value.

var source = Observable.Repeat(100);
// source.Subscribe(CreateShoutObserver<int>());
// the above would create an infinite pool of generating "100" integers

// You can specify how many times it repeats.
var source = Observable.Repeat(100, 3);
source.Subscribe(CreateShoutObserver<int>());
// OnNext: 100
// OnNext: 100
// OnNext: 100
// OnCompleted

IObservable.RepeatUntilDestroy Create a stream that repeats the previous stream until the given component or game object is destroyed. There is also similar RepeatUntilDisable.

var go = new GameObject();

Observable
    .Return("Hello!")
    .RepeatUntilDestroy(go)
    .Subscribe(CreateShoutObserver<string>());

Observable
    .Timer(TimeSpan.FromMilliseconds(1))
    .Subscribe((_) => UnityEngine.Object.DestroyImmediate(go));

// OnNext: Hello! - usually has time to print it 2 times
// Without the Destroy, has time to print it 10 or so times
// when garbage collection destroys the game object.

Observable.Defer Create a separate stream when an observer subscribes.

var original = 5;

var source = Observable.Return(original);
var deferred = Observable.Defer(() => Observable.Return(original));

original = 10;

source.Subscribe((number) => Debug.Log("sourceOnNext: " + number));
deferred.Subscribe((number) => Debug.Log("deferredOnNext: " + number));

original = 15;

// srcOnNext: 5
// deferredOnNext: 10

Sources