none
Is it possbile to change delay of a subscription?

    Question

  • (Still learning Rx)

    In the following, I have the Delay(TimeSpan.FromSeconds(1) so that there is a one second delay.  Now lets say that the user has been given an option to change the delay to a different number.  Is there any way to change the existing subscription, or is it necessary to Dispose() of the current subscription, and then create a new one?

    var textChanged = Observable.FromEventPattern<TextChangedEventArgs>(this.textBox, "TextChanged");
    textChanged.Delay(TimeSpan.FromSeconds(1)).ObserveOnDispatcher().Subscribe(e => this.ListItems.Add(this.textBox.Text));

    Greg


    • Edited by Greg Gum Sunday, June 30, 2013 6:22 PM Clarity
    Sunday, June 30, 2013 2:27 PM

Answers

  • Hi Greg,

    Start by creating an observable that represents changes to the delay value.  Then compose a query that projects each delay into a new observable and apply Switch to subscribe to the latest observable.

    IObservable<TimeSpan> delays = GetDelayChanges();
    
    var query = 
    	(from delay in delays
    	 select textChanged.Delay(delay))
    	.Switch();
    
    query.ObserveOnDispatcher()
    	.Subscribe(e => this.ListItems.Add(this.textBox.Text));

    - Dave


    http://davesexton.com/blog

    • Edited by Dave Sexton Monday, July 01, 2013 1:06 AM Formatting
    • Proposed as answer by LeeCampbell Monday, July 01, 2013 10:46 AM
    • Marked as answer by Greg Gum Monday, July 01, 2013 2:47 PM
    Monday, July 01, 2013 1:05 AM

All replies

  • Hi Greg,

    It's not clear what you mean.  Can you provide example output for your code and describe how it differs from the output that you want?

    A short but complete program would be helpful.

    - Dave


    http://davesexton.com/blog

    Sunday, June 30, 2013 4:25 PM
  • Hi Dave,

    I edited the above question for clarity. 

    I appreciate your responses, as always.

    Greg 

    Sunday, June 30, 2013 6:23 PM
  • Hi Greg,

    Start by creating an observable that represents changes to the delay value.  Then compose a query that projects each delay into a new observable and apply Switch to subscribe to the latest observable.

    IObservable<TimeSpan> delays = GetDelayChanges();
    
    var query = 
    	(from delay in delays
    	 select textChanged.Delay(delay))
    	.Switch();
    
    query.ObserveOnDispatcher()
    	.Subscribe(e => this.ListItems.Add(this.textBox.Text));

    - Dave


    http://davesexton.com/blog

    • Edited by Dave Sexton Monday, July 01, 2013 1:06 AM Formatting
    • Proposed as answer by LeeCampbell Monday, July 01, 2013 10:46 AM
    • Marked as answer by Greg Gum Monday, July 01, 2013 2:47 PM
    Monday, July 01, 2013 1:05 AM
  • I see, very nice.

    Greg

    Monday, July 01, 2013 2:48 PM