BehaviorSubject<T> コンストラクター
BehaviorSubject<T> クラスの新しいインスタンスを初期化します。このクラスは、最後の値をキャッシュし、指定された値で始まるサブジェクトを作成します。
Namespace:System.Reactive.Subjects
アセンブリ: System.Reactive (System.Reactive.dll)
構文
'Declaration
Public Sub New ( _
value As T _
)
'Usage
Dim value As T
Dim instance As New BehaviorSubject(value)
public BehaviorSubject(
T value
)
public:
BehaviorSubject(
T value
)
new :
value:'T -> BehaviorSubject
public function BehaviorSubject(
value : T
)
パラメーター
- 値
種類: T
サブジェクトによって他の値がまだ受信されていない場合にオブザーバーに送信される初期値。
解説
BehaviorSubject は、IObservable インターフェイスを介して発行された最後の項目をバッファーに格納します。 IObservable インターフェイスを介してアイテムが発行されていない場合、コンストラクターで提供される最初の項目は、現在バッファーに格納されている項目です。 BehaviorSubject の IObservable インターフェイスにサブスクリプションが作成されると、発行されるシーケンスは、現在バッファーに格納されている項目から始まります。
IObserver インターフェイスが完了を受け取ると、BehaviorSubject から項目がバッファーまたは発行されません。
例
この例では、BehaviorSubject を示します。 この例では、Interval 演算子を使用して、整数を 1 秒ごとに整数シーケンスに発行します。 シーケンスは、10 個の整数がパブリッシュされた後に Take 演算子によって完了します。 これは BehaviorSubject がサブスクライブするシーケンスです。
BehaviorSubject の IObservable インターフェイス用に 2 つのサブスクリプションが作成され、データの発行方法が示されます。
サブスクリプション #1 : このサブスクリプションは最初から開始され、シーケンス内のコンストラクター (-9) からの初期バッファー値が表示されます。
サブスクリプション #2 : このサブスクリプションは、5 秒のスリープ後に開始されます。 このサブスクリプションは、シーケンスが現在バッファーに格納されている項目で始まることを示しています。
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Threading;
namespace Example
{
class Program
{
static void Main()
{
//********************************************************************************************************//
//*** A subject acts similar to a proxy in that it acts as both a subscriber and a publisher ***//
//*** It's IObserver interface can be used to subscribe to multiple streams or sequences of data. ***//
//*** The data is then published through it's IObservable interface. ***//
//*** ***//
//*** A BehaviorSubject buffers the last item it published through its IObservable interface. If no ***//
//*** item has been published through its IObservable interface then the initial item provided in the ***//
//*** constructor is the current buffered item. When a subscription is made to the BehaviorSubject's ***//
//*** IObservable interface, the sequence published begins with the currently buffered item. ***//
//*** ***//
//*** No items are buffered or published from a BehaviorSubject once its IObserver interface receives ***//
//*** a completion. ***//
//*** ***//
//*** In this example, we use the Interval operator to publish an integer to a integer sequence every ***//
//*** second. The sequence will be completed by the Take operator after 10 integers are published. ***//
//*** This will be the sequence that the BehaviorSubject subscribes to. ***//
//*** ***//
//*** We will create 2 subscriptions to the BehaviorSubject's IObservable interface to show how it ***//
//*** publishes it's data. ***//
//*** ***//
//*** Subscription #1 : This subscription will start at the very beginning and will show the initial ***//
//*** buffered value from the constructor (-9) in the sequence. ***//
//*** ***//
//*** Subscription #2 : This subscription will start after a 5 sec. sleep showing the sequence starts ***//
//*** with the currently buffered item. ***//
//********************************************************************************************************//
BehaviorSubject<long> myBehaviorSubject = new BehaviorSubject<long>((-9));
Observable.Interval(TimeSpan.FromSeconds(1), Scheduler.ThreadPool).Take(10).Subscribe(myBehaviorSubject);
//********************************************************************************************************//
//*** Subscription #1 : This subscription will start at the very beginning and will show the initial ***//
//*** buffered value from the constructor (-9) in the sequence. ***//
//********************************************************************************************************//
EventWaitHandle wait1 = new EventWaitHandle(false, EventResetMode.ManualReset);
myBehaviorSubject.Subscribe(x => Console.WriteLine("Subscription #1 observes : " + x),
() =>
{
Console.WriteLine("Subscription #1 completed.");
wait1.Set();
});
//********************************************************************************************************//
//*** Subscription #2 : This subscription will start after a 5 sec. sleep showing the sequence starts ***//
//*** with the currently buffered item. ***//
//********************************************************************************************************//
Thread.Sleep(5000);
EventWaitHandle wait2 = new EventWaitHandle(false, EventResetMode.ManualReset);
myBehaviorSubject.Subscribe(x => Console.WriteLine("{0,30}Subscription #2 observes : {1}", " ", x),
() =>
{
Console.WriteLine("{0,30}Subscription #2 completed.", " ");
wait2.Set();
});
//**************************************************//
// *** Wait for completion on both subscriptions ***//
//**************************************************//
WaitHandle.WaitAll(new WaitHandle[] { wait1, wait2 });
myBehaviorSubject.Dispose();
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
}
}
コード例からの次の出力は、重複するサブスクリプションを示しています。
Subscription #1 observes : -9
Subscription #1 observes : 0
Subscription #1 observes : 1
Subscription #1 observes : 2
Subscription #1 observes : 3
Subscription #1 observes : 4
Subscription #2 observes : 4
Subscription #1 observes : 5
Subscription #2 observes : 5
Subscription #1 observes : 6
Subscription #2 observes : 6
Subscription #1 observes : 7
Subscription #2 observes : 7
Subscription #1 observes : 8
Subscription #2 observes : 8
Subscription #1 observes : 9
Subscription #2 observes : 9
Subscription #1 completed.
Subscription #2 completed.
Press ENTER to exit...