Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(share): Remove reliance on take #7016

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/internal/operators/share.ts
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important bit here.

}