-
Notifications
You must be signed in to change notification settings - Fork 782
Review Delay-operator #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1770b4a
Save the _sourceSubscription field in the various Sinks of Delay, use…
danielcweber f0cfded
Save the allocation of a SingleAssignmentDisposable.
danielcweber 46d3caa
Save some closure allocations and allow delegate caching.
danielcweber 7150a3d
Don't set fields to their default values in Run.
danielcweber c995ad7
Merge branch 'master' into ReviewDelay
danielcweber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,8 +46,6 @@ public S(TParent parent, IObserver<TSource> observer) | |
| _scheduler = parent._scheduler; | ||
| } | ||
|
|
||
| private IDisposable _sourceSubscription; | ||
|
|
||
| protected IStopwatch _watch; | ||
| protected TimeSpan _delay; | ||
| protected bool _ready; | ||
|
|
@@ -62,30 +60,21 @@ public S(TParent parent, IObserver<TSource> observer) | |
|
|
||
| public override void Run(TParent parent) | ||
| { | ||
| _active = false; | ||
| _running = false; | ||
| _queue = new Queue<System.Reactive.TimeInterval<TSource>>(); | ||
| _hasCompleted = false; | ||
| _completeAt = default(TimeSpan); | ||
| _hasFailed = false; | ||
| _exception = default(Exception); | ||
|
|
||
| _watch = _scheduler.StartStopwatch(); | ||
|
|
||
| RunCore(parent); | ||
|
|
||
| Disposable.SetSingle(ref _sourceSubscription, parent._source.SubscribeSafe(this)); | ||
| base.Run(parent._source); | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| base.Dispose(disposing); | ||
|
|
||
| if (disposing) | ||
| { | ||
| Disposable.TryDispose(ref _sourceSubscription); | ||
| Disposable.TryDispose(ref _cancelable); | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| protected abstract void RunCore(TParent parent); | ||
|
|
@@ -112,7 +101,7 @@ public override void OnNext(TSource value) | |
|
|
||
| public override void OnError(Exception error) | ||
| { | ||
| Disposable.TryDispose(ref _sourceSubscription); | ||
| DisposeUpstream(); | ||
|
|
||
| var shouldRun = false; | ||
|
|
||
|
|
@@ -134,7 +123,7 @@ public override void OnError(Exception error) | |
|
|
||
| public override void OnCompleted() | ||
| { | ||
| Disposable.TryDispose(ref _sourceSubscription); | ||
| DisposeUpstream(); | ||
|
|
||
| var shouldRun = false; | ||
|
|
||
|
|
@@ -276,11 +265,9 @@ public L(TParent parent, IObserver<TSource> observer) | |
| _scheduler = parent._scheduler; | ||
| } | ||
|
|
||
| private IDisposable _sourceSubscription; | ||
|
|
||
| protected IStopwatch _watch; | ||
| protected TimeSpan _delay; | ||
| protected Queue<System.Reactive.TimeInterval<TSource>> _queue; | ||
| protected Queue<System.Reactive.TimeInterval<TSource>> _queue = new Queue<System.Reactive.TimeInterval<TSource>>(); | ||
|
|
||
| private CancellationTokenSource _stop; | ||
| private bool _hasCompleted; | ||
|
|
@@ -290,27 +277,21 @@ public L(TParent parent, IObserver<TSource> observer) | |
|
|
||
| public override void Run(TParent parent) | ||
| { | ||
| _queue = new Queue<System.Reactive.TimeInterval<TSource>>(); | ||
| _hasCompleted = false; | ||
| _completeAt = default(TimeSpan); | ||
| _hasFailed = false; | ||
| _exception = default(Exception); | ||
|
|
||
| _watch = _scheduler.StartStopwatch(); | ||
|
|
||
| RunCore(parent); | ||
|
|
||
| Disposable.SetSingle(ref _sourceSubscription, parent._source.SubscribeSafe(this)); | ||
| base.Run(parent._source); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, no need to initialize fields to their default value.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the default value initializations. |
||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| base.Dispose(disposing); | ||
|
|
||
| if (disposing) | ||
| { | ||
| Disposable.TryDispose(ref _sourceSubscription); | ||
| Disposable.TryDispose(ref _cancelable); | ||
| } | ||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| protected abstract void RunCore(TParent parent); | ||
|
|
@@ -325,7 +306,6 @@ protected void ScheduleDrain() | |
|
|
||
| public override void OnNext(TSource value) | ||
| { | ||
|
|
||
| lock (_gate) | ||
| { | ||
| var next = _watch.Elapsed.Add(_delay); | ||
|
|
@@ -338,7 +318,7 @@ public override void OnNext(TSource value) | |
|
|
||
| public override void OnError(Exception error) | ||
| { | ||
| Disposable.TryDispose(ref _sourceSubscription); | ||
| DisposeUpstream(); | ||
|
|
||
| lock (_gate) | ||
| { | ||
|
|
@@ -353,8 +333,7 @@ public override void OnError(Exception error) | |
|
|
||
| public override void OnCompleted() | ||
| { | ||
| Disposable.TryDispose(ref _sourceSubscription); | ||
|
|
||
| DisposeUpstream(); | ||
|
|
||
| lock (_gate) | ||
| { | ||
|
|
@@ -490,10 +469,10 @@ protected override void RunCore(Absolute parent) | |
| { | ||
| _ready = false; | ||
|
|
||
| Disposable.TrySetSingle(ref _cancelable, parent._scheduler.Schedule(parent._dueTime, Start)); | ||
| Disposable.TrySetSingle(ref _cancelable, parent._scheduler.Schedule(this, parent._dueTime, (_, @this) => @this.Start())); | ||
| } | ||
|
|
||
| private void Start() | ||
| private IDisposable Start() | ||
| { | ||
| var next = default(TimeSpan); | ||
| var shouldRun = false; | ||
|
|
@@ -526,6 +505,8 @@ private void Start() | |
| { | ||
| Disposable.TrySetSerial(ref _cancelable, _scheduler.Schedule((Base<Absolute>.S)this, next, (@this, a) => DrainQueue(a))); | ||
| } | ||
|
|
||
| return Disposable.Empty; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -541,10 +522,10 @@ protected override void RunCore(Absolute parent) | |
| // ScheduleDrain might have already set a newer disposable | ||
| // using TrySetSerial would cancel it, stopping the emission | ||
| // and hang the consumer | ||
| Disposable.TrySetSingle(ref _cancelable, parent._scheduler.Schedule(parent._dueTime, Start)); | ||
| Disposable.TrySetSingle(ref _cancelable, parent._scheduler.Schedule(this, parent._dueTime, (_, @this) => @this.Start())); | ||
| } | ||
|
|
||
| private void Start() | ||
| private IDisposable Start() | ||
| { | ||
| lock (_gate) | ||
| { | ||
|
|
@@ -561,6 +542,8 @@ private void Start() | |
| } | ||
|
|
||
| ScheduleDrain(); | ||
|
|
||
| return Disposable.Empty; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -674,9 +657,9 @@ public override void OnNext(TSource value) | |
| return; | ||
| } | ||
|
|
||
| var d = new SingleAssignmentDisposable(); | ||
| _delays.Add(d); | ||
| d.Disposable = delay.SubscribeSafe(new DelayObserver(this, value, d)); | ||
| var observer = new DelayObserver(this, value); | ||
| _delays.Add(observer); | ||
| observer.SetResource(delay.SubscribeSafe(observer)); | ||
| } | ||
|
|
||
| public override void OnError(Exception error) | ||
|
|
@@ -706,45 +689,43 @@ private void CheckDone() | |
| } | ||
| } | ||
|
|
||
| private sealed class DelayObserver : IObserver<TDelay> | ||
| private sealed class DelayObserver : SafeObserver<TDelay> | ||
| { | ||
| private readonly _ _parent; | ||
| private readonly TSource _value; | ||
| private readonly IDisposable _self; | ||
|
|
||
| public DelayObserver(_ parent, TSource value, IDisposable self) | ||
| public DelayObserver(_ parent, TSource value) | ||
| { | ||
| _parent = parent; | ||
| _value = value; | ||
| _self = self; | ||
| } | ||
|
|
||
| public void OnNext(TDelay value) | ||
| public override void OnNext(TDelay value) | ||
| { | ||
| lock (_parent._gate) | ||
| { | ||
| _parent.ForwardOnNext(_value); | ||
|
|
||
| _parent._delays.Remove(_self); | ||
| _parent._delays.Remove(this); | ||
| _parent.CheckDone(); | ||
| } | ||
| } | ||
|
|
||
| public void OnError(Exception error) | ||
| public override void OnError(Exception error) | ||
| { | ||
| lock (_parent._gate) | ||
| { | ||
| _parent.ForwardOnError(error); | ||
| } | ||
| } | ||
|
|
||
| public void OnCompleted() | ||
| public override void OnCompleted() | ||
| { | ||
| lock (_parent._gate) | ||
| { | ||
| _parent.ForwardOnNext(_value); | ||
|
|
||
| _parent._delays.Remove(_self); | ||
| _parent._delays.Remove(this); | ||
| _parent.CheckDone(); | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest removing those init-to-default lines above this and move
_queue = ...into the constructor/field initializer.