Skip to content

Commit a780bf2

Browse files
committed
feat(timestamp): add higher-order lettable version of timestamp
1 parent 82480cf commit a780bf2

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

src/Rx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export {ObjectUnsubscribedError} from './util/ObjectUnsubscribedError';
158158
export {TimeoutError} from './util/TimeoutError';
159159
export {UnsubscriptionError} from './util/UnsubscriptionError';
160160
export {TimeInterval} from './operator/timeInterval';
161-
export {Timestamp} from './operator/timestamp';
161+
export {Timestamp} from './operators/timestamp';
162162
export {TestScheduler} from './testing/TestScheduler';
163163
export {VirtualTimeScheduler} from './scheduler/VirtualTimeScheduler';
164164
export {AjaxRequest, AjaxResponse, AjaxError, AjaxTimeoutError} from './observable/dom/AjaxObservable';

src/operator/timestamp.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
1-
import { Operator } from '../Operator';
21
import { Observable } from '../Observable';
3-
import { Subscriber } from '../Subscriber';
42
import { IScheduler } from '../Scheduler';
53
import { async } from '../scheduler/async';
6-
4+
import { timestamp as higherOrder } from '../operators';
5+
import { Timestamp } from '../operators/timestamp';
76
/**
87
* @param scheduler
98
* @return {Observable<Timestamp<any>>|WebSocketSubject<T>|Observable<T>}
109
* @method timestamp
1110
* @owner Observable
1211
*/
1312
export function timestamp<T>(this: Observable<T>, scheduler: IScheduler = async): Observable<Timestamp<T>> {
14-
return this.lift(new TimestampOperator(scheduler));
15-
}
16-
17-
export class Timestamp<T> {
18-
constructor(public value: T, public timestamp: number) {
19-
}
20-
};
21-
22-
class TimestampOperator<T> implements Operator<T, Timestamp<T>> {
23-
constructor(private scheduler: IScheduler) {
24-
}
25-
26-
call(observer: Subscriber<Timestamp<T>>, source: any): any {
27-
return source.subscribe(new TimestampSubscriber(observer, this.scheduler));
28-
}
29-
}
30-
31-
class TimestampSubscriber<T> extends Subscriber<T> {
32-
constructor(destination: Subscriber<Timestamp<T>>, private scheduler: IScheduler) {
33-
super(destination);
34-
}
35-
36-
protected _next(value: T): void {
37-
const now = this.scheduler.now();
38-
39-
this.destination.next(new Timestamp(value, now));
40-
}
13+
return higherOrder(scheduler)(this);
4114
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export { switchAll } from './switchAll';
2222
export { switchMap } from './switchMap';
2323
export { takeLast } from './takeLast';
2424
export { tap } from './tap';
25+
export { timestamp } from './timestamp';
2526
export { toArray } from './toArray';
2627
export { window } from './window';
2728
export { windowCount } from './windowCount';

src/operators/timestamp.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import { IScheduler } from '../Scheduler';
3+
import { async } from '../scheduler/async';
4+
import { OperatorFunction } from '../interfaces';
5+
import { map } from './map';
6+
7+
/**
8+
* @param scheduler
9+
* @return {Observable<Timestamp<any>>|WebSocketSubject<T>|Observable<T>}
10+
* @method timestamp
11+
* @owner Observable
12+
*/
13+
export function timestamp<T>(scheduler: IScheduler = async): OperatorFunction<T, Timestamp<T>> {
14+
return map((value: T) => new Timestamp(value, scheduler.now()));
15+
// return (source: Observable<T>) => source.lift(new TimestampOperator(scheduler));
16+
}
17+
18+
export class Timestamp<T> {
19+
constructor(public value: T, public timestamp: number) {
20+
}
21+
};

0 commit comments

Comments
 (0)