Skip to content

Commit

Permalink
fix(throttle): properly handle default ThrottleConfig values
Browse files Browse the repository at this point in the history
  • Loading branch information
jakovljevic-mladen committed Jan 31, 2023
1 parent 7d3c4ec commit 2c8a649
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
6 changes: 3 additions & 3 deletions spec/operators/throttle-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ describe('throttle', () => {
];
const expected = '-a---y----b---x---x---x---|';

const result = e1.pipe(throttle(() => e2, { leading: true, trailing: true }));
const result = e1.pipe(throttle(() => e2, { trailing: true }));

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand All @@ -414,7 +414,7 @@ describe('throttle', () => {
const n1Subs = ['--^------------------! '];
const exp = ' --x------------------| ';

const result = s1.pipe(throttle(() => n1, { leading: true, trailing: true }));
const result = s1.pipe(throttle(() => n1, { trailing: true }));
expectObservable(result).toBe(exp);
expectSubscriptions(s1.subscriptions).toBe(s1Subs);
expectSubscriptions(n1.subscriptions).toBe(n1Subs);
Expand All @@ -433,7 +433,7 @@ describe('throttle', () => {
];
const expected = '-a--------x---(y|)';

const result = e1.pipe(throttle(() => e2, { leading: true, trailing: true }));
const result = e1.pipe(throttle(() => e2, { trailing: true }));

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand Down
42 changes: 21 additions & 21 deletions spec/operators/throttleTime-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('throttleTime operator', () => {
const expected = '-a--------b-----c----|';
const subs = ' ^--------------------!';

const result = e1.pipe(throttleTime(5, rxTest));
const result = e1.pipe(throttleTime(5));

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -31,7 +31,7 @@ describe('throttleTime operator', () => {
of(1, 2, 3)
.pipe(throttleTime(5))
.subscribe({
next: (x: number) => {
next: (x) => {
expect(x).to.equal(1);
},
complete: done,
Expand All @@ -41,17 +41,17 @@ describe('throttleTime operator', () => {
it('should throttle events multiple times', () => {
const expected = ['1-0', '2-0'];
concat(
timer(0, 1, rxTest).pipe(
timer(0, 1).pipe(
take(3),
map((x: number) => '1-' + x)
map((x) => '1-' + x)
),
timer(8, 1, rxTest).pipe(
timer(8, 1).pipe(
take(5),
map((x: number) => '2-' + x)
map((x) => '2-' + x)
)
)
.pipe(throttleTime(5, rxTest))
.subscribe((x: string) => {
.subscribe((x) => {
expect(x).to.equal(expected.shift());
});

Expand All @@ -64,7 +64,7 @@ describe('throttleTime operator', () => {
const subs = ' ^--------------------!';
const expected = '-a--------b-----c----|';

expectObservable(e1.pipe(throttleTime(5, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(5))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -75,7 +75,7 @@ describe('throttleTime operator', () => {
const subs = ' ^------------------------!';
const expected = 'a-----a-----a-----a-----a|';

expectObservable(e1.pipe(throttleTime(5, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(5))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -86,7 +86,7 @@ describe('throttleTime operator', () => {
const subs = ' ^----!';
const expected = '-----|';

expectObservable(e1.pipe(throttleTime(5, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(5))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -97,7 +97,7 @@ describe('throttleTime operator', () => {
const subs = ' ^----!';
const expected = '-----#';

expectObservable(e1.pipe(throttleTime(10, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(10))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -108,7 +108,7 @@ describe('throttleTime operator', () => {
const subs = ' (^!)';
const expected = '|';

expectObservable(e1.pipe(throttleTime(30, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(30))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -119,7 +119,7 @@ describe('throttleTime operator', () => {
const subs = ' ^';
const expected = '-';

expectObservable(e1.pipe(throttleTime(30, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(30))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -130,7 +130,7 @@ describe('throttleTime operator', () => {
const subs = ' (^!)';
const expected = '#';

expectObservable(e1.pipe(throttleTime(30, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(30))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -142,7 +142,7 @@ describe('throttleTime operator', () => {
const subs = ' ^------------------------------!';
const expected = '-a-------------d----------------';

expectObservable(e1.pipe(throttleTime(5, rxTest)), unsub).toBe(expected);
expectObservable(e1.pipe(throttleTime(5)), unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -155,9 +155,9 @@ describe('throttleTime operator', () => {
const unsub = ' -------------------------------!';

const result = e1.pipe(
mergeMap((x: string) => of(x)),
throttleTime(5, rxTest),
mergeMap((x: string) => of(x))
mergeMap((x) => of(x)),
throttleTime(5),
mergeMap((x) => of(x))
);

expectObservable(result, unsub).toBe(expected);
Expand All @@ -171,7 +171,7 @@ describe('throttleTime operator', () => {
const subs = ' ^------------------------------!';
const expected = '-a-------------d---------------#';

expectObservable(e1.pipe(throttleTime(5, rxTest))).toBe(expected);
expectObservable(e1.pipe(throttleTime(5))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
});
});
Expand All @@ -186,7 +186,7 @@ describe('throttleTime operator', () => {
// ----|----|---|---|
const expected = '-a---y----b---x---x---(x|)';

const result = e1.pipe(throttleTime(t, rxTest, { leading: true, trailing: true }));
const result = e1.pipe(throttleTime(t, rxTest, { trailing: true }));

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand All @@ -199,7 +199,7 @@ describe('throttleTime operator', () => {
const t = time(' ----| ');
const expected = '-a--------------------|';

const result = e1.pipe(throttleTime(t, rxTest, { leading: true, trailing: true }));
const result = e1.pipe(throttleTime(t, rxTest, { trailing: true }));

expectObservable(result).toBe(expected);
});
Expand Down
12 changes: 2 additions & 10 deletions src/internal/operators/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ export interface ThrottleConfig {
trailing?: boolean;
}

export const defaultThrottleConfig: ThrottleConfig = {
leading: true,
trailing: false,
};

/**
* Emits a value from the source Observable, then ignores subsequent source
* values for a duration determined by another Observable, then repeats this
Expand Down Expand Up @@ -61,12 +56,9 @@ export const defaultThrottleConfig: ThrottleConfig = {
* @return A function that returns an Observable that performs the throttle
* operation to limit the rate of emissions from the source.
*/
export function throttle<T>(
durationSelector: (value: T) => ObservableInput<any>,
config: ThrottleConfig = defaultThrottleConfig
): MonoTypeOperatorFunction<T> {
export function throttle<T>(durationSelector: (value: T) => ObservableInput<any>, config?: ThrottleConfig): MonoTypeOperatorFunction<T> {
return operate((source, subscriber) => {
const { leading, trailing } = config;
const { leading = true, trailing = false } = config ?? {};
let hasValue = false;
let sendValue: T | null = null;
let throttled: Subscription | null = null;
Expand Down
4 changes: 2 additions & 2 deletions src/internal/operators/throttleTime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { asyncScheduler } from '../scheduler/async';
import { defaultThrottleConfig, throttle } from './throttle';
import { throttle, ThrottleConfig } from './throttle';
import { MonoTypeOperatorFunction, SchedulerLike } from '../types';
import { timer } from '../observable/timer';

Expand Down Expand Up @@ -55,7 +55,7 @@ import { timer } from '../observable/timer';
export function throttleTime<T>(
duration: number,
scheduler: SchedulerLike = asyncScheduler,
config = defaultThrottleConfig
config?: ThrottleConfig
): MonoTypeOperatorFunction<T> {
const duration$ = timer(duration, scheduler);
return throttle(() => duration$, config);
Expand Down

0 comments on commit 2c8a649

Please sign in to comment.