Last active
June 8, 2018 08:47
-
-
Save yKimisaki/0bd6252641e8fc523178 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using UniRx; | |
using UniRx.Operators; | |
namespace Tonari.UniRx | |
{ | |
public static class WithHistoryExtensions | |
{ | |
public static IObservable<WithHistoryObservable<T>.WithHistoryValue> WithHistory<T>(this IObservable<T> source, int maxCount) | |
{ | |
return new WithHistoryObservable<T>(source, maxCount); | |
} | |
} | |
public class WithHistoryObservable<T> : OperatorObservableBase<WithHistoryObservable<T>.WithHistoryValue> | |
{ | |
private IObservable<T> _source; | |
private int _count; | |
public WithHistoryObservable(IObservable<T> source, int count) | |
: base(source.IsRequiredSubscribeOnCurrentThread()) | |
{ | |
_source = source; | |
_count = count; | |
} | |
protected override IDisposable SubscribeCore(IObserver<WithHistoryValue> observer, IDisposable cancel) | |
{ | |
return _source.Subscribe(new WithHistory(observer, cancel, _count)); | |
} | |
private class WithHistory : OperatorObserverBase<T, WithHistoryValue> | |
{ | |
private Queue<T> _history; | |
private int _count; | |
public WithHistory(IObserver<WithHistoryValue> observer, IDisposable cancel, int count) | |
: base(observer, cancel) | |
{ | |
_history = new Queue<T>(); | |
_count = count; | |
} | |
public override void OnNext(T value) | |
{ | |
observer.OnNext(new WithHistoryValue(value, _history.ToArray())); | |
_history.Enqueue(value); | |
if (_history.Count > _count) | |
{ | |
_history.Dequeue(); | |
} | |
} | |
public override void OnError(Exception error) | |
{ | |
try { observer.OnCompleted(); } finally { Dispose(); } | |
} | |
public override void OnCompleted() | |
{ | |
try { observer.OnCompleted(); } finally { Dispose(); } | |
} | |
} | |
public struct WithHistoryValue | |
{ | |
public T Current { get; private set; } | |
/// <summary> | |
/// 最新の値を含まない履歴を古いものから順に返します。 | |
/// </summary> | |
public IList<T> History { get; private set; } | |
public WithHistoryValue(T current, params T[] history) | |
{ | |
Current = current; | |
History = history; | |
} | |
} | |
} | |
} |
Author
yKimisaki
commented
Dec 15, 2015
Bufferと同じでIList返すようにした
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment