| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { AsyncSubject } from '../AsyncSubject'; |
| 3 |
import { ConnectableObservable } from '../observable/ConnectableObservable'; |
| 4 |
import { UnaryFunction } from '../types'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Returns a connectable observable sequence that shares a single subscription to the |
| 8 |
* underlying sequence containing only the last notification. |
| 9 |
* |
| 10 |
*  |
| 11 |
* |
| 12 |
* Similar to {@link publish}, but it waits until the source observable completes and stores |
| 13 |
* the last emitted value. |
| 14 |
* Similarly to {@link publishReplay} and {@link publishBehavior}, this keeps storing the last |
| 15 |
* value even if it has no more subscribers. If subsequent subscriptions happen, they will |
| 16 |
* immediately get that last stored value and complete. |
| 17 |
* |
| 18 |
* ## Example |
| 19 |
* |
| 20 |
* ```ts |
| 21 |
* import { ConnectableObservable, interval, publishLast, tap, take } from 'rxjs'; |
| 22 |
* |
| 23 |
* const connectable = <ConnectableObservable<number>>interval(1000) |
| 24 |
* .pipe( |
| 25 |
* tap(x => console.log('side effect', x)), |
| 26 |
* take(3), |
| 27 |
* publishLast() |
| 28 |
* ); |
| 29 |
* |
| 30 |
* connectable.subscribe({ |
| 31 |
* next: x => console.log('Sub. A', x), |
| 32 |
* error: err => console.log('Sub. A Error', err), |
| 33 |
* complete: () => console.log('Sub. A Complete') |
| 34 |
* }); |
| 35 |
* |
| 36 |
* connectable.subscribe({ |
| 37 |
* next: x => console.log('Sub. B', x), |
| 38 |
* error: err => console.log('Sub. B Error', err), |
| 39 |
* complete: () => console.log('Sub. B Complete') |
| 40 |
* }); |
| 41 |
* |
| 42 |
* connectable.connect(); |
| 43 |
* |
| 44 |
* // Results: |
| 45 |
* // 'side effect 0' - after one second |
| 46 |
* // 'side effect 1' - after two seconds |
| 47 |
* // 'side effect 2' - after three seconds |
| 48 |
* // 'Sub. A 2' - immediately after 'side effect 2' |
| 49 |
* // 'Sub. B 2' |
| 50 |
* // 'Sub. A Complete' |
| 51 |
* // 'Sub. B Complete' |
| 52 |
* ``` |
| 53 |
* |
| 54 |
* @see {@link ConnectableObservable} |
| 55 |
* @see {@link publish} |
| 56 |
* @see {@link publishReplay} |
| 57 |
* @see {@link publishBehavior} |
| 58 |
* |
| 59 |
* @return A function that returns an Observable that emits elements of a |
| 60 |
* sequence produced by multicasting the source sequence. |
| 61 |
* @deprecated Will be removed in v8. To create a connectable observable with an |
| 62 |
* {@link AsyncSubject} under the hood, use {@link connectable}. |
| 63 |
* `source.pipe(publishLast())` is equivalent to |
| 64 |
* `connectable(source, { connector: () => new AsyncSubject(), resetOnDisconnect: false })`. |
| 65 |
* If you're using {@link refCount} after `publishLast`, use the {@link share} operator instead. |
| 66 |
* `source.pipe(publishLast(), refCount())` is equivalent to |
| 67 |
* `source.pipe(share({ connector: () => new AsyncSubject(), resetOnError: false, resetOnComplete: false, resetOnRefCountZero: false }))`. |
| 68 |
* Details: https://rxjs.dev/deprecations/multicasting |
| 69 |
*/ |
| 70 |
export function publishLast<T>(): UnaryFunction<Observable<T>, ConnectableObservable<T>> { |
| 71 |
// Note that this has *never* supported a selector function like `publish` and `publishReplay`. |
| 72 |
return (source) => { |
| 73 |
const subject = new AsyncSubject<T>(); |
| 74 |
return new ConnectableObservable(source, () => subject); |
| 75 |
}; |
| 76 |
} |