From aec8015255aee4ae148b3a286d02217878db6360 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Wed, 27 Feb 2019 21:11:27 +0200 Subject: [PATCH] Refactor: unroll static loop --- .eslintrc | 2 +- elements/Text.js | 3 +- lib/Matrix2D.js | 5 ++++ lib/extract/extractFill.js | 19 ++++++------ lib/extract/extractResponder.js | 53 +++++++++++++-------------------- lib/extract/extractStroke.js | 40 ++++++++++++++----------- lib/extract/extractTransform.js | 6 +--- 7 files changed, 63 insertions(+), 65 deletions(-) diff --git a/.eslintrc b/.eslintrc index c739b3fc0..9fa0ac25e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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 diff --git a/elements/Text.js b/elements/Text.js index 043472f7c..dcf4821c3 100644 --- a/elements/Text.js +++ b/elements/Text.js @@ -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, diff --git a/lib/Matrix2D.js b/lib/Matrix2D.js index 763e607af..63434c70e 100644 --- a/lib/Matrix2D.js +++ b/lib/Matrix2D.js @@ -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. * @@ -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]; }; diff --git a/lib/extract/extractFill.js b/lib/extract/extractFill.js index 0ab4b437a..313b88b86 100644 --- a/lib/extract/extractFill.js +++ b/lib/extract/extractFill.js @@ -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; diff --git a/lib/extract/extractResponder.js b/lib/extract/extractResponder.js index 810b9413a..d8b697cf5 100644 --- a/lib/extract/extractResponder.js +++ b/lib/extract/extractResponder.js @@ -3,29 +3,21 @@ 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++) { @@ -33,31 +25,28 @@ export default function extractResponder(props, ref) { 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; } diff --git a/lib/extract/extractStroke.js b/lib/extract/extractStroke.js index ec8fa516b..df04204bf 100644 --- a/lib/extract/extractStroke.js +++ b/lib/extract/extractStroke.js @@ -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; diff --git a/lib/extract/extractTransform.js b/lib/extract/extractTransform.js index 94a76472d..e1fc9dc6f 100644 --- a/lib/extract/extractTransform.js +++ b/lib/extract/extractTransform.js @@ -1,4 +1,4 @@ -import Matrix2D from '../Matrix2D'; +import Matrix2D, { identity } from '../Matrix2D'; import transformParser from './transform'; const pooledMatrix = new Matrix2D(); @@ -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, ); @@ -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;