Skip to content

Commit

Permalink
refactor(share): Remove reliance on take (#7016)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jul 10, 2022
1 parent 5d4c1d9 commit 57143be
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/internal/operators/share.ts
@@ -1,6 +1,5 @@
import { Observable } from '../Observable';
import { innerFrom } from '../observable/innerFrom';
import { take } from '../operators/take';
import { Subject } from '../Subject';
import { SafeSubscriber } from '../Subscriber';
import { Subscription } from '../Subscription';
Expand Down Expand Up @@ -153,22 +152,22 @@ export function share<T>(options: ShareConfig<T> = {}): MonoTypeOperatorFunction
// call to a source observable's `pipe` method - not when the static `pipe`
// function is called.
return (wrapperSource) => {
let connection: SafeSubscriber<T> | null = null;
let resetConnection: Subscription | null = null;
let subject: SubjectLike<T> | null = null;
let connection: SafeSubscriber<T> | undefined;
let resetConnection: Subscription | undefined;
let subject: SubjectLike<T> | undefined;
let refCount = 0;
let hasCompleted = false;
let hasErrored = false;

const cancelReset = () => {
resetConnection?.unsubscribe();
resetConnection = null;
resetConnection = undefined;
};
// Used to reset the internal state to a "cold"
// state, as though it had never been subscribed to.
const reset = () => {
cancelReset();
connection = subject = null;
connection = subject = undefined;
hasCompleted = hasErrored = false;
};
const resetAndUnsubscribe = () => {
Expand Down Expand Up @@ -248,18 +247,22 @@ function handleReset<T extends unknown[] = never[]>(
reset: () => void,
on: boolean | ((...args: T) => Observable<any>),
...args: T
): Subscription | null {
): Subscription | undefined {
if (on === true) {
reset();

return null;
return;
}

if (on === false) {
return null;
return;
}

return on(...args)
.pipe(take(1))
.subscribe(() => reset());
const onSubscriber = new SafeSubscriber({
next: () => {
onSubscriber.unsubscribe();
reset();
},
});

return on(...args).subscribe(onSubscriber);
}

0 comments on commit 57143be

Please sign in to comment.