| 1 |
import { ReplaySubject } from '../ReplaySubject'; |
| 2 |
import { MonoTypeOperatorFunction, SchedulerLike } from '../types'; |
| 3 |
import { share } from './share'; |
| 4 |
|
| 5 |
export interface ShareReplayConfig { |
| 6 |
bufferSize?: number; |
| 7 |
windowTime?: number; |
| 8 |
refCount: boolean; |
| 9 |
scheduler?: SchedulerLike; |
| 10 |
} |
| 11 |
|
| 12 |
export function shareReplay<T>(config: ShareReplayConfig): MonoTypeOperatorFunction<T>; |
| 13 |
export function shareReplay<T>(bufferSize?: number, windowTime?: number, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>; |
| 14 |
|
| 15 |
/** |
| 16 |
* Share source and replay specified number of emissions on subscription. |
| 17 |
* |
| 18 |
* This operator is a specialization of `replay` that connects to a source observable |
| 19 |
* and multicasts through a `ReplaySubject` constructed with the specified arguments. |
| 20 |
* A successfully completed source will stay cached in the `shareReplay`ed observable forever, |
| 21 |
* but an errored source can be retried. |
| 22 |
* |
| 23 |
* ## Why use `shareReplay`? |
| 24 |
* |
| 25 |
* You generally want to use `shareReplay` when you have side-effects or taxing computations |
| 26 |
* that you do not wish to be executed amongst multiple subscribers. |
| 27 |
* It may also be valuable in situations where you know you will have late subscribers to |
| 28 |
* a stream that need access to previously emitted values. |
| 29 |
* This ability to replay values on subscription is what differentiates {@link share} and `shareReplay`. |
| 30 |
* |
| 31 |
* ## Reference counting |
| 32 |
* |
| 33 |
* By default `shareReplay` will use `refCount` of false, meaning that it will _not_ unsubscribe the |
| 34 |
* source when the reference counter drops to zero, i.e. the inner `ReplaySubject` will _not_ be unsubscribed |
| 35 |
* (and potentially run for ever). |
| 36 |
* This is the default as it is expected that `shareReplay` is often used to keep around expensive to setup |
| 37 |
* observables which we want to keep running instead of having to do the expensive setup again. |
| 38 |
* |
| 39 |
* As of RXJS version 6.4.0 a new overload signature was added to allow for manual control over what |
| 40 |
* happens when the operators internal reference counter drops to zero. |
| 41 |
* If `refCount` is true, the source will be unsubscribed from once the reference count drops to zero, i.e. |
| 42 |
* the inner `ReplaySubject` will be unsubscribed. All new subscribers will receive value emissions from a |
| 43 |
* new `ReplaySubject` which in turn will cause a new subscription to the source observable. |
| 44 |
* |
| 45 |
* ## Examples |
| 46 |
* |
| 47 |
* Example with a third subscriber coming late to the party |
| 48 |
* |
| 49 |
* ```ts |
| 50 |
* import { interval, take, shareReplay } from 'rxjs'; |
| 51 |
* |
| 52 |
* const shared$ = interval(2000).pipe( |
| 53 |
* take(6), |
| 54 |
* shareReplay(3) |
| 55 |
* ); |
| 56 |
* |
| 57 |
* shared$.subscribe(x => console.log('sub A: ', x)); |
| 58 |
* shared$.subscribe(y => console.log('sub B: ', y)); |
| 59 |
* |
| 60 |
* setTimeout(() => { |
| 61 |
* shared$.subscribe(y => console.log('sub C: ', y)); |
| 62 |
* }, 11000); |
| 63 |
* |
| 64 |
* // Logs: |
| 65 |
* // (after ~2000 ms) |
| 66 |
* // sub A: 0 |
| 67 |
* // sub B: 0 |
| 68 |
* // (after ~4000 ms) |
| 69 |
* // sub A: 1 |
| 70 |
* // sub B: 1 |
| 71 |
* // (after ~6000 ms) |
| 72 |
* // sub A: 2 |
| 73 |
* // sub B: 2 |
| 74 |
* // (after ~8000 ms) |
| 75 |
* // sub A: 3 |
| 76 |
* // sub B: 3 |
| 77 |
* // (after ~10000 ms) |
| 78 |
* // sub A: 4 |
| 79 |
* // sub B: 4 |
| 80 |
* // (after ~11000 ms, sub C gets the last 3 values) |
| 81 |
* // sub C: 2 |
| 82 |
* // sub C: 3 |
| 83 |
* // sub C: 4 |
| 84 |
* // (after ~12000 ms) |
| 85 |
* // sub A: 5 |
| 86 |
* // sub B: 5 |
| 87 |
* // sub C: 5 |
| 88 |
* ``` |
| 89 |
* |
| 90 |
* Example for `refCount` usage |
| 91 |
* |
| 92 |
* ```ts |
| 93 |
* import { Observable, tap, interval, shareReplay, take } from 'rxjs'; |
| 94 |
* |
| 95 |
* const log = <T>(name: string, source: Observable<T>) => source.pipe( |
| 96 |
* tap({ |
| 97 |
* subscribe: () => console.log(`${ name }: subscribed`), |
| 98 |
* next: value => console.log(`${ name }: ${ value }`), |
| 99 |
* complete: () => console.log(`${ name }: completed`), |
| 100 |
* finalize: () => console.log(`${ name }: unsubscribed`) |
| 101 |
* }) |
| 102 |
* ); |
| 103 |
* |
| 104 |
* const obs$ = log('source', interval(1000)); |
| 105 |
* |
| 106 |
* const shared$ = log('shared', obs$.pipe( |
| 107 |
* shareReplay({ bufferSize: 1, refCount: true }), |
| 108 |
* take(2) |
| 109 |
* )); |
| 110 |
* |
| 111 |
* shared$.subscribe(x => console.log('sub A: ', x)); |
| 112 |
* shared$.subscribe(y => console.log('sub B: ', y)); |
| 113 |
* |
| 114 |
* // PRINTS: |
| 115 |
* // shared: subscribed <-- reference count = 1 |
| 116 |
* // source: subscribed |
| 117 |
* // shared: subscribed <-- reference count = 2 |
| 118 |
* // source: 0 |
| 119 |
* // shared: 0 |
| 120 |
* // sub A: 0 |
| 121 |
* // shared: 0 |
| 122 |
* // sub B: 0 |
| 123 |
* // source: 1 |
| 124 |
* // shared: 1 |
| 125 |
* // sub A: 1 |
| 126 |
* // shared: completed <-- take(2) completes the subscription for sub A |
| 127 |
* // shared: unsubscribed <-- reference count = 1 |
| 128 |
* // shared: 1 |
| 129 |
* // sub B: 1 |
| 130 |
* // shared: completed <-- take(2) completes the subscription for sub B |
| 131 |
* // shared: unsubscribed <-- reference count = 0 |
| 132 |
* // source: unsubscribed <-- replaySubject unsubscribes from source observable because the reference count dropped to 0 and refCount is true |
| 133 |
* |
| 134 |
* // In case of refCount being false, the unsubscribe is never called on the source and the source would keep on emitting, even if no subscribers |
| 135 |
* // are listening. |
| 136 |
* // source: 2 |
| 137 |
* // source: 3 |
| 138 |
* // source: 4 |
| 139 |
* // ... |
| 140 |
* ``` |
| 141 |
* |
| 142 |
* @see {@link publish} |
| 143 |
* @see {@link share} |
| 144 |
* @see {@link publishReplay} |
| 145 |
* |
| 146 |
* @param configOrBufferSize Maximum element count of the replay buffer or {@link ShareReplayConfig configuration} |
| 147 |
* object. |
| 148 |
* @param windowTime Maximum time length of the replay buffer in milliseconds. |
| 149 |
* @param scheduler Scheduler where connected observers within the selector function |
| 150 |
* will be invoked on. |
| 151 |
* @return A function that returns an Observable sequence that contains the |
| 152 |
* elements of a sequence produced by multicasting the source sequence within a |
| 153 |
* selector function. |
| 154 |
*/ |
| 155 |
export function shareReplay<T>( |
| 156 |
configOrBufferSize?: ShareReplayConfig | number, |
| 157 |
windowTime?: number, |
| 158 |
scheduler?: SchedulerLike |
| 159 |
): MonoTypeOperatorFunction<T> { |
| 160 |
let bufferSize: number; |
| 161 |
let refCount = false; |
| 162 |
if (configOrBufferSize && typeof configOrBufferSize === 'object') { |
| 163 |
({ bufferSize = Infinity, windowTime = Infinity, refCount = false, scheduler } = configOrBufferSize); |
| 164 |
} else { |
| 165 |
bufferSize = (configOrBufferSize ?? Infinity) as number; |
| 166 |
} |
| 167 |
return share<T>({ |
| 168 |
connector: () => new ReplaySubject(bufferSize, windowTime, scheduler), |
| 169 |
resetOnError: true, |
| 170 |
resetOnComplete: false, |
| 171 |
resetOnRefCountZero: refCount, |
| 172 |
}); |
| 173 |
} |