11/* eslint-disable no-console */
22
33import React , { Component } from 'react'
4- import { v4 } from 'uuid'
5- import keys from 'lodash.keys'
64import throttle from 'lodash.throttle'
75
86import * as presets from './presets'
@@ -11,67 +9,51 @@ import getDisplayName from './utils/getDisplayName'
119import shallowDiff from './utils/shallowDiff'
1210import getWindowSizes from './utils/getWindowSizes'
1311
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'
1814
1915const withSizes = ( ...mappedSizesToProps ) => ( WrappedComponent ) => {
20- const parseMappedSizesToProps = ( dimensions , props ) => {
21- const propsToPass = mappedSizesToProps
16+ const parseMappedSizesToProps = ( dimensions , props ) =>
17+ mappedSizesToProps
2218 . map ( check => check ( dimensions , props ) )
23- . reduce ( ( acc , props ) => ( { ...acc , ...props } ) , { } )
24-
25- return propsToPass
26- }
19+ . reduce ( ( acc , props ) => ( { ...acc , ...props } ) , { } )
2720
28- return class extends Component {
29- static displayName = `withSizes(${ getDisplayName ( WrappedComponent ) } )` ;
21+ return class ComponentWithSizes extends Component {
22+ static displayName = `withSizes(${ getDisplayName ( WrappedComponent ) } )`
3023
3124 state = {
32- id : `A ${ v4 ( ) } ` ,
25+ initialSizes : getWindowSizes ( window ) ,
3326 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 ( )
4427 }
4528
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 */
5330
54- listenerCallback = ( sizes ) => {
55- const propsToPass = parseMappedSizesToProps ( sizes , this . props )
31+ dispatchSizes = ( ) => {
32+ const propsToPass = parseMappedSizesToProps ( getWindowSizes ( window ) , this . props )
5633
5734 if ( shallowDiff ( propsToPass , this . state . propsToPass ) ) {
5835 this . setState ( { propsToPass } )
5936 }
6037 }
6138
62- dispatchSizes = ( ) => {
63- keys ( listeners ) . forEach ( key => {
64- const callback = listeners [ key ]
39+ throttledDispatchSizes = (
40+ throttle ( this . dispatchSizes , 200 )
41+ )
6542
66- if ( typeof callback === 'function' ) {
67- callback ( getWindowSizes ( window ) )
68- }
69- } )
70- } ;
43+ /* Lifecycles */
7144
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+ }
7557
7658 render ( ) {
7759 if ( debug ) console . log ( 'render' , this . state . propsToPass )
0 commit comments