Skip to content

Commit 81e468a

Browse files
committed
(refactor) use throttle-debounce and improve throttle tests
1 parent 2e00c1c commit 81e468a

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

examples/apps/infinite-scrolling/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable react/sort-comp */
22
import React, {Component} from "react";
33
import Radium from "radium";
4-
import debounce from "lodash.debounce";
4+
import debounce from "throttle-debounce/debounce";
55
import {Bling as Gpt, Events} from "react-gpt"; // eslint-disable-line import/no-unresolved
66
import "../log";
77
import Content from "./content";
@@ -40,14 +40,14 @@ class App extends Component {
4040
window.removeEventListener("resize", this.onScroll);
4141
this.stopTimer();
4242
}
43-
onScroll = debounce(() => {
43+
onScroll = debounce(66, () => {
4444
const scrollTop = window.scrollY || document.documentElement.scrollTop;
4545
if (scrollTop + window.innerHeight >= document.body.clientHeight) {
4646
this.setState({
4747
page: ++this.state.page
4848
});
4949
}
50-
}, 66)
50+
})
5151
startTimer() {
5252
this.stopTimer();
5353
this.timer = setInterval(() => {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
"eventemitter3": "^2.0.2",
3737
"fbjs": "^0.8.1",
3838
"hoist-non-react-statics": "^1.0.5",
39-
"lodash.debounce": "^4.0.8",
40-
"lodash.throttle": "^4.1.1"
39+
"throttle-debounce": "^1.0.1"
4140
},
4241
"devDependencies": {
4342
"babel-cli": "^6.5.1",

src/createManager.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import EventEmitter from "eventemitter3";
2-
import debounce from "lodash.debounce";
3-
import throttle from "lodash.throttle";
2+
import {debounce, throttle} from "throttle-debounce";
43
import invariant from "fbjs/lib/invariant";
54
import {canUseDOM} from "fbjs/lib/ExecutionEnvironment";
65
import Events from "./Events";
@@ -132,18 +131,23 @@ export class AdManager extends EventEmitter {
132131
});
133132
}
134133

135-
_foldCheck = throttle(event => {
134+
_foldCheck = throttle(20, event => {
136135
const instances = this.getMountedInstances();
137136
instances.forEach(instance => {
138137
if (instance.getRenderWhenViewable()) {
139138
instance.foldCheck(event);
140139
}
141140
});
142-
}, 20, {
143-
leading: true,
144-
trailing: true
141+
142+
if (this.testMode) {
143+
this._getTimer();
144+
}
145145
})
146146

147+
_getTimer() {
148+
return Date.now();
149+
}
150+
147151
_handleMediaQueryChange = event => {
148152
if (this._syncCorrelator) {
149153
this.refresh();
@@ -311,7 +315,7 @@ export class AdManager extends EventEmitter {
311315
return true;
312316
}
313317

314-
render = debounce(() => {
318+
render = debounce(4, () => {
315319
if (!this._initialRender) {
316320
return;
317321
}
@@ -379,7 +383,7 @@ export class AdManager extends EventEmitter {
379383

380384
this._initialRender = false;
381385
});
382-
}, 4)
386+
})
383387

384388
/**
385389
* Re-render(not refresh) all the ads in the page and the first ad will update the correlator value.
@@ -388,7 +392,7 @@ export class AdManager extends EventEmitter {
388392
* @method renderAll
389393
* @static
390394
*/
391-
renderAll = debounce(() => {
395+
renderAll = debounce(4, () => {
392396
if (!this.apiReady) {
393397
return false;
394398
}
@@ -403,7 +407,7 @@ export class AdManager extends EventEmitter {
403407
});
404408

405409
return true;
406-
}, 4)
410+
})
407411

408412
getGPTVersion() {
409413
if (!this.apiReady) {

test/createManager.spec.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,23 +422,45 @@ describe("createManager", () => {
422422

423423
const foldCheck = sinon.stub(instance, "foldCheck");
424424
const foldCheck2 = sinon.stub(instance2, "foldCheck");
425+
const getRenderWhenViewable = sinon.spy(instance, "getRenderWhenViewable");
426+
const getRenderWhenViewable2 = sinon.spy(instance2, "getRenderWhenViewable");
427+
const managerFoldCheck = sinon.spy(adManager, "_foldCheck");
428+
const timer = sinon.spy(adManager, "_getTimer");
425429

426430
adManager.addInstance(instance);
427431
adManager.addInstance(instance2);
428432

433+
const start = Date.now();
434+
adManager._foldCheck();
435+
adManager._foldCheck();
436+
setTimeout(() => {
437+
adManager._foldCheck();
438+
}, 5);
439+
setTimeout(() => {
440+
adManager._foldCheck();
441+
}, 10);
442+
setTimeout(() => {
443+
adManager._foldCheck();
444+
}, 15);
445+
429446
setTimeout(() => {
447+
expect(managerFoldCheck.callCount).to.equal(5);
448+
expect(timer.calledTwice).to.be.true;
449+
expect(timer.returnValues[1] - timer.returnValues[0]).to.be.above(19); // timer above 20ms timeout
450+
expect(timer.returnValues[0] - start).to.be.below(5); // should start ~immediately
430451
expect(foldCheck.calledTwice).to.be.true;
431452
expect(foldCheck2.notCalled).to.be.true;
453+
432454
foldCheck.restore();
433455
foldCheck2.restore();
456+
getRenderWhenViewable.restore();
457+
getRenderWhenViewable2.restore();
458+
managerFoldCheck.restore();
459+
timer.restore()
434460
adManager.removeInstance(instance);
435461
adManager.removeInstance(instance2);
436462
done();
437463
}, 100);
438-
439-
adManager._foldCheck();
440-
adManager._foldCheck();
441-
adManager._foldCheck();
442464
});
443465

444466
it("renders all ads", (done) => {

0 commit comments

Comments
 (0)