Skip to content

Commit

Permalink
feat(share): add higher-order lettable version of share
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonaden committed Sep 6, 2017
1 parent 7cd3165 commit f10c42e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/operator/share.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { Observable } from '../Observable';
import { multicast } from './multicast';
import { Subject } from '../Subject';

function shareSubjectFactory() {
return new Subject();
}
import { share as higherOrder } from '../operators/share';

/**
* Returns a new Observable that multicasts (shares) the original Observable. As long as there is at least one
Expand All @@ -19,5 +14,5 @@ function shareSubjectFactory() {
* @owner Observable
*/
export function share<T>(this: Observable<T>): Observable<T> {
return multicast.call(this, shareSubjectFactory).refCount();
return higherOrder()(this);
};
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export { sample } from './sample';
export { sampleTime } from './sampleTime';
export { scan } from './scan';
export { sequenceEqual } from './sequenceEqual';
export { share } from './share';
export { subscribeOn } from './subscribeOn';
export { switchAll } from './switchAll';
export { switchMap } from './switchMap';
Expand Down
26 changes: 26 additions & 0 deletions src/operators/share.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Observable } from '../Observable';
import { multicast } from './multicast';
import { refCount } from './refCount';
import { Subject } from '../Subject';

import { MonoTypeOperatorFunction } from '../interfaces';

function shareSubjectFactory() {
return new Subject();
}

/**
* Returns a new Observable that multicasts (shares) the original Observable. As long as there is at least one
* Subscriber this Observable will be subscribed and emitting data. When all subscribers have unsubscribed it will
* unsubscribe from the source Observable. Because the Observable is multicasting it makes the stream `hot`.
* This is an alias for .publish().refCount().
*
* <img src="./img/share.png" width="100%">
*
* @return {Observable<T>} An Observable that upon connection causes the source Observable to emit items to its Observers.
* @method share
* @owner Observable
*/
export function share<T>(): MonoTypeOperatorFunction<T> {
return (source: Observable<T>) => refCount()(multicast(shareSubjectFactory)(source));
};

0 comments on commit f10c42e

Please sign in to comment.