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

docs: document and export TapObserver #6944

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -185,7 +185,7 @@ export { take } from './internal/operators/take';
export { takeLast } from './internal/operators/takeLast';
export { takeUntil } from './internal/operators/takeUntil';
export { takeWhile } from './internal/operators/takeWhile';
export { tap } from './internal/operators/tap';
export { tap, TapObserver } from './internal/operators/tap';
export { throttle, ThrottleConfig } from './internal/operators/throttle';
export { throttleTime } from './internal/operators/throttleTime';
export { throwIfEmpty } from './internal/operators/throwIfEmpty';
Expand Down
66 changes: 63 additions & 3 deletions src/internal/operators/tap.ts
Expand Up @@ -4,9 +4,71 @@ import { operate } from '../util/lift';
import { createOperatorSubscriber } from './OperatorSubscriber';
import { identity } from '../util/identity';

/**
* An extension to the {@link Observer} interface used only by the {@link tap} operator.
*
* It provides a useful set of callbacks a user can register to do side-effects in
* cases other than what the usual {@link Observer} callbacks are
* ({@link guide/glossary-and-semantics#next next},
* {@link guide/glossary-and-semantics#error error} and/or
* {@link guide/glossary-and-semantics#complete complete}).
*
* ## Example
*
* ```ts
* import { fromEvent, switchMap, tap, interval, take } from 'rxjs';
*
* const source$ = fromEvent(document, 'click');
* const result$ = source$.pipe(
* switchMap((_, i) => i % 2 === 0
* ? fromEvent(document, 'mousemove').pipe(
* tap({
* subscribe: () => console.log('Subscribed to the mouse move events after click #' + i),
* unsubscribe: () => console.log('Mouse move events #' + i + ' unsubscribed'),
* finalize: () => console.log('Mouse move events #' + i + ' finalized')
* })
* )
* : interval(1_000).pipe(
* take(5),
* tap({
* subscribe: () => console.log('Subscribed to the 1-second interval events after click #' + i),
* unsubscribe: () => console.log('1-second interval events #' + i + ' unsubscribed'),
* finalize: () => console.log('1-second interval events #' + i + ' finalized')
* })
* )
* )
* );
*
* const subscription = result$.subscribe({
* next: console.log
* });
*
* setTimeout(() => {
* console.log('Unsubscribe after 60 seconds');
* subscription.unsubscribe();
* }, 60_000);
* ```
*/
export interface TapObserver<T> extends Observer<T> {
/**
* The callback that `tap` operator invokes at the moment when the source Observable
* gets subscribed to.
*/
subscribe: () => void;
/**
* The callback that `tap` operator invokes when an explicit
* {@link guide/glossary-and-semantics#unsubscription unsubscribe} happens. It won't get invoked on
* `error` or `complete` events.
*/
unsubscribe: () => void;
/**
* The callback that `tap` operator invokes when any kind of
* {@link guide/glossary-and-semantics#finalization finalization} happens - either when
* the source Observable `error`s or `complete`s or when it gets explicitly unsubscribed
* by the user. There is no difference in using this callback or the {@link finalize}
* operator, but if you're already using `tap` operator, you can use this callback
* instead. You'd get the same result in either case.
*/
finalize: () => void;
}

Expand Down Expand Up @@ -87,11 +149,9 @@ export interface TapObserver<T> extends Observer<T> {
* ```
*
* @see {@link finalize}
* @see {@link Observable#subscribe}
* @see {@link TapObserver}
*
* @param observerOrNext A next handler or partial observer
* @param error An error handler
* @param complete A completion handler
* @return A function that returns an Observable identical to the source, but
* runs the specified Observer or callback(s) for each item.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/operators/index.ts
Expand Up @@ -92,7 +92,7 @@ export { take } from '../internal/operators/take';
export { takeLast } from '../internal/operators/takeLast';
export { takeUntil } from '../internal/operators/takeUntil';
export { takeWhile } from '../internal/operators/takeWhile';
export { tap } from '../internal/operators/tap';
export { tap, TapObserver } from '../internal/operators/tap';
export { throttle, ThrottleConfig } from '../internal/operators/throttle';
export { throttleTime } from '../internal/operators/throttleTime';
export { throwIfEmpty } from '../internal/operators/throwIfEmpty';
Expand Down