-
Notifications
You must be signed in to change notification settings - Fork 751
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
4.x: Improve Generate() internals #674
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -28,36 +28,38 @@ public NoTime(TState initialState, Func<TState, bool> condition, Func<TState, TS | |
|
||
protected override _ CreateSink(IObserver<TResult> observer) => new _(this, observer); | ||
|
||
protected override void Run(_ sink) => sink.Run(); | ||
protected override void Run(_ sink) => sink.Run(_scheduler); | ||
|
||
internal sealed class _ : IdentitySink<TResult> | ||
{ | ||
// CONSIDER: This sink has a parent reference that can be considered for removal. | ||
|
||
private readonly NoTime _parent; | ||
readonly Func<TState, bool> _condition; | ||
readonly Func<TState, TState> _iterate; | ||
readonly Func<TState, TResult> _resultSelector; | ||
|
||
public _(NoTime parent, IObserver<TResult> observer) | ||
: base(observer) | ||
{ | ||
_parent = parent; | ||
_condition = parent._condition; | ||
_iterate = parent._iterate; | ||
_resultSelector = parent._resultSelector; | ||
|
||
_state = parent._initialState; | ||
_first = true; | ||
} | ||
|
||
private TState _state; | ||
private bool _first; | ||
|
||
public void Run() | ||
public void Run(IScheduler _scheduler) | ||
{ | ||
_state = _parent._initialState; | ||
_first = true; | ||
|
||
var longRunning = _parent._scheduler.AsLongRunning(); | ||
var longRunning = _scheduler.AsLongRunning(); | ||
if (longRunning != null) | ||
{ | ||
SetUpstream(longRunning.ScheduleLongRunning(this, (@this, c) => @this.Loop(c))); | ||
} | ||
else | ||
{ | ||
SetUpstream(_parent._scheduler.Schedule(this, (@this, a) => @this.LoopRec(a))); | ||
SetUpstream(_scheduler.Schedule(this, (@this, a) => @this.LoopRec(a))); | ||
} | ||
} | ||
|
||
|
@@ -75,14 +77,14 @@ private void Loop(ICancelable cancel) | |
} | ||
else | ||
{ | ||
_state = _parent._iterate(_state); | ||
_state = _iterate(_state); | ||
} | ||
|
||
hasResult = _parent._condition(_state); | ||
hasResult = _condition(_state); | ||
|
||
if (hasResult) | ||
{ | ||
result = _parent._resultSelector(_state); | ||
result = _resultSelector(_state); | ||
} | ||
} | ||
catch (Exception exception) | ||
|
@@ -119,14 +121,14 @@ private void LoopRec(Action<_> recurse) | |
} | ||
else | ||
{ | ||
_state = _parent._iterate(_state); | ||
_state = _iterate(_state); | ||
} | ||
|
||
hasResult = _parent._condition(_state); | ||
hasResult = _condition(_state); | ||
|
||
if (hasResult) | ||
{ | ||
result = _parent._resultSelector(_state); | ||
result = _resultSelector(_state); | ||
} | ||
} | ||
catch (Exception exception) | ||
|
@@ -169,31 +171,34 @@ public Absolute(TState initialState, Func<TState, bool> condition, Func<TState, | |
|
||
protected override _ CreateSink(IObserver<TResult> observer) => new _(this, observer); | ||
|
||
protected override void Run(_ sink) => sink.Run(); | ||
protected override void Run(_ sink) => sink.Run(_scheduler, _initialState); | ||
|
||
internal sealed class _ : IdentitySink<TResult> | ||
{ | ||
// CONSIDER: This sink has a parent reference that can be considered for removal. | ||
|
||
private readonly Absolute _parent; | ||
readonly Func<TState, bool> _condition; | ||
readonly Func<TState, TState> _iterate; | ||
readonly Func<TState, TResult> _resultSelector; | ||
readonly Func<TState, DateTimeOffset> _timeSelector; | ||
|
||
public _(Absolute parent, IObserver<TResult> observer) | ||
: base(observer) | ||
{ | ||
_parent = parent; | ||
_condition = parent._condition; | ||
_iterate = parent._iterate; | ||
_resultSelector = parent._resultSelector; | ||
_timeSelector = parent._timeSelector; | ||
|
||
_first = true; | ||
} | ||
|
||
private bool _first; | ||
private bool _hasResult; | ||
|
||
private TResult _result; | ||
|
||
public void Run() | ||
public void Run(IScheduler outerScheduler, TState initialState) | ||
{ | ||
_first = true; | ||
_hasResult = false; | ||
_result = default(TResult); | ||
|
||
SetUpstream(_parent._scheduler.Schedule((@this: this, _parent._initialState), (scheduler, tuple) => [email protected](scheduler, tuple._initialState))); | ||
SetUpstream(outerScheduler.Schedule((@this: this, initialState), (scheduler, tuple) => [email protected](scheduler, tuple.initialState))); | ||
} | ||
|
||
private IDisposable InvokeRec(IScheduler self, TState state) | ||
|
@@ -213,15 +218,15 @@ private IDisposable InvokeRec(IScheduler self, TState state) | |
} | ||
else | ||
{ | ||
state = _parent._iterate(state); | ||
state = _iterate(state); | ||
} | ||
|
||
_hasResult = _parent._condition(state); | ||
_hasResult = _condition(state); | ||
|
||
if (_hasResult) | ||
{ | ||
_result = _parent._resultSelector(state); | ||
time = _parent._timeSelector(state); | ||
_result = _resultSelector(state); | ||
time = _timeSelector(state); | ||
} | ||
} | ||
catch (Exception exception) | ||
|
@@ -262,31 +267,33 @@ public Relative(TState initialState, Func<TState, bool> condition, Func<TState, | |
|
||
protected override _ CreateSink(IObserver<TResult> observer) => new _(this, observer); | ||
|
||
protected override void Run(_ sink) => sink.Run(); | ||
protected override void Run(_ sink) => sink.Run(_scheduler, _initialState); | ||
|
||
internal sealed class _ : IdentitySink<TResult> | ||
{ | ||
// CONSIDER: This sink has a parent reference that can be considered for removal. | ||
|
||
private readonly Relative _parent; | ||
private readonly Func<TState, bool> _condition; | ||
private readonly Func<TState, TState> _iterate; | ||
private readonly Func<TState, TResult> _resultSelector; | ||
private readonly Func<TState, TimeSpan> _timeSelector; | ||
|
||
public _(Relative parent, IObserver<TResult> observer) | ||
: base(observer) | ||
{ | ||
_parent = parent; | ||
_condition = parent._condition; | ||
_iterate = parent._iterate; | ||
_resultSelector = parent._resultSelector; | ||
_timeSelector = parent._timeSelector; | ||
|
||
_first = true; | ||
} | ||
|
||
private bool _first; | ||
private bool _hasResult; | ||
private TResult _result; | ||
|
||
public void Run() | ||
public void Run(IScheduler outerScheduler, TState initialState) | ||
{ | ||
_first = true; | ||
_hasResult = false; | ||
_result = default(TResult); | ||
|
||
SetUpstream(_parent._scheduler.Schedule((@this: this, _parent._initialState), (scheduler, tuple) => [email protected](scheduler, tuple._initialState))); | ||
SetUpstream(outerScheduler.Schedule((@this: this, initialState), (scheduler, tuple) => [email protected](scheduler, tuple.initialState))); | ||
} | ||
|
||
private IDisposable InvokeRec(IScheduler self, TState state) | ||
|
@@ -306,15 +313,15 @@ private IDisposable InvokeRec(IScheduler self, TState state) | |
} | ||
else | ||
{ | ||
state = _parent._iterate(state); | ||
state = _iterate(state); | ||
} | ||
|
||
_hasResult = _parent._condition(state); | ||
_hasResult = _condition(state); | ||
|
||
if (_hasResult) | ||
{ | ||
_result = _parent._resultSelector(state); | ||
time = _parent._timeSelector(state); | ||
_result = _resultSelector(state); | ||
time = _timeSelector(state); | ||
} | ||
} | ||
catch (Exception exception) | ||
|
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.
Assign the scheduler also in the constructor?
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.
The
_scheduler
is only required when starting the operator and is no longer needed after that in the Loop(Rec) body.