123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace UniRx.Operators
- {
- public delegate TR ZipLatestFunc<T1, T2, T3, TR>(T1 arg1, T2 arg2, T3 arg3);
- public delegate TR ZipLatestFunc<T1, T2, T3, T4, TR>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
- public delegate TR ZipLatestFunc<T1, T2, T3, T4, T5, TR>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
- public delegate TR ZipLatestFunc<T1, T2, T3, T4, T5, T6, TR>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);
- public delegate TR ZipLatestFunc<T1, T2, T3, T4, T5, T6, T7, TR>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);
- // binary
- internal class ZipLatestObservable<TLeft, TRight, TResult> : OperatorObservableBase<TResult>
- {
- readonly IObservable<TLeft> left;
- readonly IObservable<TRight> right;
- readonly Func<TLeft, TRight, TResult> selector;
- public ZipLatestObservable(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector)
- : base(left.IsRequiredSubscribeOnCurrentThread() || right.IsRequiredSubscribeOnCurrentThread())
- {
- this.left = left;
- this.right = right;
- this.selector = selector;
- }
- protected override IDisposable SubscribeCore(IObserver<TResult> observer, IDisposable cancel)
- {
- return new ZipLatest(this, observer, cancel).Run();
- }
- class ZipLatest : OperatorObserverBase<TResult, TResult>
- {
- readonly ZipLatestObservable<TLeft, TRight, TResult> parent;
- readonly object gate = new object();
- TLeft leftValue = default(TLeft);
- bool leftStarted = false;
- bool leftCompleted = false;
- TRight rightValue = default(TRight);
- bool rightStarted = false;
- bool rightCompleted = false;
- public ZipLatest(ZipLatestObservable<TLeft, TRight, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- var l = parent.left.Subscribe(new LeftObserver(this));
- var r = parent.right.Subscribe(new RightObserver(this));
- return StableCompositeDisposable.Create(l, r);
- }
- // publish in lock
- public void Publish()
- {
- if ((leftCompleted && !leftStarted) || (rightCompleted && !rightStarted))
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- return;
- }
- else if (!(leftStarted && rightStarted))
- {
- return;
- }
- TResult v;
- try
- {
- v = parent.selector(leftValue, rightValue);
- }
- catch (Exception ex)
- {
- try { observer.OnError(ex); }
- finally { Dispose(); }
- return;
- }
- OnNext(v);
- leftStarted = false;
- rightStarted = false;
- if (leftCompleted || rightCompleted)
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- return;
- }
- }
- public override void OnNext(TResult value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- class LeftObserver : IObserver<TLeft>
- {
- readonly ZipLatest parent;
- public LeftObserver(ZipLatest parent)
- {
- this.parent = parent;
- }
- public void OnNext(TLeft value)
- {
- lock (parent.gate)
- {
- parent.leftStarted = true;
- parent.leftValue = value;
- parent.Publish();
- }
- }
- public void OnError(Exception error)
- {
- lock (parent.gate)
- {
- parent.OnError(error);
- }
- }
- public void OnCompleted()
- {
- lock (parent.gate)
- {
- parent.leftCompleted = true;
- if (parent.rightCompleted) parent.OnCompleted();
- }
- }
- }
- class RightObserver : IObserver<TRight>
- {
- readonly ZipLatest parent;
- public RightObserver(ZipLatest parent)
- {
- this.parent = parent;
- }
- public void OnNext(TRight value)
- {
- lock (parent.gate)
- {
- parent.rightStarted = true;
- parent.rightValue = value;
- parent.Publish();
- }
- }
- public void OnError(Exception error)
- {
- lock (parent.gate)
- {
- parent.OnError(error);
- }
- }
- public void OnCompleted()
- {
- lock (parent.gate)
- {
- parent.rightCompleted = true;
- if (parent.leftCompleted) parent.OnCompleted();
- }
- }
- }
- }
- }
- // array
- internal class ZipLatestObservable<T> : OperatorObservableBase<IList<T>>
- {
- readonly IObservable<T>[] sources;
- public ZipLatestObservable(IObservable<T>[] sources)
- : base(true)
- {
- this.sources = sources;
- }
- protected override IDisposable SubscribeCore(IObserver<IList<T>> observer, IDisposable cancel)
- {
- return new ZipLatest(this, observer, cancel).Run();
- }
- class ZipLatest : OperatorObserverBase<IList<T>, IList<T>>
- {
- readonly ZipLatestObservable<T> parent;
- readonly object gate = new object();
- int length;
- T[] values;
- bool[] isStarted;
- bool[] isCompleted;
- public ZipLatest(ZipLatestObservable<T> parent, IObserver<IList<T>> observer, IDisposable cancel) : base(observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- length = parent.sources.Length;
- values = new T[length];
- isStarted = new bool[length];
- isCompleted = new bool[length];
- var disposables = new IDisposable[length];
- for (int i = 0; i < length; i++)
- {
- var source = parent.sources[i];
- disposables[i] = source.Subscribe(new ZipLatestObserver(this, i));
- }
- return StableCompositeDisposable.CreateUnsafe(disposables);
- }
- // publish is in the lock
- void Publish(int index)
- {
- isStarted[index] = true;
- var hasOnCompleted = false;
- var allValueStarted = true;
- for (int i = 0; i < length; i++)
- {
- if (!isStarted[i])
- {
- allValueStarted = false;
- break;
- }
- if (i == index) continue;
- if (isCompleted[i]) hasOnCompleted = true;
- }
- if (allValueStarted)
- {
- OnNext(new List<T>(values));
- if (hasOnCompleted)
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- return;
- }
- else
- {
- Array.Clear(isStarted, 0, length); // reset
- return;
- }
- }
- else
- {
- for (int i = 0; i < length; i++)
- {
- if (i == index) continue;
- if (isCompleted[i] && !isStarted[i])
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- return;
- }
- }
- }
- }
- public override void OnNext(IList<T> value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- class ZipLatestObserver : IObserver<T>
- {
- readonly ZipLatest parent;
- readonly int index;
- public ZipLatestObserver(ZipLatest parent, int index)
- {
- this.parent = parent;
- this.index = index;
- }
- public void OnNext(T value)
- {
- lock (parent.gate)
- {
- parent.values[index] = value;
- parent.Publish(index);
- }
- }
- public void OnError(Exception ex)
- {
- lock (parent.gate)
- {
- parent.OnError(ex);
- }
- }
- public void OnCompleted()
- {
- lock (parent.gate)
- {
- parent.isCompleted[index] = true;
- var allTrue = true;
- for (int i = 0; i < parent.length; i++)
- {
- if (!parent.isCompleted[i])
- {
- allTrue = false;
- break;
- }
- }
- if (allTrue)
- {
- parent.OnCompleted();
- }
- }
- }
- }
- }
- }
- // generated from UniRx.Console.ZipLatestGenerator.tt
- #region NTH
- internal class ZipLatestObservable<T1, T2, T3, TR> : OperatorObservableBase<TR>
- {
- IObservable<T1> source1;
- IObservable<T2> source2;
- IObservable<T3> source3;
- ZipLatestFunc<T1, T2, T3, TR> resultSelector;
- public ZipLatestObservable(
- IObservable<T1> source1,
- IObservable<T2> source2,
- IObservable<T3> source3,
- ZipLatestFunc<T1, T2, T3, TR> resultSelector)
- : base(
- source1.IsRequiredSubscribeOnCurrentThread() ||
- source2.IsRequiredSubscribeOnCurrentThread() ||
- source3.IsRequiredSubscribeOnCurrentThread() ||
- false)
- {
- this.source1 = source1;
- this.source2 = source2;
- this.source3 = source3;
- this.resultSelector = resultSelector;
- }
- protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
- {
- return new ZipLatest(3, this, observer, cancel).Run();
- }
- class ZipLatest : NthZipLatestObserverBase<TR>
- {
- readonly ZipLatestObservable<T1, T2, T3, TR> parent;
- readonly object gate = new object();
- ZipLatestObserver<T1> c1;
- ZipLatestObserver<T2> c2;
- ZipLatestObserver<T3> c3;
- public ZipLatest(int length, ZipLatestObservable<T1, T2, T3, TR> parent, IObserver<TR> observer, IDisposable cancel)
- : base(length, observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- c1 = new ZipLatestObserver<T1>(gate, this, 0);
- c2 = new ZipLatestObserver<T2>(gate, this, 1);
- c3 = new ZipLatestObserver<T3>(gate, this, 2);
- var s1 = parent.source1.Subscribe(c1);
- var s2 = parent.source2.Subscribe(c2);
- var s3 = parent.source3.Subscribe(c3);
- return StableCompositeDisposable.Create(s1, s2, s3);
- }
- public override TR GetResult()
- {
- return parent.resultSelector(c1.Value, c2.Value, c3.Value);
- }
- public override void OnNext(TR value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- internal class ZipLatestObservable<T1, T2, T3, T4, TR> : OperatorObservableBase<TR>
- {
- IObservable<T1> source1;
- IObservable<T2> source2;
- IObservable<T3> source3;
- IObservable<T4> source4;
- ZipLatestFunc<T1, T2, T3, T4, TR> resultSelector;
- public ZipLatestObservable(
- IObservable<T1> source1,
- IObservable<T2> source2,
- IObservable<T3> source3,
- IObservable<T4> source4,
- ZipLatestFunc<T1, T2, T3, T4, TR> resultSelector)
- : base(
- source1.IsRequiredSubscribeOnCurrentThread() ||
- source2.IsRequiredSubscribeOnCurrentThread() ||
- source3.IsRequiredSubscribeOnCurrentThread() ||
- source4.IsRequiredSubscribeOnCurrentThread() ||
- false)
- {
- this.source1 = source1;
- this.source2 = source2;
- this.source3 = source3;
- this.source4 = source4;
- this.resultSelector = resultSelector;
- }
- protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
- {
- return new ZipLatest(4, this, observer, cancel).Run();
- }
- class ZipLatest : NthZipLatestObserverBase<TR>
- {
- readonly ZipLatestObservable<T1, T2, T3, T4, TR> parent;
- readonly object gate = new object();
- ZipLatestObserver<T1> c1;
- ZipLatestObserver<T2> c2;
- ZipLatestObserver<T3> c3;
- ZipLatestObserver<T4> c4;
- public ZipLatest(int length, ZipLatestObservable<T1, T2, T3, T4, TR> parent, IObserver<TR> observer, IDisposable cancel)
- : base(length, observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- c1 = new ZipLatestObserver<T1>(gate, this, 0);
- c2 = new ZipLatestObserver<T2>(gate, this, 1);
- c3 = new ZipLatestObserver<T3>(gate, this, 2);
- c4 = new ZipLatestObserver<T4>(gate, this, 3);
- var s1 = parent.source1.Subscribe(c1);
- var s2 = parent.source2.Subscribe(c2);
- var s3 = parent.source3.Subscribe(c3);
- var s4 = parent.source4.Subscribe(c4);
- return StableCompositeDisposable.Create(s1, s2, s3, s4);
- }
- public override TR GetResult()
- {
- return parent.resultSelector(c1.Value, c2.Value, c3.Value, c4.Value);
- }
- public override void OnNext(TR value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- internal class ZipLatestObservable<T1, T2, T3, T4, T5, TR> : OperatorObservableBase<TR>
- {
- IObservable<T1> source1;
- IObservable<T2> source2;
- IObservable<T3> source3;
- IObservable<T4> source4;
- IObservable<T5> source5;
- ZipLatestFunc<T1, T2, T3, T4, T5, TR> resultSelector;
- public ZipLatestObservable(
- IObservable<T1> source1,
- IObservable<T2> source2,
- IObservable<T3> source3,
- IObservable<T4> source4,
- IObservable<T5> source5,
- ZipLatestFunc<T1, T2, T3, T4, T5, TR> resultSelector)
- : base(
- source1.IsRequiredSubscribeOnCurrentThread() ||
- source2.IsRequiredSubscribeOnCurrentThread() ||
- source3.IsRequiredSubscribeOnCurrentThread() ||
- source4.IsRequiredSubscribeOnCurrentThread() ||
- source5.IsRequiredSubscribeOnCurrentThread() ||
- false)
- {
- this.source1 = source1;
- this.source2 = source2;
- this.source3 = source3;
- this.source4 = source4;
- this.source5 = source5;
- this.resultSelector = resultSelector;
- }
- protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
- {
- return new ZipLatest(5, this, observer, cancel).Run();
- }
- class ZipLatest : NthZipLatestObserverBase<TR>
- {
- readonly ZipLatestObservable<T1, T2, T3, T4, T5, TR> parent;
- readonly object gate = new object();
- ZipLatestObserver<T1> c1;
- ZipLatestObserver<T2> c2;
- ZipLatestObserver<T3> c3;
- ZipLatestObserver<T4> c4;
- ZipLatestObserver<T5> c5;
- public ZipLatest(int length, ZipLatestObservable<T1, T2, T3, T4, T5, TR> parent, IObserver<TR> observer, IDisposable cancel)
- : base(length, observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- c1 = new ZipLatestObserver<T1>(gate, this, 0);
- c2 = new ZipLatestObserver<T2>(gate, this, 1);
- c3 = new ZipLatestObserver<T3>(gate, this, 2);
- c4 = new ZipLatestObserver<T4>(gate, this, 3);
- c5 = new ZipLatestObserver<T5>(gate, this, 4);
- var s1 = parent.source1.Subscribe(c1);
- var s2 = parent.source2.Subscribe(c2);
- var s3 = parent.source3.Subscribe(c3);
- var s4 = parent.source4.Subscribe(c4);
- var s5 = parent.source5.Subscribe(c5);
- return StableCompositeDisposable.Create(s1, s2, s3, s4, s5);
- }
- public override TR GetResult()
- {
- return parent.resultSelector(c1.Value, c2.Value, c3.Value, c4.Value, c5.Value);
- }
- public override void OnNext(TR value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- internal class ZipLatestObservable<T1, T2, T3, T4, T5, T6, TR> : OperatorObservableBase<TR>
- {
- IObservable<T1> source1;
- IObservable<T2> source2;
- IObservable<T3> source3;
- IObservable<T4> source4;
- IObservable<T5> source5;
- IObservable<T6> source6;
- ZipLatestFunc<T1, T2, T3, T4, T5, T6, TR> resultSelector;
- public ZipLatestObservable(
- IObservable<T1> source1,
- IObservable<T2> source2,
- IObservable<T3> source3,
- IObservable<T4> source4,
- IObservable<T5> source5,
- IObservable<T6> source6,
- ZipLatestFunc<T1, T2, T3, T4, T5, T6, TR> resultSelector)
- : base(
- source1.IsRequiredSubscribeOnCurrentThread() ||
- source2.IsRequiredSubscribeOnCurrentThread() ||
- source3.IsRequiredSubscribeOnCurrentThread() ||
- source4.IsRequiredSubscribeOnCurrentThread() ||
- source5.IsRequiredSubscribeOnCurrentThread() ||
- source6.IsRequiredSubscribeOnCurrentThread() ||
- false)
- {
- this.source1 = source1;
- this.source2 = source2;
- this.source3 = source3;
- this.source4 = source4;
- this.source5 = source5;
- this.source6 = source6;
- this.resultSelector = resultSelector;
- }
- protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
- {
- return new ZipLatest(6, this, observer, cancel).Run();
- }
- class ZipLatest : NthZipLatestObserverBase<TR>
- {
- readonly ZipLatestObservable<T1, T2, T3, T4, T5, T6, TR> parent;
- readonly object gate = new object();
- ZipLatestObserver<T1> c1;
- ZipLatestObserver<T2> c2;
- ZipLatestObserver<T3> c3;
- ZipLatestObserver<T4> c4;
- ZipLatestObserver<T5> c5;
- ZipLatestObserver<T6> c6;
- public ZipLatest(int length, ZipLatestObservable<T1, T2, T3, T4, T5, T6, TR> parent, IObserver<TR> observer, IDisposable cancel)
- : base(length, observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- c1 = new ZipLatestObserver<T1>(gate, this, 0);
- c2 = new ZipLatestObserver<T2>(gate, this, 1);
- c3 = new ZipLatestObserver<T3>(gate, this, 2);
- c4 = new ZipLatestObserver<T4>(gate, this, 3);
- c5 = new ZipLatestObserver<T5>(gate, this, 4);
- c6 = new ZipLatestObserver<T6>(gate, this, 5);
- var s1 = parent.source1.Subscribe(c1);
- var s2 = parent.source2.Subscribe(c2);
- var s3 = parent.source3.Subscribe(c3);
- var s4 = parent.source4.Subscribe(c4);
- var s5 = parent.source5.Subscribe(c5);
- var s6 = parent.source6.Subscribe(c6);
- return StableCompositeDisposable.Create(s1, s2, s3, s4, s5, s6);
- }
- public override TR GetResult()
- {
- return parent.resultSelector(c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value);
- }
- public override void OnNext(TR value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- internal class ZipLatestObservable<T1, T2, T3, T4, T5, T6, T7, TR> : OperatorObservableBase<TR>
- {
- IObservable<T1> source1;
- IObservable<T2> source2;
- IObservable<T3> source3;
- IObservable<T4> source4;
- IObservable<T5> source5;
- IObservable<T6> source6;
- IObservable<T7> source7;
- ZipLatestFunc<T1, T2, T3, T4, T5, T6, T7, TR> resultSelector;
- public ZipLatestObservable(
- IObservable<T1> source1,
- IObservable<T2> source2,
- IObservable<T3> source3,
- IObservable<T4> source4,
- IObservable<T5> source5,
- IObservable<T6> source6,
- IObservable<T7> source7,
- ZipLatestFunc<T1, T2, T3, T4, T5, T6, T7, TR> resultSelector)
- : base(
- source1.IsRequiredSubscribeOnCurrentThread() ||
- source2.IsRequiredSubscribeOnCurrentThread() ||
- source3.IsRequiredSubscribeOnCurrentThread() ||
- source4.IsRequiredSubscribeOnCurrentThread() ||
- source5.IsRequiredSubscribeOnCurrentThread() ||
- source6.IsRequiredSubscribeOnCurrentThread() ||
- source7.IsRequiredSubscribeOnCurrentThread() ||
- false)
- {
- this.source1 = source1;
- this.source2 = source2;
- this.source3 = source3;
- this.source4 = source4;
- this.source5 = source5;
- this.source6 = source6;
- this.source7 = source7;
- this.resultSelector = resultSelector;
- }
- protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
- {
- return new ZipLatest(7, this, observer, cancel).Run();
- }
- class ZipLatest : NthZipLatestObserverBase<TR>
- {
- readonly ZipLatestObservable<T1, T2, T3, T4, T5, T6, T7, TR> parent;
- readonly object gate = new object();
- ZipLatestObserver<T1> c1;
- ZipLatestObserver<T2> c2;
- ZipLatestObserver<T3> c3;
- ZipLatestObserver<T4> c4;
- ZipLatestObserver<T5> c5;
- ZipLatestObserver<T6> c6;
- ZipLatestObserver<T7> c7;
- public ZipLatest(int length, ZipLatestObservable<T1, T2, T3, T4, T5, T6, T7, TR> parent, IObserver<TR> observer, IDisposable cancel)
- : base(length, observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- c1 = new ZipLatestObserver<T1>(gate, this, 0);
- c2 = new ZipLatestObserver<T2>(gate, this, 1);
- c3 = new ZipLatestObserver<T3>(gate, this, 2);
- c4 = new ZipLatestObserver<T4>(gate, this, 3);
- c5 = new ZipLatestObserver<T5>(gate, this, 4);
- c6 = new ZipLatestObserver<T6>(gate, this, 5);
- c7 = new ZipLatestObserver<T7>(gate, this, 6);
- var s1 = parent.source1.Subscribe(c1);
- var s2 = parent.source2.Subscribe(c2);
- var s3 = parent.source3.Subscribe(c3);
- var s4 = parent.source4.Subscribe(c4);
- var s5 = parent.source5.Subscribe(c5);
- var s6 = parent.source6.Subscribe(c6);
- var s7 = parent.source7.Subscribe(c7);
- return StableCompositeDisposable.Create(s1, s2, s3, s4, s5, s6, s7);
- }
- public override TR GetResult()
- {
- return parent.resultSelector(c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value);
- }
- public override void OnNext(TR value)
- {
- base.observer.OnNext(value);
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- #endregion
- // Nth infrastructure
- internal interface IZipLatestObservable
- {
- void Publish(int index);
- void Fail(Exception error);
- void Done(int index);
- }
- internal abstract class NthZipLatestObserverBase<T> : OperatorObserverBase<T, T>, IZipLatestObservable
- {
- readonly int length;
- readonly bool[] isStarted;
- readonly bool[] isCompleted;
- public NthZipLatestObserverBase(int length, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
- {
- this.length = length;
- this.isStarted = new bool[length];
- this.isCompleted = new bool[length];
- }
- public abstract T GetResult();
- // operators in lock
- public void Publish(int index)
- {
- isStarted[index] = true;
- var hasOnCompleted = false;
- var allValueStarted = true;
- for (int i = 0; i < length; i++)
- {
- if (!isStarted[i])
- {
- allValueStarted = false;
- break;
- }
- if (i == index) continue;
- if (isCompleted[i]) hasOnCompleted = true;
- }
- if (allValueStarted)
- {
- var result = default(T);
- try
- {
- result = GetResult();
- }
- catch (Exception ex)
- {
- try { observer.OnError(ex); }
- finally { Dispose(); }
- return;
- }
- OnNext(result);
- if (hasOnCompleted)
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- return;
- }
- else
- {
- Array.Clear(isStarted, 0, length); // reset
- return;
- }
- }
- else
- {
- for (int i = 0; i < length; i++)
- {
- if (i == index) continue;
- if (isCompleted[i] && !isStarted[i])
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- return;
- }
- }
- }
- }
- public void Done(int index)
- {
- isCompleted[index] = true;
- var allTrue = true;
- for (int i = 0; i < length; i++)
- {
- if (!isCompleted[i])
- {
- allTrue = false;
- break;
- }
- }
- if (allTrue)
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- public void Fail(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- }
- // Nth
- internal class ZipLatestObserver<T> : IObserver<T>
- {
- readonly object gate;
- readonly IZipLatestObservable parent;
- readonly int index;
- T value;
- public T Value { get { return value; } }
- public ZipLatestObserver(object gate, IZipLatestObservable parent, int index)
- {
- this.gate = gate;
- this.parent = parent;
- this.index = index;
- }
- public void OnNext(T value)
- {
- lock (gate)
- {
- this.value = value;
- parent.Publish(index);
- }
- }
- public void OnError(Exception error)
- {
- lock (gate)
- {
- parent.Fail(error);
- }
- }
- public void OnCompleted()
- {
- lock (gate)
- {
- parent.Done(index);
- }
- }
- }
- }
|