Skip to content
richardszalay edited this page May 20, 2011 · 4 revisions

Emits values into consecutive “windows”, the lifetime for which are controlled by input.

function window(windowClosingSelector : Function) : IObservable

Where windowClosingSelector is: function():IObservable

Remarks

windowClosingSelector is called at the start of each window, the first of which occurs immediately after subscription. When the sequence returned by windowClosingSelector emits a value or completes the window closes and a new window is opened.

A new IObservable is emitted at the start of each window, and any values received from the source during the window’s lifetime are emitted to that sequence.

The returned sequence completes when the source sequence completes

The returned sequence raises an error if the source sequence raises an error or if the “lifetime” sequence raises an error.

Marble Diagrams

wcs = windowClosingSelector

xs ─────o──o─────o──o──/
        |  |     |  |  |
ys o─┬──┼──┼─o┬──┼──┼──/
   |wcs |  | |wcs|  |  |
   | └─────┼/|└──┼──┼──┤
   └────o──o/└───o──o──/

Return Value

IObservable.<T>

Examples

var source : IObservable = Observable.range(1, 10);

var total : int = 0;
var endOfWindow : Subject;

var windowsTotallingSevenOrMore = source.window(function():IObservable
    {
        total = 0;
        endOfWindow = new Subject();
        return endOfWindow;
    })
    .peek(function(value:int) : void
    {
        total += value;

        if (total > 7)
        {
            endOfWindow.onCompleted();
        }
    });

windowsTotallingSevenOrMore
    .map(function(window:IObservable) : IObservable
    {
        return window.toArray();
    })
    .subscribe(function(values : Array) : void
    {
        trace(values.join(", "));
    });

// Output:
// 1, 2, 3, 4
// 5, 6
// 7, 8
// 9
// 10