-
Notifications
You must be signed in to change notification settings - Fork 8
prune
Creates an raix.reactive.IConnectableObservable that repeats the last value from the source to subscribers after the original observable completes.
function prune(scheduler : IScheduler = null) : IConnectableObservable.<T>
See publish for an overview of raix.reactive.IConnectableObservable.
Prune is quite often a good choice for situations where a observer may want to subscribe to a hot sequence after it has already completed. Namely, the return of a web request (where there is only one value). In these cases, prune can be used to ‘remember’ the last value from the source sequence. For an operator that remembers all values in the source sequence, see replay.
Any observers that are subscribing while the source sequence is still live will receive live messages.
source = source sequence (when connected to)
observer 1 = subscriber during sequence
observer 2 = subscriber after sequence completes
┌─────────┐
source ──o───o───o───/ │
│ │ │ │
observer 1 ─o───o───/ │
│
observer 2 ─o/
The line on “source” represents the time from when IConnectableObservable.connect() was called until the sequence completed.
“observer 1” subscribed while the source sequence was still running, but after the first value was emitted. It, therefore, received live values but missed the first value.
“observer 2” subscribed sometime after the source sequence finished and therefore received the last value received from the source sequence and is then completed.
IConnectableObservable.<T>
var obs : IConnectableObservable Observable.fromEvent(stage, MouseEvent.MOUSE_MOVE)
.map(String, function(ev:MouseEvent) : String { return ev.x.toString() + "," + ev.y.toString(); })
.take(3)
.prune();
obs.subscribe(
function(coords : String) : void { trace("Live: " + coords); }
);
obs.connect();
// Some time later, after 3 mouse movements
obs.subscribe(
function(coords : String) : void { trace("Later: " + coords); }
);
// Trace output is:
// Live: 50,50
// Live: 50,51
// Live: 52,51
// (some time later)
// Later: 52,51