Skip to content

Commit

Permalink
feat(ViewportProvider): minor improvements to reduce garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
garthenweb committed Sep 22, 2018
1 parent f4c8906 commit 674d03f
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/ViewportProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default class ViewportProvider extends React.PureComponent {
private lastSyncedScrollState: IPrivateScroll;
private lastSyncedDimensionsState: IDimensions;
private tickId: NodeJS.Timer;
private componentMightHaveUpdated: boolean;
private listeners: IListener[] = [];

constructor(props: {}) {
Expand All @@ -144,7 +145,8 @@ export default class ViewportProvider extends React.PureComponent {
window.addEventListener('scroll', this.handleScroll, false);
window.addEventListener('resize', this.handleResize, false);
window.addEventListener('orientationchange', this.handleResize, false);
this.tick(this.syncState);

this.tickId = raf(this.tick);
}

componentWillUnmount() {
Expand All @@ -155,6 +157,16 @@ export default class ViewportProvider extends React.PureComponent {
this.listeners = [];
}

tick = () => {
if (this) {
if (this.componentMightHaveUpdated) {
this.syncState();
}
this.componentMightHaveUpdated = false;
this.tickId = raf(this.tick);
}
};

handleScroll = throttle(
() => {
const { x, y } = getNodeScroll();
Expand All @@ -178,6 +190,8 @@ export default class ViewportProvider extends React.PureComponent {

this.scrollState.x = x;
this.scrollState.y = y;

this.componentMightHaveUpdated = true;
},
16,
{
Expand All @@ -190,6 +204,8 @@ export default class ViewportProvider extends React.PureComponent {
const { width, height } = getClientDimensions();
this.dimensionsState.width = width;
this.dimensionsState.height = height;

this.componentMightHaveUpdated = true;
}, 80);

getPublicScroll: ((scroll: IScroll) => IScroll) = memoize(
Expand All @@ -202,15 +218,6 @@ export default class ViewportProvider extends React.PureComponent {
shallowEqualDimensions,
);

tick(updater: () => void) {
this.tickId = raf(() => {
if (this) {
updater();
this.tick(updater);
}
});
}

syncState = () => {
const scrollDidUpdate = !shallowEqualPrivateScroll(
this.lastSyncedScrollState,
Expand Down

0 comments on commit 674d03f

Please sign in to comment.