다음을 통해 공유


BehaviorSubject<T> 생성자

BehaviorSubject<T> 클래스의 새 instance 초기화하여 마지막 값을 캐시하고 지정된 값으로 시작하는 제목을 만듭니다.

네임스페이스: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 인터페이스에 대해 데이터를 게시하는 방법을 보여 줄 두 개의 구독이 만들어집니다.

  • 구독 #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...

참고 항목

참조

BehaviorSubject<T> 클래스

System.Reactive.Subjects 네임스페이스