| 1 |
import { Subscription } from '../Subscription'; |
| 2 |
|
| 3 |
import { MonoTypeOperatorFunction, ObservableInput } from '../types'; |
| 4 |
import { operate } from '../util/lift'; |
| 5 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 6 |
import { innerFrom } from '../observable/innerFrom'; |
| 7 |
|
| 8 |
/** |
| 9 |
* An object interface used by {@link throttle} or {@link throttleTime} that ensure |
| 10 |
* configuration options of these operators. |
| 11 |
* |
| 12 |
* @see {@link throttle} |
| 13 |
* @see {@link throttleTime} |
| 14 |
*/ |
| 15 |
export interface ThrottleConfig { |
| 16 |
/** |
| 17 |
* If `true`, the resulting Observable will emit the first value from the source |
| 18 |
* Observable at the **start** of the "throttling" process (when starting an |
| 19 |
* internal timer that prevents other emissions from the source to pass through). |
| 20 |
* If `false`, it will not emit the first value from the source Observable at the |
| 21 |
* start of the "throttling" process. |
| 22 |
* |
| 23 |
* If not provided, defaults to: `true`. |
| 24 |
*/ |
| 25 |
leading?: boolean; |
| 26 |
/** |
| 27 |
* If `true`, the resulting Observable will emit the last value from the source |
| 28 |
* Observable at the **end** of the "throttling" process (when ending an internal |
| 29 |
* timer that prevents other emissions from the source to pass through). |
| 30 |
* If `false`, it will not emit the last value from the source Observable at the |
| 31 |
* end of the "throttling" process. |
| 32 |
* |
| 33 |
* If not provided, defaults to: `false`. |
| 34 |
*/ |
| 35 |
trailing?: boolean; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Emits a value from the source Observable, then ignores subsequent source |
| 40 |
* values for a duration determined by another Observable, then repeats this |
| 41 |
* process. |
| 42 |
* |
| 43 |
* <span class="informal">It's like {@link throttleTime}, but the silencing |
| 44 |
* duration is determined by a second Observable.</span> |
| 45 |
* |
| 46 |
*  |
| 47 |
* |
| 48 |
* `throttle` emits the source Observable values on the output Observable |
| 49 |
* when its internal timer is disabled, and ignores source values when the timer |
| 50 |
* is enabled. Initially, the timer is disabled. As soon as the first source |
| 51 |
* value arrives, it is forwarded to the output Observable, and then the timer |
| 52 |
* is enabled by calling the `durationSelector` function with the source value, |
| 53 |
* which returns the "duration" Observable. When the duration Observable emits a |
| 54 |
* value, the timer is disabled, and this process repeats for the |
| 55 |
* next source value. |
| 56 |
* |
| 57 |
* ## Example |
| 58 |
* |
| 59 |
* Emit clicks at a rate of at most one click per second |
| 60 |
* |
| 61 |
* ```ts |
| 62 |
* import { fromEvent, throttle, interval } from 'rxjs'; |
| 63 |
* |
| 64 |
* const clicks = fromEvent(document, 'click'); |
| 65 |
* const result = clicks.pipe(throttle(() => interval(1000))); |
| 66 |
* |
| 67 |
* result.subscribe(x => console.log(x)); |
| 68 |
* ``` |
| 69 |
* |
| 70 |
* @see {@link audit} |
| 71 |
* @see {@link debounce} |
| 72 |
* @see {@link delayWhen} |
| 73 |
* @see {@link sample} |
| 74 |
* @see {@link throttleTime} |
| 75 |
* |
| 76 |
* @param durationSelector A function that receives a value from the source |
| 77 |
* Observable, for computing the silencing duration for each source value, |
| 78 |
* returned as an `ObservableInput`. |
| 79 |
* @param config A configuration object to define `leading` and `trailing` |
| 80 |
* behavior. Defaults to `{ leading: true, trailing: false }`. |
| 81 |
* @return A function that returns an Observable that performs the throttle |
| 82 |
* operation to limit the rate of emissions from the source. |
| 83 |
*/ |
| 84 |
export function throttle<T>(durationSelector: (value: T) => ObservableInput<any>, config?: ThrottleConfig): MonoTypeOperatorFunction<T> { |
| 85 |
return operate((source, subscriber) => { |
| 86 |
const { leading = true, trailing = false } = config ?? {}; |
| 87 |
let hasValue = false; |
| 88 |
let sendValue: T | null = null; |
| 89 |
let throttled: Subscription | null = null; |
| 90 |
let isComplete = false; |
| 91 |
|
| 92 |
const endThrottling = () => { |
| 93 |
throttled?.unsubscribe(); |
| 94 |
throttled = null; |
| 95 |
if (trailing) { |
| 96 |
send(); |
| 97 |
isComplete && subscriber.complete(); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
const cleanupThrottling = () => { |
| 102 |
throttled = null; |
| 103 |
isComplete && subscriber.complete(); |
| 104 |
}; |
| 105 |
|
| 106 |
const startThrottle = (value: T) => |
| 107 |
(throttled = innerFrom(durationSelector(value)).subscribe(createOperatorSubscriber(subscriber, endThrottling, cleanupThrottling))); |
| 108 |
|
| 109 |
const send = () => { |
| 110 |
if (hasValue) { |
| 111 |
// Ensure we clear out our value and hasValue flag |
| 112 |
// before we emit, otherwise reentrant code can cause |
| 113 |
// issues here. |
| 114 |
hasValue = false; |
| 115 |
const value = sendValue!; |
| 116 |
sendValue = null; |
| 117 |
// Emit the value. |
| 118 |
subscriber.next(value); |
| 119 |
!isComplete && startThrottle(value); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
source.subscribe( |
| 124 |
createOperatorSubscriber( |
| 125 |
subscriber, |
| 126 |
// Regarding the presence of throttled.closed in the following |
| 127 |
// conditions, if a synchronous duration selector is specified - weird, |
| 128 |
// but legal - an already-closed subscription will be assigned to |
| 129 |
// throttled, so the subscription's closed property needs to be checked, |
| 130 |
// too. |
| 131 |
(value) => { |
| 132 |
hasValue = true; |
| 133 |
sendValue = value; |
| 134 |
!(throttled && !throttled.closed) && (leading ? send() : startThrottle(value)); |
| 135 |
}, |
| 136 |
() => { |
| 137 |
isComplete = true; |
| 138 |
!(trailing && hasValue && throttled && !throttled.closed) && subscriber.complete(); |
| 139 |
} |
| 140 |
) |
| 141 |
); |
| 142 |
}); |
| 143 |
} |