Learn RxJS
Search…
Smart Counter
An interesting element on interfaces which involve dynamically updating numbers is a smart counter, or odometer effect. Instead of jumping a number up and down, quickly counting to the desired number can achieve a cool effect. An example of a popular library that accomplishes this is odometer by Hubspot. Let's see how we can accomplish something similar with just a few lines of RxJS.

Vanilla JS

Could not load image
Smart Counter
1
import { timer, fromEvent, merge } from 'rxjs';
2
import {
3
switchMap,
4
startWith,
5
scan,
6
takeWhile,
7
takeUntil,
8
filter,
9
mapTo,
10
map,
11
tap,
12
pluck
13
} from 'rxjs/operators';
14
15
let currentNumber = 0;
16
17
// elems
18
const input: any = document.getElementById('range');
19
20
// utility functions
21
const takeUntilFunc = (endRange, currentNumber) => {
22
return endRange > currentNumber
23
? val => val <= endRange
24
: val => val >= endRange;
25
};
26
const positiveOrNegative = (endRange, currentNumber) => {
27
return endRange > currentNumber ? 1 : -1;
28
};
29
const updateHTML = id => val => (document.getElementById(id).innerHTML = val);
30
31
// streams
32
const enter$ = fromEvent(input, 'keyup').pipe(
33
pluck('code'),
34
filter(code => code === 'Enter')
35
);
36
37
enter$
38
.pipe(
39
map(() => parseInt(input.value)),
40
switchMap(endRange => {
41
return timer(0, 20).pipe(
42
mapTo(positiveOrNegative(endRange, currentNumber)),
43
startWith(currentNumber),
44
scan((acc, curr) => acc + curr),
45
takeWhile(takeUntilFunc(endRange, currentNumber))
46
);
47
}),
48
tap(v => (currentNumber = v)),
49
startWith(currentNumber)
50
)
51
.subscribe(updateHTML('display'));
Copied!

HTML

1
<div class="container">
2
<input id="range" type="number" />
3
<h1 id="display"></h1>
4
</div>
Copied!
We can easily take our vanilla smart counter and wrap it in any popular component based UI library. Below is an example of an Angular smart counter component which takes an Input of the updated end ranges and performs the appropriate transition.

Angular Version

1
import { Component, Input, OnDestroy } from '@angular/core';
2
import { Subject } from 'rxjs/Subject';
3
import { timer } from 'rxjs/observable/timer';
4
import { switchMap, startWith, scan, takeWhile, takeUntil, mapTo } from 'rxjs/operators';
5
6
@Component({
7
selector: 'number-tracker',
8
template: `
9
<h3> {{ currentNumber }}</h3>
10
`
11
})
12
export class NumberTrackerComponent implements OnDestroy {
13
@Input()
14
set end(endRange: number) {
15
this._counterSub$.next(endRange);
16
}
17
@Input() countInterval = 20;
18
public currentNumber = 0;
19
private _counterSub$ = new Subject();
20
private _onDestroy$ = new Subject();
21
22
constructor() {
23
this._counterSub$
24
.pipe(
25
switchMap(endRange => {
26
return timer(0, this.countInterval).pipe(
27
mapTo(this.positiveOrNegative(endRange, this.currentNumber)),
28
startWith(this.currentNumber),
29
scan((acc: number, curr: number) => acc + curr),
30
takeWhile(this.isApproachingRange(endRange, this.currentNumber))
31
)
32
}),
33
takeUntil(this._onDestroy$)
34
)
35
.subscribe((val: number) => this.currentNumber = val);
36
}
37
38
private positiveOrNegative(endRange, currentNumber) {
39
return endRange > currentNumber ? 1 : -1;
40
}
41
42
private isApproachingRange(endRange, currentNumber) {
43
return endRange > currentNumber
44
? val => val <= endRange
45
: val => val >= endRange;
46
}
47
48
ngOnDestroy() {
49
this._onDestroy$.next();
50
this._onDestroy$.complete();
51
}
52
}
Copied!

HTML

1
<p>
2
<input
3
type="number"
4
(keyup.enter)="counterNumber = vanillaInput.value"
5
#vanillaInput
6
/>
7
<button (click)="counterNumber = vanillaInput.value">
8
Update number
9
</button>
10
</p>
11
<number-tracker [end]="counterNumber"></number-tracker>
Copied!

Operators Used

Last modified 1yr ago