From 57143be29450ec8be054851eeff90d9d997b3d36 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Sun, 10 Jul 2022 13:16:11 -0500 Subject: [PATCH] refactor(share): Remove reliance on take (#7016) --- src/internal/operators/share.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/internal/operators/share.ts b/src/internal/operators/share.ts index 82c17f98f4..6666bdf146 100644 --- a/src/internal/operators/share.ts +++ b/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'; @@ -153,22 +152,22 @@ export function share(options: ShareConfig = {}): MonoTypeOperatorFunction // call to a source observable's `pipe` method - not when the static `pipe` // function is called. return (wrapperSource) => { - let connection: SafeSubscriber | null = null; - let resetConnection: Subscription | null = null; - let subject: SubjectLike | null = null; + let connection: SafeSubscriber | undefined; + let resetConnection: Subscription | undefined; + let subject: SubjectLike | 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 = () => { @@ -248,18 +247,22 @@ function handleReset( reset: () => void, on: boolean | ((...args: T) => Observable), ...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); }