Skip to content

Commit 2e00c1c

Browse files
committed
(refactor) throttle foldCheck for viewability during scrolling instead of debounce
1 parent 7481c4d commit 2e00c1c

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

examples/apps/infinite-scrolling/app.js

Lines changed: 1 addition & 1 deletion
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 "debounce";
4+
import debounce from "lodash.debounce";
55
import {Bling as Gpt, Events} from "react-gpt"; // eslint-disable-line import/no-unresolved
66
import "../log";
77
import Content from "./content";

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232
"lib"
3333
],
3434
"dependencies": {
35-
"debounce": "^1.0.0",
3635
"deep-equal": "^1.0.1",
3736
"eventemitter3": "^2.0.2",
3837
"fbjs": "^0.8.1",
39-
"hoist-non-react-statics": "^1.0.5"
38+
"hoist-non-react-statics": "^1.0.5",
39+
"lodash.debounce": "^4.0.8",
40+
"lodash.throttle": "^4.1.1"
4041
},
4142
"devDependencies": {
4243
"babel-cli": "^6.5.1",

src/createManager.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import EventEmitter from "eventemitter3";
2-
import debounce from "debounce";
2+
import debounce from "lodash.debounce";
3+
import throttle from "lodash.throttle";
34
import invariant from "fbjs/lib/invariant";
45
import {canUseDOM} from "fbjs/lib/ExecutionEnvironment";
56
import Events from "./Events";
@@ -131,14 +132,17 @@ export class AdManager extends EventEmitter {
131132
});
132133
}
133134

134-
_foldCheck = debounce(event => {
135+
_foldCheck = throttle(event => {
135136
const instances = this.getMountedInstances();
136137
instances.forEach(instance => {
137138
if (instance.getRenderWhenViewable()) {
138139
instance.foldCheck(event);
139140
}
140141
});
141-
}, 66)
142+
}, 20, {
143+
leading: true,
144+
trailing: true
145+
})
142146

143147
_handleMediaQueryChange = event => {
144148
if (this._syncCorrelator) {

test/createManager.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ describe("createManager", () => {
399399
adManager.render();
400400
});
401401

402-
it("debounces foldCheck", (done) => {
402+
it("throttles foldCheck", (done) => {
403403
const instance = {
404404
props: {
405405
sizeMapping: [{viewport: [0, 0], slot: [320, 50]}]
@@ -427,8 +427,8 @@ describe("createManager", () => {
427427
adManager.addInstance(instance2);
428428

429429
setTimeout(() => {
430-
expect(foldCheck.calledOnce).to.be.true;
431-
expect(foldCheck2.calledOnce).to.be.false;
430+
expect(foldCheck.calledTwice).to.be.true;
431+
expect(foldCheck2.notCalled).to.be.true;
432432
foldCheck.restore();
433433
foldCheck2.restore();
434434
adManager.removeInstance(instance);

0 commit comments

Comments
 (0)