Skip to content

Commit

Permalink
Refactor: unroll static loop
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Feb 27, 2019
1 parent 0a282f1 commit aec8015
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"curly": 1, // specify curly brace conventions for all control statements
"default-case": 0, // require default case in switch statements (off by default)
"dot-notation": 1, // encourages use of dot notation whenever possible
"eqeqeq": 1, // require the use of === and !==
"eqeqeq": 0, // require the use of === and !==
"guard-for-in": 0, // make sure for-in loops have an if statement (off by default)
"no-alert": 0, // disallow the use of alert, confirm, and prompt
"no-caller": 1, // disallow use of arguments.caller or arguments.callee
Expand Down
3 changes: 2 additions & 1 deletion elements/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default class Text extends Shape {
if (matrix) {
props.matrix = matrix;
}
const text = pickNotNil(extractText(props, true));
const prop = propsAndStyles(props);
const text = pickNotNil(extractText(prop, true));
this.root.setNativeProps({
...props,
...text,
Expand Down
5 changes: 5 additions & 0 deletions lib/Matrix2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
const DEG_TO_RAD = Math.PI / 180;

export const identity = [1, 0, 0, 1, 0, 0];

/**
* Represents an affine transformation matrix, and provides tools for constructing and concatenating matrices.
*
Expand Down Expand Up @@ -85,6 +87,9 @@ export default class Matrix2D {
* @return {Array} an array with current matrix values.
**/
toArray = function() {
if (this.hasInitialState) {
return identity;
}
return [this.a, this.b, this.c, this.d, this.tx, this.ty];
};

Expand Down
19 changes: 10 additions & 9 deletions lib/extract/extractFill.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ const fillRules = {
nonzero: 1,
};

const fillProps = ['fill', 'fillOpacity', 'fillRule'];
const numFillProps = fillProps.length;

// default fill is black
const black = colorNames.black;
const defaultFill = [
0,
Platform.OS === 'android' ? colorNames.black | 0x0 : colorNames.black,
Platform.OS === 'android' ? black | 0x0 : black,
];

export default function extractFill(props, styleProperties) {
for (let i = 0; i < numFillProps; i++) {
const name = fillProps[i];
if (props.hasOwnProperty(name)) {
styleProperties.push(name);
}
if (props.fill != null) {
styleProperties.push('fill');
}
if (props.fillOpacity != null) {
styleProperties.push('fillOpacity');
}
if (props.fillRule != null) {
styleProperties.push('fillRule');
}

const { fill, fillRule, fillOpacity } = props;
Expand Down
53 changes: 21 additions & 32 deletions lib/extract/extractResponder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,50 @@ import { PanResponder } from 'react-native';
const responderProps = Object.keys(PanResponder.create({}).panHandlers);
const numResponderProps = responderProps.length;

const touchableProps = [
'disabled',
'onPress',
'onPressIn',
'onPressOut',
'onLongPress',
'delayPressIn',
'delayPressOut',
'delayLongPress',
];
const numTouchableProps = touchableProps.length;

function hasTouchableProperty(props) {
for (let i = 0; i < numTouchableProps; i++) {
if (props.hasOwnProperty(touchableProps[i])) {
return true;
}
}
return false;
return (
props.disabled != null ||
props.onPress ||
props.onPressIn ||
props.onPressOut ||
props.onLongPress ||
props.delayPressIn ||
props.delayPressOut ||
props.delayLongPress
);
}

export default function extractResponder(props, ref) {
const extractedProps = {};
const o = {};

let responsible = false;
for (let i = 0; i < numResponderProps; i++) {
const key = responderProps[i];
const value = props[key];
if (value) {
responsible = true;
extractedProps[key] = value;
o[key] = value;
}
}

const pointerEvents = props.pointerEvents;
if (pointerEvents) {
extractedProps.pointerEvents = pointerEvents;
o.pointerEvents = pointerEvents;
}

if (hasTouchableProperty(props)) {
responsible = true;
Object.assign(extractedProps, {
onStartShouldSetResponder: ref.touchableHandleStartShouldSetResponder,
onResponderTerminationRequest:
ref.touchableHandleResponderTerminationRequest,
onResponderGrant: ref.touchableHandleResponderGrant,
onResponderMove: ref.touchableHandleResponderMove,
onResponderRelease: ref.touchableHandleResponderRelease,
onResponderTerminate: ref.touchableHandleResponderTerminate,
});
o.onResponderMove = ref.touchableHandleResponderMove;
o.onResponderGrant = ref.touchableHandleResponderGrant;
o.onResponderRelease = ref.touchableHandleResponderRelease;
o.onResponderTerminate = ref.touchableHandleResponderTerminate;
o.onStartShouldSetResponder = ref.touchableHandleStartShouldSetResponder;
o.onResponderTerminationRequest = ref.touchableHandleResponderTerminationRequest;
}

if (responsible) {
extractedProps.responsible = true;
o.responsible = true;
}

return extractedProps;
return o;
}
40 changes: 23 additions & 17 deletions lib/extract/extractStroke.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,30 @@ const joins = {
round: 1,
};

const strokeProps = [
'stroke',
'strokeWidth',
'strokeOpacity',
'strokeDasharray',
'strokeDashoffset',
'strokeLinecap',
'strokeLinejoin',
'strokeMiterlimit',
];
const numStrokeProps = strokeProps.length;

export default function extractStroke(props, styleProperties) {
for (let i = 0; i < numStrokeProps; i++) {
const name = strokeProps[i];
if (props.hasOwnProperty(name)) {
styleProperties.push(name);
}
if (props.stroke != null) {
styleProperties.push('stroke');
}
if (props.strokeWidth != null) {
styleProperties.push('strokeWidth');
}
if (props.strokeOpacity != null) {
styleProperties.push('strokeOpacity');
}
if (props.strokeDasharray != null) {
styleProperties.push('strokeDasharray');
}
if (props.strokeDashoffset != null) {
styleProperties.push('strokeDashoffset');
}
if (props.strokeLinecap != null) {
styleProperties.push('strokeLinecap');
}
if (props.strokeLinejoin != null) {
styleProperties.push('strokeLinejoin');
}
if (props.strokeMiterlimit != null) {
styleProperties.push('strokeMiterlimit');
}

const { stroke, strokeWidth = 1, strokeDasharray } = props;
Expand Down
6 changes: 1 addition & 5 deletions lib/extract/extractTransform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Matrix2D from '../Matrix2D';
import Matrix2D, { identity } from '../Matrix2D';
import transformParser from './transform';

const pooledMatrix = new Matrix2D();
Expand Down Expand Up @@ -51,9 +51,7 @@ export function props2transform(props) {
const skew = universal2axis(props.skew, props.skewX, props.skewY);
const translate = universal2axis(
props.translate,
// eslint-disable-next-line eqeqeq
props.translateX == null ? props.x || 0 : props.translateX,
// eslint-disable-next-line eqeqeq
props.translateY == null ? props.y || 0 : props.translateY,
);

Expand Down Expand Up @@ -102,8 +100,6 @@ export function transformToMatrix(props, transform) {
return pooledMatrix.toArray();
}

const identity = [1, 0, 0, 1, 0, 0];

export default function extractTransform(props) {
if (Array.isArray(props)) {
return props;
Expand Down

0 comments on commit aec8015

Please sign in to comment.