Skip to content

Commit cc3995a

Browse files
authoredDec 15, 2022
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
1 parent dfd95db commit cc3995a

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed
 

‎spec-dtslint/operators/share-spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ it('should infer correctly', () => {
88
it('should enforce types', () => {
99
const o = of('foo', 'bar', 'baz').pipe(share('abc')); // $ExpectError
1010
});
11+
12+
it('should support Promises', () => {
13+
const factory = () => Promise.resolve();
14+
of(1, 2, 3).pipe(share({ resetOnError: factory, resetOnComplete: factory, resetOnRefCountZero: factory })); // $ExpectType Observable<number>
15+
});

‎src/internal/operators/share.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Observable } from '../Observable';
21
import { innerFrom } from '../observable/innerFrom';
32
import { Subject } from '../Subject';
43
import { SafeSubscriber } from '../Subscriber';
54
import { Subscription } from '../Subscription';
6-
import { MonoTypeOperatorFunction, SubjectLike } from '../types';
5+
import { MonoTypeOperatorFunction, SubjectLike, ObservableInput } from '../types';
76
import { operate } from '../util/lift';
87

98
export interface ShareConfig<T> {
@@ -13,37 +12,37 @@ export interface ShareConfig<T> {
1312
*/
1413
connector?: () => SubjectLike<T>;
1514
/**
16-
* If true, the resulting observable will reset internal state on error from source and return to a "cold" state. This
15+
* If `true`, the resulting observable will reset internal state on error from source and return to a "cold" state. This
1716
* allows the resulting observable to be "retried" in the event of an error.
18-
* If false, when an error comes from the source it will push the error into the connecting subject, and the subject
17+
* If `false`, when an error comes from the source it will push the error into the connecting subject, and the subject
1918
* will remain the connecting subject, meaning the resulting observable will not go "cold" again, and subsequent retries
2019
* or resubscriptions will resubscribe to that same subject. In all cases, RxJS subjects will emit the same error again, however
2120
* {@link ReplaySubject} will also push its buffered values before pushing the error.
22-
* It is also possible to pass a notifier factory returning an observable instead which grants more fine-grained
21+
* It is also possible to pass a notifier factory returning an `ObservableInput` instead which grants more fine-grained
2322
* control over how and when the reset should happen. This allows behaviors like conditional or delayed resets.
2423
*/
25-
resetOnError?: boolean | ((error: any) => Observable<any>);
24+
resetOnError?: boolean | ((error: any) => ObservableInput<any>);
2625
/**
27-
* If true, the resulting observable will reset internal state on completion from source and return to a "cold" state. This
26+
* If `true`, the resulting observable will reset internal state on completion from source and return to a "cold" state. This
2827
* allows the resulting observable to be "repeated" after it is done.
29-
* If false, when the source completes, it will push the completion through the connecting subject, and the subject
28+
* If `false`, when the source completes, it will push the completion through the connecting subject, and the subject
3029
* will remain the connecting subject, meaning the resulting observable will not go "cold" again, and subsequent repeats
3130
* or resubscriptions will resubscribe to that same subject.
32-
* It is also possible to pass a notifier factory returning an observable instead which grants more fine-grained
31+
* It is also possible to pass a notifier factory returning an `ObservableInput` instead which grants more fine-grained
3332
* control over how and when the reset should happen. This allows behaviors like conditional or delayed resets.
3433
*/
35-
resetOnComplete?: boolean | (() => Observable<any>);
34+
resetOnComplete?: boolean | (() => ObservableInput<any>);
3635
/**
37-
* If true, when the number of subscribers to the resulting observable reaches zero due to those subscribers unsubscribing, the
36+
* If `true`, when the number of subscribers to the resulting observable reaches zero due to those subscribers unsubscribing, the
3837
* internal state will be reset and the resulting observable will return to a "cold" state. This means that the next
3938
* time the resulting observable is subscribed to, a new subject will be created and the source will be subscribed to
4039
* again.
41-
* If false, when the number of subscribers to the resulting observable reaches zero due to unsubscription, the subject
40+
* If `false`, when the number of subscribers to the resulting observable reaches zero due to unsubscription, the subject
4241
* will remain connected to the source, and new subscriptions to the result will be connected through that same subject.
43-
* It is also possible to pass a notifier factory returning an observable instead which grants more fine-grained
42+
* It is also possible to pass a notifier factory returning an `ObservableInput` instead which grants more fine-grained
4443
* control over how and when the reset should happen. This allows behaviors like conditional or delayed resets.
4544
*/
46-
resetOnRefCountZero?: boolean | (() => Observable<any>);
45+
resetOnRefCountZero?: boolean | (() => ObservableInput<any>);
4746
}
4847

4948
export function share<T>(): MonoTypeOperatorFunction<T>;
@@ -245,7 +244,7 @@ export function share<T>(options: ShareConfig<T> = {}): MonoTypeOperatorFunction
245244

246245
function handleReset<T extends unknown[] = never[]>(
247246
reset: () => void,
248-
on: boolean | ((...args: T) => Observable<any>),
247+
on: boolean | ((...args: T) => ObservableInput<any>),
249248
...args: T
250249
): Subscription | undefined {
251250
if (on === true) {
@@ -264,5 +263,5 @@ function handleReset<T extends unknown[] = never[]>(
264263
},
265264
});
266265

267-
return on(...args).subscribe(onSubscriber);
266+
return innerFrom(on(...args)).subscribe(onSubscriber);
268267
}

0 commit comments

Comments
 (0)
Please sign in to comment.