Skip to content

Commit

Permalink
Optimizations (planttheidea#9)
Browse files Browse the repository at this point in the history
* Updates...
* Move all constants to constants (no duplicate declarations of default property names)
* Refactor a few things to be more functional
* Return dev sourcemap to cheap-module-eval-source-map

* Updates...
* Correct alphabetization
  • Loading branch information
planttheidea authored Sep 22, 2016
1 parent 1068a5c commit c0e661d
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 141 deletions.
16 changes: 8 additions & 8 deletions DEV_ONLY/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class Div extends Component {
children
} = this.props;

// console.group('Div');
// console.log('div position', this.props.position);
// console.log('div size', this.props.size);
// console.groupEnd();
console.group('Div');
console.log('div position', this.props.position);
console.log('div size', this.props.size);
console.groupEnd();

return (
<div style={DIV_STYLES}>
Expand Down Expand Up @@ -100,10 +100,10 @@ class Main extends Component {
children
} = this.props;

console.group('Main');
console.log('main position', this.props.position);
console.log('main size', this.props.size);
console.groupEnd();
// console.group('Main');
// console.log('main position', this.props.position);
// console.log('main size', this.props.size);
// console.groupEnd();

return (
<main>
Expand Down
166 changes: 100 additions & 66 deletions dist/remeasure.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/remeasure.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/remeasure.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@
"test:watch": "NODE_ENV=test ava --no-cache --watch",
"transpile": "./node_modules/babel-cli/lib/babel/index.js -d lib/ src/"
},
"version": "1.4.0"
"version": "1.4.1"
}
7 changes: 7 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const POSITION_PROP_DEFAULT = 'position';
const RENDER_ON_RESIZE_DEFAULT = true;
const SIZE_PROP_DEFAULT = 'size';

const BOUNDING_CLIENT_RECT_SIZE_KEYS = [
'height',
'width'
Expand Down Expand Up @@ -75,3 +79,6 @@ export {BOUNDING_CLIENT_RECT_SIZE_KEYS};
export {DOM_ELEMENT_POSITION_KEYS};
export {DOM_ELEMENT_SIZE_KEYS};
export {initialState};
export {POSITION_PROP_DEFAULT};
export {RENDER_ON_RESIZE_DEFAULT};
export {SIZE_PROP_DEFAULT};
41 changes: 25 additions & 16 deletions src/getHigherOrderComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import {
import {
ALL_BOUNDING_CLIENT_RECT_KEYS,
ALL_DOM_ELEMENT_KEYS,
ALL_KEYS
ALL_KEYS,
POSITION_PROP_DEFAULT,
RENDER_ON_RESIZE_DEFAULT,
SIZE_PROP_DEFAULT
} from './constants';

const POSITION_PROP_DEFAULT = 'position';
const RENDER_ON_RESIZE_DEFAULT = true;
const SIZE_PROP_DEFAULT = 'size';

let raf;

/**
Expand All @@ -43,6 +42,26 @@ const setRaf = () => {
);
};

/**
* based on desiredKeys, build the initialState object
*
* @param {array<string>} allKeys
* @param {array<string>} desiredKeys
* @returns {array<T>}
*/
const reduceStateToMatchingKeys = (allKeys, desiredKeys) => {
return allKeys.reduce((accumulatedInitialState, key) => {
if (desiredKeys.includes(key)) {
return {
...accumulatedInitialState,
[key]: 0
};
}

return accumulatedInitialState;
}, {});
};

/**
* create the HOC that injects the position and size props
* into the child (assuming they have keys that are valid
Expand All @@ -67,17 +86,7 @@ const getHigherOrderComponent = (OriginalComponent, keys, options = {}) => {

const boundingClientRectKeys = arraySubset(ALL_BOUNDING_CLIENT_RECT_KEYS, keys);
const domElementKeys = arraySubset(ALL_DOM_ELEMENT_KEYS, keys);

const initialState = ALL_KEYS.reduce((accumulatedInitialState, key) => {
if (keys.includes(key)) {
return {
...accumulatedInitialState,
[key]: 0
};
}

return accumulatedInitialState;
}, {});
const initialState = reduceStateToMatchingKeys(ALL_KEYS, keys);

if (!raf) {
setRaf();
Expand Down
Loading

0 comments on commit c0e661d

Please sign in to comment.