Skip to content

Commit 34f0b36

Browse files
committed
refactore(ViewportCollector): remove private scroll state abstraction
1 parent 4b5321a commit 34f0b36

File tree

4 files changed

+38
-107
lines changed

4 files changed

+38
-107
lines changed

lib/ViewportCollector.tsx

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,13 @@ import raf from 'raf';
44

55
import {
66
shallowEqualScroll,
7-
shallowEqualPrivateScroll,
87
shallowEqualDimensions,
98
browserSupportsPassiveEvents,
109
simpleDebounce,
1110
debounceOnUpdate,
1211
} from './utils';
1312

14-
import {
15-
IDimensions,
16-
IPrivateScroll,
17-
IScroll,
18-
IViewport,
19-
OnUpdateType,
20-
} from './types';
21-
22-
export const SCROLL_DIR_DOWN = Symbol('SCROLL_DIR_DOWN');
23-
export const SCROLL_DIR_UP = Symbol('SCROLL_DIR_UP');
24-
export const SCROLL_DIR_LEFT = Symbol('SCROLL_DIR_LEFT');
25-
export const SCROLL_DIR_RIGHT = Symbol('SCROLL_DIR_RIGHT');
13+
import { IDimensions, IScroll, IViewport, OnUpdateType } from './types';
2614

2715
const getNodeScroll = (elem = window) => {
2816
let { scrollX, scrollY } = elem;
@@ -64,51 +52,39 @@ const getClientDimensions = (): IDimensions => {
6452
};
6553
};
6654

67-
const getXDir = (x: number, prev: IPrivateScroll) => {
55+
const isScrollingLeft = (x: number, prev: IScroll) => {
6856
switch (true) {
6957
case x < prev.x:
70-
return SCROLL_DIR_LEFT;
58+
return true;
7159
case x > prev.x:
72-
return SCROLL_DIR_RIGHT;
60+
return false;
7361
case x === prev.x:
74-
return prev.xDir;
62+
return prev.isScrollingLeft;
7563
default:
76-
throw new Error('Could not calculate xDir');
64+
throw new Error('Could not calculate isScrollingLeft');
7765
}
7866
};
7967

80-
const getYDir = (y: number, prev: IPrivateScroll) => {
68+
const isScrollingUp = (y: number, prev: IScroll) => {
8169
switch (true) {
8270
case y < prev.y:
83-
return SCROLL_DIR_UP;
71+
return true;
8472
case y > prev.y:
85-
return SCROLL_DIR_DOWN;
73+
return false;
8674
case y === prev.y:
87-
return prev.yDir;
75+
return prev.isScrollingUp;
8876
default:
8977
throw new Error('Could not calculate yDir');
9078
}
9179
};
9280

93-
const privateToPublicScroll = ({
94-
yDir,
95-
xDir,
96-
...scroll
97-
}: IPrivateScroll): IScroll => {
98-
return {
99-
...scroll,
100-
isScrollingUp: yDir === SCROLL_DIR_UP,
101-
isScrollingDown: yDir === SCROLL_DIR_DOWN,
102-
isScrollingLeft: xDir === SCROLL_DIR_LEFT,
103-
isScrollingRight: xDir === SCROLL_DIR_RIGHT,
104-
};
105-
};
106-
107-
const createInitPrivateScrollState = () => ({
81+
export const createInitScrollState = () => ({
10882
x: 0,
10983
y: 0,
110-
xDir: undefined,
111-
yDir: undefined,
84+
isScrollingUp: false,
85+
isScrollingDown: false,
86+
isScrollingLeft: false,
87+
isScrollingRight: false,
11288
xTurn: 0,
11389
yTurn: 0,
11490
xDTurn: 0,
@@ -126,9 +102,6 @@ const createEmptyDimensionState = (): IDimensions => ({
126102
documentHeight: 0,
127103
});
128104

129-
export const createInitScrollState = (): IScroll =>
130-
privateToPublicScroll(createInitPrivateScrollState());
131-
132105
export const createInitDimensionsState = (): IDimensions => {
133106
if (typeof window === 'undefined') {
134107
return createEmptyDimensionState();
@@ -142,9 +115,9 @@ interface IProps {
142115
}
143116

144117
export default class ViewportCollector extends React.PureComponent<IProps> {
145-
private scrollState: IPrivateScroll;
118+
private scrollState: IScroll;
146119
private dimensionsState: IDimensions;
147-
private lastSyncedScrollState: IPrivateScroll;
120+
private lastSyncedScrollState: IScroll;
148121
private lastSyncedDimensionsState: IDimensions;
149122
private tickId: NodeJS.Timer;
150123
private componentMightHaveUpdated: boolean;
@@ -154,7 +127,7 @@ export default class ViewportCollector extends React.PureComponent<IProps> {
154127
this.state = {
155128
parentProviderExists: false,
156129
};
157-
this.scrollState = createInitPrivateScrollState();
130+
this.scrollState = createInitScrollState();
158131
this.dimensionsState = createInitDimensionsState();
159132
this.lastSyncedDimensionsState = { ...this.dimensionsState };
160133
this.lastSyncedScrollState = { ...this.scrollState };
@@ -189,17 +162,22 @@ export default class ViewportCollector extends React.PureComponent<IProps> {
189162
handleScroll = () => {
190163
const { x, y } = getNodeScroll();
191164
const {
192-
xDir: prevXDir,
193-
yDir: prevYDir,
165+
isScrollingLeft: prevIsScrollingLeft,
166+
isScrollingUp: prevIsScrollingUp,
194167
xTurn: prevXTurn,
195168
yTurn: prevYTurn,
196169
} = this.scrollState;
197170

198-
this.scrollState.xDir = getXDir(x, this.scrollState);
199-
this.scrollState.yDir = getYDir(y, this.scrollState);
171+
this.scrollState.isScrollingLeft = isScrollingLeft(x, this.scrollState);
172+
this.scrollState.isScrollingRight = !this.scrollState.isScrollingLeft;
173+
174+
this.scrollState.isScrollingUp = isScrollingUp(y, this.scrollState);
175+
this.scrollState.isScrollingDown = !this.scrollState.isScrollingUp;
200176

201-
this.scrollState.xTurn = this.scrollState.xDir === prevXDir ? prevXTurn : x;
202-
this.scrollState.yTurn = this.scrollState.yDir === prevYDir ? prevYTurn : y;
177+
this.scrollState.xTurn =
178+
this.scrollState.isScrollingLeft === prevIsScrollingLeft ? prevXTurn : x;
179+
this.scrollState.yTurn =
180+
this.scrollState.isScrollingUp === prevIsScrollingUp ? prevYTurn : y;
203181

204182
this.scrollState.xDTurn = x - this.scrollState.xTurn;
205183
this.scrollState.yDTurn = y - this.scrollState.yTurn;
@@ -217,17 +195,17 @@ export default class ViewportCollector extends React.PureComponent<IProps> {
217195
}, 80);
218196

219197
getPublicScroll: ((scroll: IScroll) => IScroll) = memoize(
220-
(scroll: IScroll): IScroll => scroll,
198+
(scroll: IScroll): IScroll => ({ ...scroll }),
221199
shallowEqualScroll,
222200
);
223201

224202
getPublicDimensions: ((dimensions: IDimensions) => IDimensions) = memoize(
225-
(dimensions: IDimensions): IDimensions => dimensions,
203+
(dimensions: IDimensions): IDimensions => ({ ...dimensions }),
226204
shallowEqualDimensions,
227205
);
228206

229207
syncState = () => {
230-
const scrollDidUpdate = !shallowEqualPrivateScroll(
208+
const scrollDidUpdate = !shallowEqualScroll(
231209
this.lastSyncedScrollState,
232210
this.scrollState,
233211
);
@@ -265,12 +243,8 @@ export default class ViewportCollector extends React.PureComponent<IProps> {
265243

266244
getPropsFromState(): IViewport {
267245
return {
268-
scroll: this.getPublicScroll(
269-
privateToPublicScroll(this.lastSyncedScrollState),
270-
),
271-
dimensions: this.getPublicDimensions({
272-
...this.lastSyncedDimensionsState,
273-
}),
246+
scroll: this.getPublicScroll(this.lastSyncedScrollState),
247+
dimensions: this.getPublicDimensions(this.lastSyncedDimensionsState),
274248
};
275249
}
276250

lib/ViewportProvider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class ViewportProvider extends React.PureComponent<
4444
}
4545

4646
triggerUpdateToListeners = (
47-
publicState: IViewport,
47+
state: IViewport,
4848
{ scrollDidUpdate, dimensionsDidUpdate }: IViewportCollectorUpdateOptions,
4949
options?: { isIdle: boolean },
5050
) => {
@@ -62,15 +62,15 @@ export default class ViewportProvider extends React.PureComponent<
6262
const layouts = updatableListeners.map(
6363
({ recalculateLayoutBeforeUpdate }) => {
6464
if (recalculateLayoutBeforeUpdate) {
65-
return recalculateLayoutBeforeUpdate(publicState);
65+
return recalculateLayoutBeforeUpdate(state);
6666
}
6767
return null;
6868
},
6969
);
7070

7171
updatableListeners.forEach(({ handler }, index) => {
7272
const layout = layouts[index];
73-
handler(publicState, layout);
73+
handler(state, layout);
7474
});
7575
};
7676

lib/types.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
import {
2-
SCROLL_DIR_LEFT,
3-
SCROLL_DIR_RIGHT,
4-
SCROLL_DIR_UP,
5-
SCROLL_DIR_DOWN,
6-
} from './ViewportCollector';
7-
81
export interface IDimensions {
92
width: number;
103
height: number;
@@ -29,17 +22,6 @@ export interface IScroll {
2922
isScrollingRight: boolean;
3023
}
3124

32-
export interface IPrivateScroll {
33-
x: number;
34-
y: number;
35-
xDir: typeof SCROLL_DIR_LEFT | typeof SCROLL_DIR_RIGHT | undefined;
36-
yDir: typeof SCROLL_DIR_UP | typeof SCROLL_DIR_DOWN | undefined;
37-
xTurn: number;
38-
yTurn: number;
39-
xDTurn: number;
40-
yDTurn: number;
41-
}
42-
4325
export interface IRect {
4426
top: number;
4527
right: number;

lib/utils.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
IRect,
3-
IScroll,
4-
IPrivateScroll,
5-
IDimensions,
6-
OnUpdateType,
7-
} from './types';
1+
import { IRect, IScroll, IDimensions, OnUpdateType } from './types';
82

93
export const shallowEqualScroll = (a: IScroll, b: IScroll) => {
104
if (a === b) {
@@ -24,25 +18,6 @@ export const shallowEqualScroll = (a: IScroll, b: IScroll) => {
2418
);
2519
};
2620

27-
export const shallowEqualPrivateScroll = (
28-
a: IPrivateScroll,
29-
b: IPrivateScroll,
30-
) => {
31-
if (a === b) {
32-
return true;
33-
}
34-
return (
35-
a.x === b.x &&
36-
a.y === b.y &&
37-
a.xTurn === b.xTurn &&
38-
a.yTurn === b.yTurn &&
39-
a.xDTurn === b.xDTurn &&
40-
a.yDTurn === b.yDTurn &&
41-
a.xDir === b.xDir &&
42-
a.yDir === b.yDir
43-
);
44-
};
45-
4621
export const shallowEqualRect = (a: IRect, b: IRect) => {
4722
if (a === b) {
4823
return true;

0 commit comments

Comments
 (0)