@@ -44,10 +44,13 @@ const resolverMap = new Map();
44
44
let vpWidth = getVpWidth ( ) ;
45
45
let vpHeight = getVpHeight ( ) ;
46
46
47
- // should only be called by *one* component once;
48
- // will iterate through all subscribers
49
- // afterwards
50
-
47
+ /**
48
+ * called to update resize dimensions;
49
+ * only called once throughout hooks so if
50
+ * using SSR, may be expensive to trigger in all
51
+ * components with effect on paint as described in
52
+ * readme
53
+ */
51
54
function onResize ( ) {
52
55
vpWidth = getVpWidth ( ) ;
53
56
vpHeight = getVpHeight ( ) ;
@@ -97,12 +100,12 @@ function useViewportSizes(input) {
97
100
input ?. hasher
98
101
) || undefined ;
99
102
100
- const debounceTimeout = input ?. debounceTimeout || undefined ;
101
-
102
- const throttleTimeout = ( ( typeof input == 'number' ) ?
103
+ const debounceTimeout = ( ( typeof input == 'number' ) ?
103
104
input :
104
- input ?. throttleTimeout
105
- ) || undefined ;
105
+ input ?. debounceTimeout
106
+ ) || 0 ;
107
+
108
+ const throttleTimeout = input ?. throttleTimeout || 0 ;
106
109
107
110
const dimension = input ?. dimension || 'both' ;
108
111
@@ -117,7 +120,7 @@ function useViewportSizes(input) {
117
120
hasher && hasher ( { vpW : vpWidth , vpH : vpHeight } )
118
121
) ) ;
119
122
const debounceTimeoutRef = useRef ( undefined ) ;
120
- const listener = useCallback ( ( ! hasher ?
123
+ const listener = useCallback ( ( ! debounceTimeout ?
121
124
state => setState ( state ) :
122
125
state => {
123
126
if ( debounceTimeoutRef . current ) {
@@ -126,7 +129,7 @@ function useViewportSizes(input) {
126
129
127
130
debounceTimeoutRef . current = setTimeout ( ( ) => {
128
131
setState ( state ) ;
129
- } , debounceTimeoutRef ) ;
132
+ } , debounceTimeout ) ;
130
133
}
131
134
) , [ debounceTimeoutRef , hasher , dimension ] ) ;
132
135
@@ -176,9 +179,9 @@ function useViewportSizes(input) {
176
179
const returnValue = useMemo ( ( ) => {
177
180
switch ( dimension ) {
178
181
default :
179
- case 'both' : { return [ state ?. vpW || 0 , state ?. vpH || 0 ] }
180
- case 'w' : { return state ?. vpW || 0 }
181
- case 'h' : { return state ?. vpH || 0 }
182
+ case 'both' : { return [ state ?. vpW || 0 , state ?. vpH || 0 , onResize ] }
183
+ case 'w' : { return [ state ?. vpW || 0 , onResize ] }
184
+ case 'h' : { return [ state ?. vpH || 0 , onResize ] }
182
185
}
183
186
} , [ dimensionHash , onResize , dimension ] ) ;
184
187
0 commit comments