Skip to content

Commit baed383

Browse files
committed
feat(skip): add higher-order lettable version of skip
1 parent 7bb8280 commit baed383

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
lines changed

src/operator/skip.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Operator } from '../Operator';
2-
import { Subscriber } from '../Subscriber';
31
import { Observable } from '../Observable';
4-
import { TeardownLogic } from '../Subscription';
2+
import { skip as higherOrder } from '../operators/skip';
53

64
/**
75
* Returns an Observable that skips the first `count` items emitted by the source Observable.
@@ -15,33 +13,5 @@ import { TeardownLogic } from '../Subscription';
1513
* @owner Observable
1614
*/
1715
export function skip<T>(this: Observable<T>, count: number): Observable<T> {
18-
return this.lift(new SkipOperator(count));
19-
}
20-
21-
class SkipOperator<T> implements Operator<T, T> {
22-
constructor(private total: number) {
23-
}
24-
25-
call(subscriber: Subscriber<T>, source: any): TeardownLogic {
26-
return source.subscribe(new SkipSubscriber(subscriber, this.total));
27-
}
28-
}
29-
30-
/**
31-
* We need this JSDoc comment for affecting ESDoc.
32-
* @ignore
33-
* @extends {Ignored}
34-
*/
35-
class SkipSubscriber<T> extends Subscriber<T> {
36-
count: number = 0;
37-
38-
constructor(destination: Subscriber<T>, private total: number) {
39-
super(destination);
40-
}
41-
42-
protected _next(x: T) {
43-
if (++this.count > this.total) {
44-
this.destination.next(x);
45-
}
46-
}
16+
return higherOrder(count)(this);
4717
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export { race } from './race';
6060
export { reduce } from './reduce';
6161
export { refCount } from './refCount';
6262
export { scan } from './scan';
63+
export { skip } from './skip';
6364
export { subscribeOn } from './subscribeOn';
6465
export { switchAll } from './switchAll';
6566
export { switchMap } from './switchMap';

src/operators/skip.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Operator } from '../Operator';
2+
import { Subscriber } from '../Subscriber';
3+
import { Observable } from '../Observable';
4+
import { TeardownLogic } from '../Subscription';
5+
import { MonoTypeOperatorFunction } from '../interfaces';
6+
7+
/**
8+
* Returns an Observable that skips the first `count` items emitted by the source Observable.
9+
*
10+
* <img src="./img/skip.png" width="100%">
11+
*
12+
* @param {Number} count - The number of times, items emitted by source Observable should be skipped.
13+
* @return {Observable} An Observable that skips values emitted by the source Observable.
14+
*
15+
* @method skip
16+
* @owner Observable
17+
*/
18+
export function skip<T>(count: number): MonoTypeOperatorFunction<T> {
19+
return (source: Observable<T>) => source.lift(new SkipOperator(count));
20+
}
21+
22+
class SkipOperator<T> implements Operator<T, T> {
23+
constructor(private total: number) {
24+
}
25+
26+
call(subscriber: Subscriber<T>, source: any): TeardownLogic {
27+
return source.subscribe(new SkipSubscriber(subscriber, this.total));
28+
}
29+
}
30+
31+
/**
32+
* We need this JSDoc comment for affecting ESDoc.
33+
* @ignore
34+
* @extends {Ignored}
35+
*/
36+
class SkipSubscriber<T> extends Subscriber<T> {
37+
count: number = 0;
38+
39+
constructor(destination: Subscriber<T>, private total: number) {
40+
super(destination);
41+
}
42+
43+
protected _next(x: T) {
44+
if (++this.count > this.total) {
45+
this.destination.next(x);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)