| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { BehaviorSubject } from '../BehaviorSubject'; |
| 3 |
import { ConnectableObservable } from '../observable/ConnectableObservable'; |
| 4 |
import { UnaryFunction } from '../types'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Creates a {@link ConnectableObservable} that utilizes a {@link BehaviorSubject}. |
| 8 |
* |
| 9 |
* @param initialValue The initial value passed to the {@link BehaviorSubject}. |
| 10 |
* @return A function that returns a {@link ConnectableObservable} |
| 11 |
* @deprecated Will be removed in v8. To create a connectable observable that uses a |
| 12 |
* {@link BehaviorSubject} under the hood, use {@link connectable}. |
| 13 |
* `source.pipe(publishBehavior(initValue))` is equivalent to |
| 14 |
* `connectable(source, { connector: () => new BehaviorSubject(initValue), resetOnDisconnect: false })`. |
| 15 |
* If you're using {@link refCount} after `publishBehavior`, use the {@link share} operator instead. |
| 16 |
* `source.pipe(publishBehavior(initValue), refCount())` is equivalent to |
| 17 |
* `source.pipe(share({ connector: () => new BehaviorSubject(initValue), resetOnError: false, resetOnComplete: false, resetOnRefCountZero: false }))`. |
| 18 |
* Details: https://rxjs.dev/deprecations/multicasting |
| 19 |
*/ |
| 20 |
export function publishBehavior<T>(initialValue: T): UnaryFunction<Observable<T>, ConnectableObservable<T>> { |
| 21 |
// Note that this has *never* supported the selector function. |
| 22 |
return (source) => { |
| 23 |
const subject = new BehaviorSubject<T>(initialValue); |
| 24 |
return new ConnectableObservable(source, () => subject); |
| 25 |
}; |
| 26 |
} |