Skip to content

Commit 4756389

Browse files
committed
reduce some redundancy for smaller build
1 parent 34ab21d commit 4756389

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/index.js

+22-31
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import {
77
} from 'react';
88

99
function getVpWidth() {
10-
return (typeof window != 'undefined') ? Math.max(
11-
window.document.documentElement.clientWidth,
12-
window.innerWidth || 0
13-
) : 0;
10+
return Math.max(
11+
globalThis?.document?.documentElement?.clientWidth || 0,
12+
globalThis?.innerWidth || 0
13+
);
1414
}
1515

1616

1717
function getVpHeight() {
18-
return (typeof window != 'undefined') ? Math.max(
19-
window.document.documentElement.clientHeight,
20-
window.innerHeight || 0
21-
) : 0;
18+
return Math.max(
19+
globalThis?.document?.documentElement?.clientHeight || 0,
20+
globalThis?.innerHeight || 0
21+
);
2222
}
2323

2424
// Avoid useLayoutEffect warning during SSR
@@ -68,14 +68,11 @@ let vpHeight = getVpHeight();
6868
*/
6969
function triggerResizeListener(listener, vpWidth, vpHeight) {
7070
const params = { vpW: vpWidth, vpH: vpHeight };
71-
72-
let shouldRun = false;
7371
let hash;
7472

7573
const { options, prevHash=undefined } = resolverMap?.get(listener) || {};
76-
const { hasher } = options;
7774

78-
if(!hasher) {
75+
if(!options.hasher) {
7976
switch (options?.dimension) {
8077
case 'w':
8178
hash = vpWidth;
@@ -89,18 +86,17 @@ function triggerResizeListener(listener, vpWidth, vpHeight) {
8986
}
9087
}
9188
else {
92-
hash = hasher(params);
89+
hash = options.hasher(params);
9390
}
9491

95-
if(hash != prevHash) { shouldRun = true }
96-
97-
if(shouldRun) {
92+
if(hash != prevHash) {
9893
const state = { ...params, options, hash };
9994
resolverMap.set(listener, {
10095
options,
10196
prevHash: hash,
10297
prevState: state
10398
});
99+
104100
listener(state);
105101
}
106102
}
@@ -125,13 +121,6 @@ function onResize() {
125121
// the Hook //
126122
// =============== //
127123

128-
function getInitialState(options, vpW, vpH) {
129-
return (!options.hasher ?
130-
{ vpW, vpH } :
131-
options.hasher({ vpW: vpWidth, vpH: vpHeight })
132-
)
133-
}
134-
135124
export default function useViewportSizes(input) {
136125
const hasher = ((typeof input == 'function') ?
137126
input :
@@ -155,7 +144,10 @@ export default function useViewportSizes(input) {
155144
hasher
156145
};
157146

158-
const [state, setState] = useState(() => getInitialState(options));
147+
const [state, setState] = useState(() => {
148+
const defaultState = { vpW: vpWidth, vpH: vpHeight };
149+
return options.hasher ? options.hasher(defaultState) : defaultState;
150+
});
159151
const debounceTimeoutRef = useRef(undefined);
160152
const throttleTimeoutRef = useRef(undefined);
161153
const lastThrottledRef = useRef(undefined);
@@ -246,25 +238,24 @@ export default function useViewportSizes(input) {
246238
switch (dimension) {
247239
default:
248240
case 'both': {
249-
dimensionHash = `${state?.vpW}_${state.vpH}`;
241+
dimensionHash = `${state.vpW}_${state.vpH}`;
250242
break;
251243
}
252244
case 'w': {
253-
dimensionHash = state?.vpW || 0;
245+
dimensionHash = state.vpW || 0;
254246
break;
255247
}
256248
case 'h': {
257-
dimensionHash = state?.vpH || 0;
249+
dimensionHash = state.vpH || 0;
258250
break;
259251
}
260252
}
261253

262254
const returnValue = useMemo(() => {
263255
switch (dimension) {
264-
default:
265-
case 'both': { return [state?.vpW || 0, state?.vpH || 0, onResize] }
266-
case 'w': { return [state?.vpW || 0, onResize] }
267-
case 'h': { return [state?.vpH || 0, onResize] }
256+
default: { return [state.vpW || 0, state.vpH || 0, onResize] }
257+
case 'w': { return [state.vpW || 0, onResize] }
258+
case 'h': { return [state.vpH || 0, onResize] }
268259
}
269260
}, [dimensionHash, onResize, dimension]);
270261

0 commit comments

Comments
 (0)