Reactive-Extensions/RxJS — pull request #67 by jhusain (2013-11-25 05:46:41+00:00)
Title: Adding share, shareValue, and shareReplay.

Adding the share(), shareValue(), and shareReplay() methods. These methods are optimized for the 80% case, in which the developer simply wants to share side effects among multiple concurrent observer. This allows developers simple code like this:

var interval = Rx.Observable.interval(1000);

var source = interval
.take(2)
.doAction(
function (x) {
console.log('Side effect');
})
.share();

// When the number of observers subscribed to published observable goes from
// 0 to 1, we connect to the underlying observable sequence.
published.subscribe(createObserver('SourceA'));
// When the second subscriber is added, no additional subscriptions are added to the
// underlying observable sequence. As a result the operations that result in side
// effects are not repeated per subscriber.
published.subscribe(createObserver('SourceB'));

function createObserver(tag) {
return Rx.Observer.create(
function (x) {
console.log('Next: ' + tag + x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
}