From f10c42e24385f2fb102a0dda4989cdff6e138923 Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Wed, 6 Sep 2017 14:12:44 -0700 Subject: [PATCH] feat(share): add higher-order lettable version of share --- src/operator/share.ts | 9 ++------- src/operators/index.ts | 1 + src/operators/share.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/operators/share.ts diff --git a/src/operator/share.ts b/src/operator/share.ts index 973a672963..92d7b88c28 100644 --- a/src/operator/share.ts +++ b/src/operator/share.ts @@ -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 @@ -19,5 +14,5 @@ function shareSubjectFactory() { * @owner Observable */ export function share(this: Observable): Observable { - return multicast.call(this, shareSubjectFactory).refCount(); + return higherOrder()(this); }; diff --git a/src/operators/index.ts b/src/operators/index.ts index 0ce5b907d9..7f356703b3 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -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'; diff --git a/src/operators/share.ts b/src/operators/share.ts new file mode 100644 index 0000000000..a15a264090 --- /dev/null +++ b/src/operators/share.ts @@ -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(). + * + * + * + * @return {Observable} An Observable that upon connection causes the source Observable to emit items to its Observers. + * @method share + * @owner Observable + */ +export function share(): MonoTypeOperatorFunction { + return (source: Observable) => refCount()(multicast(shareSubjectFactory)(source)); +};