-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When averageLengthEstimate is updating it briefly has spurious values which causes jitter. Also reduced horizontal scrollbar noise.
- Loading branch information
Showing
4 changed files
with
169 additions
and
28 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
src/main/java/org/fxmisc/flowless/PausableSuccessionStream.java
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package org.fxmisc.flowless; | ||
|
||
import java.time.Duration; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
import org.reactfx.AwaitingEventStream; | ||
import org.reactfx.EventStream; | ||
import org.reactfx.EventStreamBase; | ||
import org.reactfx.Subscription; | ||
import org.reactfx.util.FxTimer; | ||
import org.reactfx.util.Timer; | ||
|
||
import javafx.beans.binding.BooleanBinding; | ||
import javafx.beans.property.BooleanProperty; | ||
import javafx.beans.value.ObservableBooleanValue; | ||
|
||
class PausableSuccessionStream<O> extends EventStreamBase<O> implements AwaitingEventStream<O> { | ||
private final EventStream<O> input; | ||
private final Function<? super O, ? extends O> initial; | ||
private final BiFunction<? super O, ? super O, ? extends O> reduction; | ||
private final Timer timer; | ||
|
||
private boolean hasEvent = false; | ||
private BooleanBinding pending = null; | ||
private BooleanProperty successionOff; | ||
private O event = null; | ||
|
||
/** | ||
* Returns an event stream that, when events are emitted from this stream | ||
* in close temporal succession, emits only the last event of the | ||
* succession. What is considered a <i>close temporal succession</i> is | ||
* defined by {@code timeout}: time gap between two successive events must | ||
* be at most {@code timeout}. | ||
* | ||
* <p><b>Note:</b> This function can be used only when this stream and | ||
* the returned stream are used from the JavaFX application thread.</p> | ||
* | ||
* @param timeout the maximum time difference between two subsequent events | ||
* in a <em>close</em> succession. | ||
* @param realTime when true immediately emits the next event and sets | ||
* realTime back to <em>false</em>. | ||
*/ | ||
public PausableSuccessionStream( EventStream<O> input, Duration timeout, BooleanProperty realTime ) | ||
{ | ||
this( input, (Function<? super O, ? extends O>) Function.identity(), (a,b) -> b, timeout, realTime ); | ||
} | ||
|
||
public PausableSuccessionStream( | ||
EventStream<O> input, | ||
Function<? super O, ? extends O> initial, | ||
BiFunction<? super O, ? super O, ? extends O> reduction, | ||
Duration timeout, | ||
BooleanProperty realTime) { | ||
|
||
this.input = input; | ||
this.initial = initial; | ||
this.reduction = reduction; | ||
this.successionOff = realTime; | ||
|
||
this.timer = FxTimer.create(timeout, this::handleTimeout); | ||
} | ||
|
||
@Override | ||
public ObservableBooleanValue pendingProperty() { | ||
if(pending == null) { | ||
pending = new BooleanBinding() { | ||
@Override | ||
protected boolean computeValue() { | ||
return hasEvent; | ||
} | ||
}; | ||
} | ||
return pending; | ||
} | ||
|
||
@Override | ||
public boolean isPending() { | ||
return pending != null ? pending.get() : hasEvent; | ||
} | ||
|
||
@Override | ||
protected final Subscription observeInputs() { | ||
return input.subscribe(this::handleEvent); | ||
} | ||
|
||
private void handleEvent(O i) { | ||
if(successionOff.get()) | ||
{ | ||
timer.stop(); | ||
hasEvent = false; | ||
event = null; | ||
emit(i); | ||
successionOff.setValue(false); | ||
} | ||
else | ||
{ | ||
if(hasEvent) { | ||
event = reduction.apply(event, i); | ||
} else { | ||
event = initial.apply(i); | ||
hasEvent = true; | ||
invalidatePending(); | ||
} | ||
timer.restart(); | ||
} | ||
} | ||
|
||
private void handleTimeout() { | ||
hasEvent = false; | ||
O toEmit = event; | ||
event = null; | ||
emit(toEmit); | ||
invalidatePending(); | ||
} | ||
|
||
private void invalidatePending() { | ||
if(pending != null) { | ||
pending.invalidate(); | ||
} | ||
} | ||
} |
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
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
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