-
Notifications
You must be signed in to change notification settings - Fork 1
/
throttle.js
84 lines (78 loc) · 2.55 KB
/
throttle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// filter, only first
const Observable = require('rxjs/Observable').Observable;
require('rxjs/add/operator/throttle');
require('rxjs/add/observable/of');
require('rxjs/add/observable/never');
const timer = require('rxjs/observable/timer').timer;
const throttle = require('rxjs/operators').throttle;
const tap = require('rxjs/operators').tap;
const flatMap = require('rxjs/operators').flatMap;
const skipWhile = require('rxjs/operators').skipWhile;
const o = Observable.create((subscriber) => {
subscriber.next('a1'); // fire
subscriber.next('a2'); // ignored
setTimeout(function () {
subscriber.next('b1'); // ignored
subscriber.next('b2'); // ignored
}, 400);
setTimeout(function () {
subscriber.next('c1'); // ignored
subscriber.next('c2'); // ignored
}, 800);
// 1000ms passed
setTimeout(function () {
subscriber.next('d1'); // fire
subscriber.next('d2'); // ignored
}, 1200); // 1200 ms passed, count another 1000ms from here
setTimeout(function () {
subscriber.next('e1'); // ignored
subscriber.next('e2'); // ignored
}, 1600);
setTimeout(function () {
subscriber.next('f1'); // ignored
subscriber.next('f2'); // ignored
}, 2000);
// 2200 ms passed
setTimeout(function () {
subscriber.next('g1'); // fire
subscriber.next('g2'); // ignored
}, 2400);
}).throttle(val => {
let t = 1000;
return timer(t);
});
o.subscribe(console.log); // a1, d1, g1
// ====================== An example that not works as expected ======================
// const api = () => Observable.create(subscriber => {
// console.log('calling api');
// timer(5000).subscribe(() => subscriber.next('emit value.'));
// });
//
// let hasValue = false;
// const request = () => Observable.of(1).pipe(
// tap(v => hasValue = false),
// flatMap(() => api()),
// tap(v => hasValue = true),
// );
//
// timer(0, 1000)
// .pipe(
// throttle(() => {
// console.error('calling throttle', hasValue);
// return hasValue ? timer(0) : timer(100);
// }),
// )
// .subscribe(() => request().subscribe(console.warn));
// ====================== Instead ======================
// let calling = false;
// const request = () => Observable.of(1).pipe(
// skipWhile(() => calling === true),
// flatMap(() => {
// calling = true;
// return api()
// }),
// tap(v => calling = false),
// );
//
// timer(0, 1000)
// .subscribe(() => request().subscribe(console.warn));