2017-05-08 4 views
0

...「デフォルト」サブスクリプションのRxでいない加入者(リアクティブエクステンション)であれば

"Exclusive" and "Default" Subscription Modes in Rx

私の場合は非常に似ています。加入者が存在しない場合、私はそのようなものを作成したい...

class Example { 
    private stativ readonly Subject<Unit> EventStreamSubject = 
     new Subject<Unit>(); 

    public static IObservable<Unit> EventStream { 
     get { 
      // TODO 
     } 
    } 

    public static void SetDefault(IObserver<Unit> defaultSubscriber) { 
     _defaultSubscriber = defaultSubscriber; 
     // TODO 
    } 
    private static IObserver<Unit> _defaultSubscriber; 
} 

ルール

は...一つだけ、あるいはまったくDefaultSubscriber

    • がある場合もありますEventStreamのDefaultSubscriberが使用されます
    • Subscriberが任意の数のEventStreamを購読すると、DefaultSubscriberはサブスクリプションが配置された
    • EventStreamがすでに別の加入者が加入(と配置された最後のサブスクリプションまで使用されていない)された後DefaultSubscriberを設定することができ、DefaultSubscriber場合はその逆

    は、要するに... EventStreamは常に加入しています利用可能です。 DefaultSubscriberまたは他の外部加入者によって(またはDefaultSubscriberがない場合は)

    サブスクリプションの変更を確認するためにRxで任意の方法を使用できますか? サブスクリプションを監視したり、サブスクリプションの終了を観察できますか?

    他の質問との違いは... 要素には、どの加入者を使用すべきかについての情報はありません。

    ありがとうございました。

    ここで私が実行取得したい私のコード...

    static void Main(string[] args) 
        { 
         var defaultSubscriber = Observer.Create(x => Console.WriteLine("DefaultSubscriber is used")); 
         Example.SetDefault(defaultSubscriber); 
    
         Example.EventStreamSubject.OnNext(Unit.Default); 
    
         using (Example.EventStream.Subscribe(x => Console.WriteLine("Subscriber 1 is used"))) 
         { 
          Example.EventStreamSubject.OnNext(Unit.Default); 
    
          using (Example.EventStream.Subscribe(x => Console.WriteLine("Subscriber 1.1 is used"))) 
          { 
           Example.EventStreamSubject.OnNext(Unit.Default); 
          } 
    
          Example.EventStreamSubject.OnNext(Unit.Default); 
         } 
    
         Example.EventStreamSubject.OnNext(Unit.Default); 
    
         using (Example.EventStream.Subscribe(x => Console.WriteLine("Subscriber 2 is used"))) 
         { 
          using (Example.EventStream.Subscribe(x => Console.WriteLine("Subscriber 2.1 is used"))) 
          { 
           Example.EventStreamSubject.OnNext(Unit.Default); 
          } 
         } 
    
         Example.EventStreamSubject.OnNext(Unit.Default); 
    
         // expected output: 
         //  DefaultSubscriber is used 
         //  Subscriber 1 is used 
         //  Subscriber 1 is used 
         //  Subscriber 1.1 is used 
         //  Subscriber 1 is used 
         //  DefaultSubscriber is used 
         //  Subscriber 2 is used 
         //  Subscriber 2.1 is used 
         //  DefaultSubscriber is used 
        } 
    } 
    
  • 答えて

    0

    は、私は、外部の加入者のために別の "件名" を使用...

    class Example 
    { 
        internal static readonly Subject<Unit> EventStreamSubject = 
         new Subject<Unit>(); 
    
        private static readonly Subject<Unit> ExternalEventStreamSubject = 
         new Subject<Unit>(); 
    
        private static bool _isConnected = false; 
    
        private static void Connect() 
        { 
         if (!_isConnected) 
         { 
          EventStreamSubject.Subscribe(ExternalEventStreamSubject); 
          _isConnected = true; 
         } 
        } 
    
        public static IObservable<Unit> EventStream 
        { 
         get 
         { 
          Connect(); 
          return ExternalEventStreamSubject.AsObservable(); 
         } 
        } 
    
        public static void SetDefault(IObserver<Unit> defaultSubscriber) 
        { 
         if (!Object.ReferenceEquals(_defaultSubscriber, defaultSubscriber)) 
         { 
          _defaultSubscription?.Dispose(); 
    
          _defaultSubscriber = defaultSubscriber; 
    
          if (_defaultSubscriber != null) 
          { 
           _defaultSubscription = EventStreamSubject. 
            Where(x => !ExternalEventStreamSubject.HasObservers). 
            Subscribe(_defaultSubscriber); 
          } 
         } 
        } 
        private static IObserver<Unit> _defaultSubscriber; 
        private static IDisposable _defaultSubscription; 
    } 
    

    を解決策を見つけました。 一部の外部オブザーバーがEventStreamにサブスクライブする場合、サブジェクトの "HasObservers"プロパティーはtrueです。

    外部加入者が追加されていないときに使用されるデフォルトの加入者(またはオブザーバー)を追加できます。 外部のオブザーバーがサブスクライブする場合、外部のサブスクライバーのみが通知を受け取ります(デフォルトのオブザーバーではありません)。

    関連する問題