From cc3995a6f6baf9456ec11f749fe89bf61b9e2d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mladen=20Jakovljevi=C4=87?= Date: Fri, 16 Dec 2022 00:21:01 +0100 Subject: [PATCH] feat(share): ShareConfig factory properties should support ObservableInput (#7093) * feat(share): ShareConfig factory properties should support ObservableInput * test(share): add test that verifies Promise support --- spec-dtslint/operators/share-spec.ts | 5 +++++ src/internal/operators/share.ts | 31 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/spec-dtslint/operators/share-spec.ts b/spec-dtslint/operators/share-spec.ts index 560941195b..3587116419 100644 --- a/spec-dtslint/operators/share-spec.ts +++ b/spec-dtslint/operators/share-spec.ts @@ -8,3 +8,8 @@ it('should infer correctly', () => { it('should enforce types', () => { const o = of('foo', 'bar', 'baz').pipe(share('abc')); // $ExpectError }); + +it('should support Promises', () => { + const factory = () => Promise.resolve(); + of(1, 2, 3).pipe(share({ resetOnError: factory, resetOnComplete: factory, resetOnRefCountZero: factory })); // $ExpectType Observable +}); diff --git a/src/internal/operators/share.ts b/src/internal/operators/share.ts index 5d88c7eb55..bc0c2707f2 100644 --- a/src/internal/operators/share.ts +++ b/src/internal/operators/share.ts @@ -1,9 +1,8 @@ -import { Observable } from '../Observable'; import { innerFrom } from '../observable/innerFrom'; import { Subject } from '../Subject'; import { SafeSubscriber } from '../Subscriber'; import { Subscription } from '../Subscription'; -import { MonoTypeOperatorFunction, SubjectLike } from '../types'; +import { MonoTypeOperatorFunction, SubjectLike, ObservableInput } from '../types'; import { operate } from '../util/lift'; export interface ShareConfig { @@ -13,37 +12,37 @@ export interface ShareConfig { */ connector?: () => SubjectLike; /** - * If true, the resulting observable will reset internal state on error from source and return to a "cold" state. This + * If `true`, the resulting observable will reset internal state on error from source and return to a "cold" state. This * allows the resulting observable to be "retried" in the event of an error. - * If false, when an error comes from the source it will push the error into the connecting subject, and the subject + * If `false`, when an error comes from the source it will push the error into the connecting subject, and the subject * will remain the connecting subject, meaning the resulting observable will not go "cold" again, and subsequent retries * or resubscriptions will resubscribe to that same subject. In all cases, RxJS subjects will emit the same error again, however * {@link ReplaySubject} will also push its buffered values before pushing the error. - * It is also possible to pass a notifier factory returning an observable instead which grants more fine-grained + * It is also possible to pass a notifier factory returning an `ObservableInput` instead which grants more fine-grained * control over how and when the reset should happen. This allows behaviors like conditional or delayed resets. */ - resetOnError?: boolean | ((error: any) => Observable); + resetOnError?: boolean | ((error: any) => ObservableInput); /** - * If true, the resulting observable will reset internal state on completion from source and return to a "cold" state. This + * If `true`, the resulting observable will reset internal state on completion from source and return to a "cold" state. This * allows the resulting observable to be "repeated" after it is done. - * If false, when the source completes, it will push the completion through the connecting subject, and the subject + * If `false`, when the source completes, it will push the completion through the connecting subject, and the subject * will remain the connecting subject, meaning the resulting observable will not go "cold" again, and subsequent repeats * or resubscriptions will resubscribe to that same subject. - * It is also possible to pass a notifier factory returning an observable instead which grants more fine-grained + * It is also possible to pass a notifier factory returning an `ObservableInput` instead which grants more fine-grained * control over how and when the reset should happen. This allows behaviors like conditional or delayed resets. */ - resetOnComplete?: boolean | (() => Observable); + resetOnComplete?: boolean | (() => ObservableInput); /** - * If true, when the number of subscribers to the resulting observable reaches zero due to those subscribers unsubscribing, the + * If `true`, when the number of subscribers to the resulting observable reaches zero due to those subscribers unsubscribing, the * internal state will be reset and the resulting observable will return to a "cold" state. This means that the next * time the resulting observable is subscribed to, a new subject will be created and the source will be subscribed to * again. - * If false, when the number of subscribers to the resulting observable reaches zero due to unsubscription, the subject + * If `false`, when the number of subscribers to the resulting observable reaches zero due to unsubscription, the subject * will remain connected to the source, and new subscriptions to the result will be connected through that same subject. - * It is also possible to pass a notifier factory returning an observable instead which grants more fine-grained + * It is also possible to pass a notifier factory returning an `ObservableInput` instead which grants more fine-grained * control over how and when the reset should happen. This allows behaviors like conditional or delayed resets. */ - resetOnRefCountZero?: boolean | (() => Observable); + resetOnRefCountZero?: boolean | (() => ObservableInput); } export function share(): MonoTypeOperatorFunction; @@ -245,7 +244,7 @@ export function share(options: ShareConfig = {}): MonoTypeOperatorFunction function handleReset( reset: () => void, - on: boolean | ((...args: T) => Observable), + on: boolean | ((...args: T) => ObservableInput), ...args: T ): Subscription | undefined { if (on === true) { @@ -264,5 +263,5 @@ function handleReset( }, }); - return on(...args).subscribe(onSubscriber); + return innerFrom(on(...args)).subscribe(onSubscriber); }