UniRx 에셋 추가
This commit is contained in:
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class BatchFrameObservable<T> : OperatorObservableBase<IList<T>>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public BatchFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType)
|
||||
: base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<IList<T>> observer, IDisposable cancel)
|
||||
{
|
||||
return new BatchFrame(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class BatchFrame : OperatorObserverBase<T, IList<T>>
|
||||
{
|
||||
readonly BatchFrameObservable<T> parent;
|
||||
readonly object gate = new object();
|
||||
readonly BooleanDisposable cancellationToken = new BooleanDisposable();
|
||||
readonly System.Collections.IEnumerator timer;
|
||||
bool isRunning;
|
||||
bool isCompleted;
|
||||
List<T> list;
|
||||
|
||||
public BatchFrame(BatchFrameObservable<T> parent, IObserver<IList<T>> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.timer = new ReusableEnumerator(this);
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
list = new List<T>();
|
||||
var sourceSubscription = parent.source.Subscribe(this);
|
||||
return StableCompositeDisposable.Create(sourceSubscription, cancellationToken);
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
if (isCompleted) return;
|
||||
list.Add(value);
|
||||
if (!isRunning)
|
||||
{
|
||||
isRunning = true;
|
||||
timer.Reset(); // reuse
|
||||
|
||||
switch (parent.frameCountType)
|
||||
{
|
||||
case FrameCountType.Update:
|
||||
MainThreadDispatcher.StartUpdateMicroCoroutine(timer);
|
||||
break;
|
||||
case FrameCountType.FixedUpdate:
|
||||
MainThreadDispatcher.StartFixedUpdateMicroCoroutine(timer);
|
||||
break;
|
||||
case FrameCountType.EndOfFrame:
|
||||
MainThreadDispatcher.StartEndOfFrameMicroCoroutine(timer);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
try { observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
List<T> currentList;
|
||||
lock (gate)
|
||||
{
|
||||
isCompleted = true;
|
||||
currentList = list;
|
||||
}
|
||||
if (currentList.Count != 0)
|
||||
{
|
||||
observer.OnNext(currentList);
|
||||
}
|
||||
try { observer.OnCompleted(); } finally { Dispose(); }
|
||||
}
|
||||
|
||||
// reuse, no gc allocate
|
||||
class ReusableEnumerator : System.Collections.IEnumerator
|
||||
{
|
||||
readonly BatchFrame parent;
|
||||
int currentFrame;
|
||||
|
||||
public ReusableEnumerator(BatchFrame parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public object Current
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (parent.cancellationToken.IsDisposed) return false;
|
||||
|
||||
List<T> currentList;
|
||||
lock (parent.gate)
|
||||
{
|
||||
if (currentFrame++ == parent.parent.frameCount)
|
||||
{
|
||||
if (parent.isCompleted) return false;
|
||||
|
||||
currentList = parent.list;
|
||||
parent.list = new List<T>();
|
||||
parent.isRunning = false;
|
||||
|
||||
// exit lock
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
parent.observer.OnNext(currentList);
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
currentFrame = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class BatchFrameObservable : OperatorObservableBase<Unit>
|
||||
{
|
||||
readonly IObservable<Unit> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public BatchFrameObservable(IObservable<Unit> source, int frameCount, FrameCountType frameCountType)
|
||||
: base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
|
||||
{
|
||||
return new BatchFrame(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class BatchFrame : OperatorObserverBase<Unit, Unit>
|
||||
{
|
||||
readonly BatchFrameObservable parent;
|
||||
readonly object gate = new object();
|
||||
readonly BooleanDisposable cancellationToken = new BooleanDisposable();
|
||||
readonly System.Collections.IEnumerator timer;
|
||||
|
||||
bool isRunning;
|
||||
bool isCompleted;
|
||||
|
||||
public BatchFrame(BatchFrameObservable parent, IObserver<Unit> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.timer = new ReusableEnumerator(this);
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
var sourceSubscription = parent.source.Subscribe(this);
|
||||
return StableCompositeDisposable.Create(sourceSubscription, cancellationToken);
|
||||
}
|
||||
|
||||
public override void OnNext(Unit value)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
if (!isRunning)
|
||||
{
|
||||
isRunning = true;
|
||||
timer.Reset(); // reuse
|
||||
|
||||
switch (parent.frameCountType)
|
||||
{
|
||||
case FrameCountType.Update:
|
||||
MainThreadDispatcher.StartUpdateMicroCoroutine(timer);
|
||||
break;
|
||||
case FrameCountType.FixedUpdate:
|
||||
MainThreadDispatcher.StartFixedUpdateMicroCoroutine(timer);
|
||||
break;
|
||||
case FrameCountType.EndOfFrame:
|
||||
MainThreadDispatcher.StartEndOfFrameMicroCoroutine(timer);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
try { observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
bool running;
|
||||
lock (gate)
|
||||
{
|
||||
running = isRunning;
|
||||
isCompleted = true;
|
||||
}
|
||||
if (running)
|
||||
{
|
||||
observer.OnNext(Unit.Default);
|
||||
}
|
||||
try { observer.OnCompleted(); } finally { Dispose(); }
|
||||
}
|
||||
|
||||
// reuse, no gc allocate
|
||||
class ReusableEnumerator : System.Collections.IEnumerator
|
||||
{
|
||||
readonly BatchFrame parent;
|
||||
int currentFrame;
|
||||
|
||||
public ReusableEnumerator(BatchFrame parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public object Current
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (parent.cancellationToken.IsDisposed) return false;
|
||||
|
||||
lock (parent.gate)
|
||||
{
|
||||
if (currentFrame++ == parent.parent.frameCount)
|
||||
{
|
||||
if (parent.isCompleted) return false;
|
||||
parent.isRunning = false;
|
||||
|
||||
// exit lock
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
parent.observer.OnNext(Unit.Default);
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
currentFrame = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 74a2b6b8c63d1144f914c7f0d6719a36
|
||||
timeCreated: 1467771656
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,260 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class DelayFrameObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public DelayFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType)
|
||||
: base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
return new DelayFrame(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class DelayFrame : OperatorObserverBase<T, T>
|
||||
{
|
||||
readonly DelayFrameObservable<T> parent;
|
||||
readonly object gate = new object();
|
||||
readonly QueuePool pool = new QueuePool();
|
||||
int runningEnumeratorCount;
|
||||
bool readyDrainEnumerator;
|
||||
bool running;
|
||||
IDisposable sourceSubscription;
|
||||
Queue<T> currentQueueReference;
|
||||
bool calledCompleted;
|
||||
bool hasError;
|
||||
Exception error;
|
||||
BooleanDisposable cancelationToken;
|
||||
|
||||
public DelayFrame(DelayFrameObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
cancelationToken = new BooleanDisposable();
|
||||
|
||||
var _sourceSubscription = new SingleAssignmentDisposable();
|
||||
sourceSubscription = _sourceSubscription;
|
||||
_sourceSubscription.Disposable = parent.source.Subscribe(this);
|
||||
|
||||
return StableCompositeDisposable.Create(cancelationToken, sourceSubscription);
|
||||
}
|
||||
|
||||
IEnumerator DrainQueue(Queue<T> q, int frameCount)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
readyDrainEnumerator = false; // use next queue.
|
||||
running = false;
|
||||
}
|
||||
|
||||
while (!cancelationToken.IsDisposed && frameCount-- != 0)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (q != null)
|
||||
{
|
||||
while (q.Count > 0 && !hasError)
|
||||
{
|
||||
if (cancelationToken.IsDisposed) break;
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
running = true;
|
||||
}
|
||||
|
||||
var value = q.Dequeue();
|
||||
observer.OnNext(value);
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (q.Count == 0)
|
||||
{
|
||||
pool.Return(q);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasError)
|
||||
{
|
||||
if (!cancelationToken.IsDisposed)
|
||||
{
|
||||
cancelationToken.Dispose();
|
||||
|
||||
try { observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
else if (calledCompleted)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
// not self only
|
||||
if (runningEnumeratorCount != 1) yield break;
|
||||
}
|
||||
|
||||
if (!cancelationToken.IsDisposed)
|
||||
{
|
||||
cancelationToken.Dispose();
|
||||
|
||||
try { observer.OnCompleted(); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
runningEnumeratorCount--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
if (cancelationToken.IsDisposed) return;
|
||||
|
||||
Queue<T> targetQueue = null;
|
||||
lock (gate)
|
||||
{
|
||||
if (!readyDrainEnumerator)
|
||||
{
|
||||
readyDrainEnumerator = true;
|
||||
runningEnumeratorCount++;
|
||||
targetQueue = currentQueueReference = pool.Get();
|
||||
targetQueue.Enqueue(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentQueueReference != null) // null - if doesn't start OnNext and start OnCompleted
|
||||
{
|
||||
currentQueueReference.Enqueue(value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch (parent.frameCountType)
|
||||
{
|
||||
case FrameCountType.Update:
|
||||
MainThreadDispatcher.StartUpdateMicroCoroutine(DrainQueue(targetQueue, parent.frameCount));
|
||||
break;
|
||||
case FrameCountType.FixedUpdate:
|
||||
MainThreadDispatcher.StartFixedUpdateMicroCoroutine(DrainQueue(targetQueue, parent.frameCount));
|
||||
break;
|
||||
case FrameCountType.EndOfFrame:
|
||||
MainThreadDispatcher.StartEndOfFrameMicroCoroutine(DrainQueue(targetQueue, parent.frameCount));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid FrameCountType:" + parent.frameCountType);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
sourceSubscription.Dispose(); // stop subscription
|
||||
|
||||
if (cancelationToken.IsDisposed) return;
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
if (running)
|
||||
{
|
||||
hasError = true;
|
||||
this.error = error;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
cancelationToken.Dispose();
|
||||
try { base.observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
sourceSubscription.Dispose(); // stop subscription
|
||||
|
||||
if (cancelationToken.IsDisposed) return;
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
calledCompleted = true;
|
||||
|
||||
if (!readyDrainEnumerator)
|
||||
{
|
||||
readyDrainEnumerator = true;
|
||||
runningEnumeratorCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch (parent.frameCountType)
|
||||
{
|
||||
case FrameCountType.Update:
|
||||
MainThreadDispatcher.StartUpdateMicroCoroutine(DrainQueue(null, parent.frameCount));
|
||||
break;
|
||||
case FrameCountType.FixedUpdate:
|
||||
MainThreadDispatcher.StartFixedUpdateMicroCoroutine(DrainQueue(null, parent.frameCount));
|
||||
break;
|
||||
case FrameCountType.EndOfFrame:
|
||||
MainThreadDispatcher.StartEndOfFrameMicroCoroutine(DrainQueue(null, parent.frameCount));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid FrameCountType:" + parent.frameCountType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class QueuePool
|
||||
{
|
||||
readonly object gate = new object();
|
||||
readonly Queue<Queue<T>> pool = new Queue<Queue<T>>(2);
|
||||
|
||||
public Queue<T> Get()
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
if (pool.Count == 0)
|
||||
{
|
||||
return new Queue<T>(2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return pool.Dequeue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Return(Queue<T> q)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
pool.Enqueue(q);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 868f75a703f1a944a801ab9c9b4512aa
|
||||
timeCreated: 1455373900
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
|
||||
#if UniRxLibrary
|
||||
using UnityObservable = UniRx.ObservableUnity;
|
||||
#else
|
||||
using UnityObservable = UniRx.Observable;
|
||||
#endif
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class DelayFrameSubscriptionObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public DelayFrameSubscriptionObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType)
|
||||
: base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
var d = new MultipleAssignmentDisposable();
|
||||
d.Disposable = UnityObservable.TimerFrame(frameCount, frameCountType)
|
||||
.SubscribeWithState3(observer, d, source, (_, o, disp, s) =>
|
||||
{
|
||||
disp.Disposable = s.Subscribe(o);
|
||||
});
|
||||
|
||||
return d;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecfef95eedf36c2448944fb8932f682c
|
||||
timeCreated: 1455373902
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class FrameIntervalObservable<T> : OperatorObservableBase<UniRx.FrameInterval<T>>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
|
||||
public FrameIntervalObservable(IObservable<T> source)
|
||||
: base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<UniRx.FrameInterval<T>> observer, IDisposable cancel)
|
||||
{
|
||||
return source.Subscribe(new FrameInterval(observer, cancel));
|
||||
}
|
||||
|
||||
class FrameInterval : OperatorObserverBase<T, UniRx.FrameInterval<T>>
|
||||
{
|
||||
int lastFrame;
|
||||
|
||||
public FrameInterval(IObserver<UniRx.FrameInterval<T>> observer, IDisposable cancel)
|
||||
: base(observer, cancel)
|
||||
{
|
||||
this.lastFrame = UnityEngine.Time.frameCount;
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
var now = UnityEngine.Time.frameCount;
|
||||
var span = now - lastFrame;
|
||||
lastFrame = now;
|
||||
|
||||
base.observer.OnNext(new UniRx.FrameInterval<T>(value, span));
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
try { observer.OnError(error); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
try { observer.OnCompleted(); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a731a1a74be20a04a9d7dedc5ceefab2
|
||||
timeCreated: 1467771656
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class FrameTimeIntervalObservable<T> : OperatorObservableBase<UniRx.TimeInterval<T>>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly bool ignoreTimeScale;
|
||||
|
||||
public FrameTimeIntervalObservable(IObservable<T> source, bool ignoreTimeScale)
|
||||
: base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.ignoreTimeScale = ignoreTimeScale;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<UniRx.TimeInterval<T>> observer, IDisposable cancel)
|
||||
{
|
||||
return source.Subscribe(new FrameTimeInterval(this, observer, cancel));
|
||||
}
|
||||
|
||||
class FrameTimeInterval : OperatorObserverBase<T, UniRx.TimeInterval<T>>
|
||||
{
|
||||
readonly FrameTimeIntervalObservable<T> parent;
|
||||
float lastTime;
|
||||
|
||||
public FrameTimeInterval(FrameTimeIntervalObservable<T> parent, IObserver<UniRx.TimeInterval<T>> observer, IDisposable cancel)
|
||||
: base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.lastTime = (parent.ignoreTimeScale)
|
||||
? UnityEngine.Time.unscaledTime
|
||||
: UnityEngine.Time.time;
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
var now = (parent.ignoreTimeScale)
|
||||
? UnityEngine.Time.unscaledTime
|
||||
: UnityEngine.Time.time;
|
||||
var span = now - lastTime;
|
||||
lastTime = now;
|
||||
|
||||
base.observer.OnNext(new UniRx.TimeInterval<T>(value, TimeSpan.FromSeconds(span)));
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
try { observer.OnError(error); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
try { observer.OnCompleted(); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a55cce9ef638364409d1227a25a32421
|
||||
timeCreated: 1467771656
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class FromCoroutineObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly Func<IObserver<T>, CancellationToken, IEnumerator> coroutine;
|
||||
|
||||
public FromCoroutineObservable(Func<IObserver<T>, CancellationToken, IEnumerator> coroutine)
|
||||
: base(false)
|
||||
{
|
||||
this.coroutine = coroutine;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
var fromCoroutineObserver = new FromCoroutine(observer, cancel);
|
||||
|
||||
#if (NETFX_CORE || NET_4_6 || NET_STANDARD_2_0 || UNITY_WSA_10_0)
|
||||
var moreCancel = new CancellationDisposable();
|
||||
var token = moreCancel.Token;
|
||||
#else
|
||||
var moreCancel = new BooleanDisposable();
|
||||
var token = new CancellationToken(moreCancel);
|
||||
#endif
|
||||
|
||||
MainThreadDispatcher.SendStartCoroutine(coroutine(fromCoroutineObserver, token));
|
||||
|
||||
return moreCancel;
|
||||
}
|
||||
|
||||
class FromCoroutine : OperatorObserverBase<T, T>
|
||||
{
|
||||
public FromCoroutine(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
try
|
||||
{
|
||||
base.observer.OnNext(value);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
try { observer.OnError(error); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
try { observer.OnCompleted(); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class FromMicroCoroutineObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly Func<IObserver<T>, CancellationToken, IEnumerator> coroutine;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public FromMicroCoroutineObservable(Func<IObserver<T>, CancellationToken, IEnumerator> coroutine, FrameCountType frameCountType)
|
||||
: base(false)
|
||||
{
|
||||
this.coroutine = coroutine;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
var microCoroutineObserver = new FromMicroCoroutine(observer, cancel);
|
||||
|
||||
#if (NETFX_CORE || NET_4_6 || NET_STANDARD_2_0 || UNITY_WSA_10_0)
|
||||
var moreCancel = new CancellationDisposable();
|
||||
var token = moreCancel.Token;
|
||||
#else
|
||||
var moreCancel = new BooleanDisposable();
|
||||
var token = new CancellationToken(moreCancel);
|
||||
#endif
|
||||
|
||||
switch (frameCountType)
|
||||
{
|
||||
case FrameCountType.Update:
|
||||
MainThreadDispatcher.StartUpdateMicroCoroutine(coroutine(microCoroutineObserver, token));
|
||||
break;
|
||||
case FrameCountType.FixedUpdate:
|
||||
MainThreadDispatcher.StartFixedUpdateMicroCoroutine(coroutine(microCoroutineObserver, token));
|
||||
break;
|
||||
case FrameCountType.EndOfFrame:
|
||||
MainThreadDispatcher.StartEndOfFrameMicroCoroutine(coroutine(microCoroutineObserver, token));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid FrameCountType:" + frameCountType);
|
||||
}
|
||||
|
||||
return moreCancel;
|
||||
}
|
||||
|
||||
class FromMicroCoroutine : OperatorObserverBase<T, T>
|
||||
{
|
||||
public FromMicroCoroutine(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
try
|
||||
{
|
||||
base.observer.OnNext(value);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
try { observer.OnError(error); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
try { observer.OnCompleted(); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e83ddad992535fb4f8a68a1e7ef8be60
|
||||
timeCreated: 1455373901
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class RepeatUntilObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IEnumerable<IObservable<T>> sources;
|
||||
readonly IObservable<Unit> trigger;
|
||||
readonly GameObject lifeTimeChecker;
|
||||
|
||||
public RepeatUntilObservable(IEnumerable<IObservable<T>> sources, IObservable<Unit> trigger, GameObject lifeTimeChecker)
|
||||
: base(true)
|
||||
{
|
||||
this.sources = sources;
|
||||
this.trigger = trigger;
|
||||
this.lifeTimeChecker = lifeTimeChecker;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
return new RepeatUntil(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class RepeatUntil : OperatorObserverBase<T, T>
|
||||
{
|
||||
readonly RepeatUntilObservable<T> parent;
|
||||
readonly object gate = new object();
|
||||
|
||||
IEnumerator<IObservable<T>> e;
|
||||
SerialDisposable subscription;
|
||||
SingleAssignmentDisposable schedule;
|
||||
Action nextSelf;
|
||||
bool isStopped;
|
||||
bool isDisposed;
|
||||
bool isFirstSubscribe;
|
||||
IDisposable stopper;
|
||||
|
||||
public RepeatUntil(RepeatUntilObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
isFirstSubscribe = true;
|
||||
isDisposed = false;
|
||||
isStopped = false;
|
||||
e = parent.sources.GetEnumerator();
|
||||
subscription = new SerialDisposable();
|
||||
schedule = new SingleAssignmentDisposable();
|
||||
|
||||
stopper = parent.trigger.Subscribe(_ =>
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
isStopped = true;
|
||||
e.Dispose();
|
||||
subscription.Dispose();
|
||||
schedule.Dispose();
|
||||
observer.OnCompleted();
|
||||
}
|
||||
}, observer.OnError);
|
||||
|
||||
schedule.Disposable = Scheduler.CurrentThread.Schedule(RecursiveRun);
|
||||
|
||||
return new CompositeDisposable(schedule, subscription, stopper, Disposable.Create(() =>
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
isDisposed = true;
|
||||
e.Dispose();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
void RecursiveRun(Action self)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
this.nextSelf = self;
|
||||
if (isDisposed) return;
|
||||
if (isStopped) return;
|
||||
|
||||
var current = default(IObservable<T>);
|
||||
var hasNext = false;
|
||||
var ex = default(Exception);
|
||||
|
||||
try
|
||||
{
|
||||
hasNext = e.MoveNext();
|
||||
if (hasNext)
|
||||
{
|
||||
current = e.Current;
|
||||
if (current == null) throw new InvalidOperationException("sequence is null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
e.Dispose();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
ex = exception;
|
||||
e.Dispose();
|
||||
}
|
||||
|
||||
if (ex != null)
|
||||
{
|
||||
stopper.Dispose();
|
||||
observer.OnError(ex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasNext)
|
||||
{
|
||||
stopper.Dispose();
|
||||
observer.OnCompleted();
|
||||
return;
|
||||
}
|
||||
|
||||
var source = e.Current;
|
||||
var d = new SingleAssignmentDisposable();
|
||||
subscription.Disposable = d;
|
||||
|
||||
if (isFirstSubscribe)
|
||||
{
|
||||
isFirstSubscribe = false;
|
||||
d.Disposable = source.Subscribe(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
MainThreadDispatcher.SendStartCoroutine(SubscribeAfterEndOfFrame(d, source, this, parent.lifeTimeChecker));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static IEnumerator SubscribeAfterEndOfFrame(SingleAssignmentDisposable d, IObservable<T> source, IObserver<T> observer, GameObject lifeTimeChecker)
|
||||
{
|
||||
yield return YieldInstructionCache.WaitForEndOfFrame;
|
||||
if (!d.IsDisposed && lifeTimeChecker != null)
|
||||
{
|
||||
d.Disposable = source.Subscribe(observer);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
base.observer.OnNext(value);
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
try { observer.OnError(error); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
if (!isDisposed)
|
||||
{
|
||||
this.nextSelf();
|
||||
}
|
||||
else
|
||||
{
|
||||
e.Dispose();
|
||||
if (!isDisposed)
|
||||
{
|
||||
try { observer.OnCompleted(); }
|
||||
finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93507e8a72a71094f870c8dbe1e5bed8
|
||||
timeCreated: 1455373900
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
|
||||
#if UniRxLibrary
|
||||
using UnityObservable = UniRx.ObservableUnity;
|
||||
#else
|
||||
using UnityObservable = UniRx.Observable;
|
||||
#endif
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class SampleFrameObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public SampleFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType) : base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
return new SampleFrame(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class SampleFrame : OperatorObserverBase<T, T>
|
||||
{
|
||||
readonly SampleFrameObservable<T> parent;
|
||||
readonly object gate = new object();
|
||||
T latestValue = default(T);
|
||||
bool isUpdated = false;
|
||||
bool isCompleted = false;
|
||||
SingleAssignmentDisposable sourceSubscription;
|
||||
|
||||
public SampleFrame(SampleFrameObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
sourceSubscription = new SingleAssignmentDisposable();
|
||||
sourceSubscription.Disposable = parent.source.Subscribe(this);
|
||||
|
||||
var scheduling = UnityObservable.IntervalFrame(parent.frameCount, parent.frameCountType)
|
||||
.Subscribe(new SampleFrameTick(this));
|
||||
|
||||
return StableCompositeDisposable.Create(sourceSubscription, scheduling);
|
||||
}
|
||||
|
||||
void OnNextTick(long _)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
if (isUpdated)
|
||||
{
|
||||
var value = latestValue;
|
||||
isUpdated = false;
|
||||
observer.OnNext(value);
|
||||
}
|
||||
if (isCompleted)
|
||||
{
|
||||
try { observer.OnCompleted(); } finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
latestValue = value;
|
||||
isUpdated = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
try { base.observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
isCompleted = true;
|
||||
sourceSubscription.Dispose();
|
||||
}
|
||||
}
|
||||
class SampleFrameTick : IObserver<long>
|
||||
{
|
||||
readonly SampleFrame parent;
|
||||
|
||||
public SampleFrameTick(SampleFrame parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnError(Exception error)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnNext(long _)
|
||||
{
|
||||
lock (parent.gate)
|
||||
{
|
||||
if (parent.isUpdated)
|
||||
{
|
||||
var value = parent.latestValue;
|
||||
parent.isUpdated = false;
|
||||
parent.observer.OnNext(value);
|
||||
}
|
||||
if (parent.isCompleted)
|
||||
{
|
||||
try { parent.observer.OnCompleted(); } finally { parent.Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e04c7fc1929a3db458bf7ae31bcd9e55
|
||||
timeCreated: 1455373901
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class SubscribeOnMainThreadObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly IObservable<long> subscribeTrigger;
|
||||
|
||||
public SubscribeOnMainThreadObservable(IObservable<T> source, IObservable<long> subscribeTrigger)
|
||||
: base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.subscribeTrigger = subscribeTrigger;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
var m = new SingleAssignmentDisposable();
|
||||
var d = new SerialDisposable();
|
||||
d.Disposable = m;
|
||||
|
||||
m.Disposable = subscribeTrigger.SubscribeWithState3(observer, d, source, (_, o, disp, s) =>
|
||||
{
|
||||
disp.Disposable = s.Subscribe(o);
|
||||
});
|
||||
|
||||
return d;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da3fd97518766ab43827991b7b5d4270
|
||||
timeCreated: 1455373901
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
|
||||
#if UniRxLibrary
|
||||
using UnityObservable = UniRx.ObservableUnity;
|
||||
#else
|
||||
using UnityObservable = UniRx.Observable;
|
||||
#endif
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class ThrottleFirstFrameObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public ThrottleFirstFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType) : base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
return new ThrottleFirstFrame(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class ThrottleFirstFrame : OperatorObserverBase<T, T>
|
||||
{
|
||||
readonly ThrottleFirstFrameObservable<T> parent;
|
||||
readonly object gate = new object();
|
||||
bool open = true;
|
||||
SerialDisposable cancelable;
|
||||
|
||||
ThrottleFirstFrameTick tick;
|
||||
|
||||
public ThrottleFirstFrame(ThrottleFirstFrameObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
tick = new ThrottleFirstFrameTick(this);
|
||||
cancelable = new SerialDisposable();
|
||||
|
||||
var subscription = parent.source.Subscribe(this);
|
||||
return StableCompositeDisposable.Create(cancelable, subscription);
|
||||
}
|
||||
|
||||
void OnNext()
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
open = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
if (!open) return;
|
||||
observer.OnNext(value);
|
||||
open = false;
|
||||
}
|
||||
|
||||
var d = new SingleAssignmentDisposable();
|
||||
cancelable.Disposable = d;
|
||||
d.Disposable = UnityObservable.TimerFrame(parent.frameCount, parent.frameCountType)
|
||||
.Subscribe(tick);
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
cancelable.Dispose();
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
try { observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
cancelable.Dispose();
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
try { observer.OnCompleted(); } finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
|
||||
// immutable, can share.
|
||||
class ThrottleFirstFrameTick : IObserver<long>
|
||||
{
|
||||
readonly ThrottleFirstFrame parent;
|
||||
|
||||
public ThrottleFirstFrameTick(ThrottleFirstFrame parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnError(Exception error)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnNext(long _)
|
||||
{
|
||||
lock (parent.gate)
|
||||
{
|
||||
parent.open = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ec92e777b0b4d949967b0663ce8bee8
|
||||
timeCreated: 1455373898
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
|
||||
#if UniRxLibrary
|
||||
using UnityObservable = UniRx.ObservableUnity;
|
||||
#else
|
||||
using UnityObservable = UniRx.Observable;
|
||||
#endif
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class ThrottleFrameObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public ThrottleFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType) : base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
return new ThrottleFrame(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class ThrottleFrame : OperatorObserverBase<T, T>
|
||||
{
|
||||
readonly ThrottleFrameObservable<T> parent;
|
||||
readonly object gate = new object();
|
||||
T latestValue = default(T);
|
||||
bool hasValue = false;
|
||||
SerialDisposable cancelable;
|
||||
ulong id = 0;
|
||||
|
||||
public ThrottleFrame(ThrottleFrameObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
cancelable = new SerialDisposable();
|
||||
var subscription = parent.source.Subscribe(this);
|
||||
|
||||
return StableCompositeDisposable.Create(cancelable, subscription);
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
ulong currentid;
|
||||
lock (gate)
|
||||
{
|
||||
hasValue = true;
|
||||
latestValue = value;
|
||||
id = unchecked(id + 1);
|
||||
currentid = id;
|
||||
}
|
||||
|
||||
var d = new SingleAssignmentDisposable();
|
||||
cancelable.Disposable = d;
|
||||
d.Disposable = UnityObservable.TimerFrame(parent.frameCount, parent.frameCountType)
|
||||
.Subscribe(new ThrottleFrameTick(this, currentid));
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
cancelable.Dispose();
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
hasValue = false;
|
||||
id = unchecked(id + 1);
|
||||
try { observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
cancelable.Dispose();
|
||||
|
||||
lock (gate)
|
||||
{
|
||||
if (hasValue)
|
||||
{
|
||||
observer.OnNext(latestValue);
|
||||
}
|
||||
hasValue = false;
|
||||
id = unchecked(id + 1);
|
||||
try { observer.OnCompleted(); } finally { Dispose(); }
|
||||
}
|
||||
}
|
||||
|
||||
class ThrottleFrameTick : IObserver<long>
|
||||
{
|
||||
readonly ThrottleFrame parent;
|
||||
readonly ulong currentid;
|
||||
|
||||
public ThrottleFrameTick(ThrottleFrame parent, ulong currentid)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.currentid = currentid;
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnError(Exception error)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnNext(long _)
|
||||
{
|
||||
lock (parent.gate)
|
||||
{
|
||||
if (parent.hasValue && parent.id == currentid)
|
||||
{
|
||||
parent.observer.OnNext(parent.latestValue);
|
||||
}
|
||||
parent.hasValue = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c4ef0bfcfe787543999c7a6cda03c07
|
||||
timeCreated: 1455373898
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
|
||||
#if UniRxLibrary
|
||||
using UnityObservable = UniRx.ObservableUnity;
|
||||
#else
|
||||
using UnityObservable = UniRx.Observable;
|
||||
#endif
|
||||
|
||||
namespace UniRx.Operators
|
||||
{
|
||||
internal class TimeoutFrameObservable<T> : OperatorObservableBase<T>
|
||||
{
|
||||
readonly IObservable<T> source;
|
||||
readonly int frameCount;
|
||||
readonly FrameCountType frameCountType;
|
||||
|
||||
public TimeoutFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType) : base(source.IsRequiredSubscribeOnCurrentThread())
|
||||
{
|
||||
this.source = source;
|
||||
this.frameCount = frameCount;
|
||||
this.frameCountType = frameCountType;
|
||||
}
|
||||
|
||||
protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
|
||||
{
|
||||
return new TimeoutFrame(this, observer, cancel).Run();
|
||||
}
|
||||
|
||||
class TimeoutFrame : OperatorObserverBase<T, T>
|
||||
{
|
||||
readonly TimeoutFrameObservable<T> parent;
|
||||
readonly object gate = new object();
|
||||
ulong objectId = 0ul;
|
||||
bool isTimeout = false;
|
||||
SingleAssignmentDisposable sourceSubscription;
|
||||
SerialDisposable timerSubscription;
|
||||
|
||||
public TimeoutFrame(TimeoutFrameObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public IDisposable Run()
|
||||
{
|
||||
sourceSubscription = new SingleAssignmentDisposable();
|
||||
timerSubscription = new SerialDisposable();
|
||||
timerSubscription.Disposable = RunTimer(objectId);
|
||||
sourceSubscription.Disposable = parent.source.Subscribe(this);
|
||||
|
||||
return StableCompositeDisposable.Create(timerSubscription, sourceSubscription);
|
||||
}
|
||||
|
||||
IDisposable RunTimer(ulong timerId)
|
||||
{
|
||||
return UnityObservable.TimerFrame(parent.frameCount, parent.frameCountType)
|
||||
.Subscribe(new TimeoutFrameTick(this, timerId));
|
||||
}
|
||||
|
||||
public override void OnNext(T value)
|
||||
{
|
||||
ulong useObjectId;
|
||||
bool timeout;
|
||||
lock (gate)
|
||||
{
|
||||
timeout = isTimeout;
|
||||
objectId++;
|
||||
useObjectId = objectId;
|
||||
}
|
||||
if (timeout) return;
|
||||
|
||||
timerSubscription.Disposable = Disposable.Empty; // cancel old timer
|
||||
observer.OnNext(value);
|
||||
timerSubscription.Disposable = RunTimer(useObjectId);
|
||||
}
|
||||
|
||||
public override void OnError(Exception error)
|
||||
{
|
||||
bool timeout;
|
||||
lock (gate)
|
||||
{
|
||||
timeout = isTimeout;
|
||||
objectId++;
|
||||
}
|
||||
if (timeout) return;
|
||||
|
||||
timerSubscription.Dispose();
|
||||
try { observer.OnError(error); } finally { Dispose(); }
|
||||
}
|
||||
|
||||
public override void OnCompleted()
|
||||
{
|
||||
bool timeout;
|
||||
lock (gate)
|
||||
{
|
||||
timeout = isTimeout;
|
||||
objectId++;
|
||||
}
|
||||
if (timeout) return;
|
||||
|
||||
timerSubscription.Dispose();
|
||||
try { observer.OnCompleted(); } finally { Dispose(); }
|
||||
}
|
||||
|
||||
class TimeoutFrameTick : IObserver<long>
|
||||
{
|
||||
readonly TimeoutFrame parent;
|
||||
readonly ulong timerId;
|
||||
|
||||
public TimeoutFrameTick(TimeoutFrame parent, ulong timerId)
|
||||
{
|
||||
this.parent = parent;
|
||||
this.timerId = timerId;
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnError(Exception error)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnNext(long _)
|
||||
{
|
||||
|
||||
|
||||
lock (parent.gate)
|
||||
{
|
||||
if (parent.objectId == timerId)
|
||||
{
|
||||
parent.isTimeout = true;
|
||||
}
|
||||
}
|
||||
if (parent.isTimeout)
|
||||
{
|
||||
try { parent.observer.OnError(new TimeoutException()); } finally { parent.Dispose(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c27be0a585d78a944bccd31b86ee6722
|
||||
timeCreated: 1455373901
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user