1
1
/* eslint-disable no-console */
2
2
3
3
import React , { Component } from 'react'
4
- import { v4 } from 'uuid'
5
- import keys from 'lodash.keys'
6
4
import throttle from 'lodash.throttle'
7
5
8
6
import * as presets from './presets'
@@ -11,67 +9,51 @@ import getDisplayName from './utils/getDisplayName'
11
9
import shallowDiff from './utils/shallowDiff'
12
10
import getWindowSizes from './utils/getWindowSizes'
13
11
14
- const debug = process . env . NODE_ENV !== 'production'
15
-
16
- let resizeListener
17
- const listeners = { }
12
+ const debug = process && process . env &&
13
+ process . env . NODE_ENV === 'debug'
18
14
19
15
const withSizes = ( ...mappedSizesToProps ) => ( WrappedComponent ) => {
20
- const parseMappedSizesToProps = ( dimensions , props ) => {
21
- const propsToPass = mappedSizesToProps
16
+ const parseMappedSizesToProps = ( dimensions , props ) =>
17
+ mappedSizesToProps
22
18
. map ( check => check ( dimensions , props ) )
23
- . reduce ( ( acc , props ) => ( { ...acc , ...props } ) , { } )
24
-
25
- return propsToPass
26
- }
19
+ . reduce ( ( acc , props ) => ( { ...acc , ...props } ) , { } )
27
20
28
- return class extends Component {
29
- static displayName = `withSizes(${ getDisplayName ( WrappedComponent ) } )` ;
21
+ return class ComponentWithSizes extends Component {
22
+ static displayName = `withSizes(${ getDisplayName ( WrappedComponent ) } )`
30
23
31
24
state = {
32
- id : `A ${ v4 ( ) } ` ,
25
+ initialSizes : getWindowSizes ( window ) ,
33
26
propsToPass : parseMappedSizesToProps ( getWindowSizes ( window ) , this . props ) ,
34
- } ;
35
-
36
- componentDidMount ( ) {
37
- if ( ! resizeListener ) {
38
- resizeListener = window . addEventListener ( 'resize' , this . throttledWindowResize )
39
- }
40
-
41
- listeners [ this . state . id ] = this . listenerCallback
42
-
43
- this . dispatchSizes ( )
44
27
}
45
28
46
- componentWillUnmount ( ) {
47
- delete listeners [ this . state . id ]
48
- if ( keys ( listeners ) . length < 1 ) {
49
- window . removeEventListener ( 'resize' , this . throttledWindowResize )
50
- resizeListener = null
51
- }
52
- }
29
+ /* Dispatching & Throttling */
53
30
54
- listenerCallback = ( sizes ) => {
55
- const propsToPass = parseMappedSizesToProps ( sizes , this . props )
31
+ dispatchSizes = ( ) => {
32
+ const propsToPass = parseMappedSizesToProps ( getWindowSizes ( window ) , this . props )
56
33
57
34
if ( shallowDiff ( propsToPass , this . state . propsToPass ) ) {
58
35
this . setState ( { propsToPass } )
59
36
}
60
37
}
61
38
62
- dispatchSizes = ( ) => {
63
- keys ( listeners ) . forEach ( key => {
64
- const callback = listeners [ key ]
39
+ throttledDispatchSizes = (
40
+ throttle ( this . dispatchSizes , 200 )
41
+ )
65
42
66
- if ( typeof callback === 'function' ) {
67
- callback ( getWindowSizes ( window ) )
68
- }
69
- } )
70
- } ;
43
+ /* Lifecycles */
71
44
72
- throttledWindowResize = (
73
- throttle ( this . dispatchSizes , 200 )
74
- ) ;
45
+ componentDidMount ( ) {
46
+ window . addEventListener ( 'resize' , this . throttledDispatchSizes )
47
+
48
+ /* dispatch if aren't computed on first render */
49
+ if ( ! this . state . initialSizes . canUseDOM ) {
50
+ this . dispatchSizes ( )
51
+ }
52
+ }
53
+
54
+ componentWillUnmount ( ) {
55
+ window . removeEventListener ( 'resize' , this . throttledDispatchSizes )
56
+ }
75
57
76
58
render ( ) {
77
59
if ( debug ) console . log ( 'render' , this . state . propsToPass )
0 commit comments