From 6cb31bb19dc29c841d5572edf48bd5dbeaa2ced2 Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Fri, 22 May 2015 09:43:42 -0700 Subject: [PATCH 01/44] [JS] Faster crc32 --- Libraries/vendor/crypto/crc32.js | 106 ++++++++++++++++++------------- 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/Libraries/vendor/crypto/crc32.js b/Libraries/vendor/crypto/crc32.js index 1ded3fddfcdddb..540d60aa832b6e 100644 --- a/Libraries/vendor/crypto/crc32.js +++ b/Libraries/vendor/crypto/crc32.js @@ -1,5 +1,5 @@ /** - * @generated SignedSource<<9bce659a43d6f6115b20a18f6c995d8a>> + * @generated SignedSource<<77bdeb858138636c96c405d64b6be55c>> * * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * !! This file is a check-in of a static_upstream project! !! @@ -20,57 +20,73 @@ /* jslint bitwise: true */ /** - * Copyright (c) 2006 Andrea Ercolino - * http://www.opensource.org/licenses/mit-license.php + * Modified from the original for performance improvements. + * + * @see http://create.stephan-brumme.com/crc32/ + * @see http://stackoverflow.com/questions/18638900/ + * @copyright 2006 Andrea Ercolino + * @license MIT */ -var table = '00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 ' + - '9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 ' + - '90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 ' + - '83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 ' + - '8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD ' + - 'A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ' + - 'ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 ' + - 'B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB ' + - 'B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 ' + - 'E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 ' + - 'E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 ' + - 'F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 ' + - 'FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D ' + - 'D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F ' + - 'DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 ' + - 'CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B ' + - 'C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 ' + - '73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 ' + - '7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 ' + - '6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 ' + - '60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD ' + - '48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF ' + - '4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 ' + - '5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B ' + - '5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 ' + - '05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 ' + - '0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 ' + - '18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 ' + - '166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D ' + - '3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F ' + - '30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 ' + - '23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B ' + - '2D02EF8D'; + +var table = [ + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, + 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, + 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, + 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, + 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, + 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, + 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, + 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, + 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, + 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, + 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, + 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, + 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, + 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, + 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, + 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, + 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, + 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, + 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, + 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, + 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, + 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D +]; + +if (global.Int32Array !== undefined) { + table = new Int32Array(table); +} /** * @returns Number */ function crc32(str) { - var crc = 0; - var n = 0; - var x = 0; - crc = crc ^ (-1); - for (var i = 0, iTop = str.length; i < iTop; i++) { - n = (crc ^ str.charCodeAt(i)) & 0xFF; - x = "0x" + table.substr(n * 9, 8); - crc = (crc >>> 8) ^ x; + var crc = -1; + for (var i = 0, len = str.length; i < len; i++) { + crc = (crc >>> 8) ^ table[(crc ^ str.charCodeAt(i)) & 0xFF]; } - return crc ^ (-1); + return ~crc; } module.exports = crc32; From bd11de1b5152f1d9b91a594fdea82e19f3fb7ac9 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Fri, 22 May 2015 10:17:43 -0700 Subject: [PATCH 02/44] [react-packager] Use actual error types Summary: @public Previously, we had to use errors as a property on the result object because there was no way to pass custom props between the child worker and the parent. This has been fixed in node-worker-farm (D2092153) and now we can use regular errors. This also adapts the transformer to babel-specific errors. Generic errors, however, should still work and render readable info. Additionally, I deprecated, but maintained backwards compatiblity for people in OSS that are using custom transformers. Test Plan: 1. `./runJestTests.sh` 2. `./runJestTests.sh PackagerIntegration` 3. open the playground app 4. Add a syntax error. Say `1=x` somewhere in the file 5. Reload and see error message 'SyntaxError description (line:col)' 6. Make sure that the stack trace is clickable and it attempts to open the editor to the location --- .../__tests__/Transformer-test.js | 27 +++++++++----- .../react-packager/src/JSTransformer/index.js | 37 +++++++++---------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js b/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js index 5948916a7353d8..22ca53e632d4ab 100644 --- a/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js +++ b/packager/react-packager/src/JSTransformer/__tests__/Transformer-test.js @@ -57,25 +57,34 @@ describe('Transformer', function() { }); pit('should add file info to parse errors', function() { + var message = 'message'; + var snippet = 'snippet'; + require('fs').readFile.mockImpl(function(file, callback) { callback(null, 'var x;\nvar answer = 1 = x;'); }); workers.mockImpl(function(data, callback) { - var esprimaError = new Error('Error: Line 2: Invalid left-hand side in assignment'); - esprimaError.description = 'Invalid left-hand side in assignment'; - esprimaError.lineNumber = 2; - esprimaError.column = 15; - callback(null, {error: esprimaError}); + var babelError = new SyntaxError(message); + babelError.type = 'SyntaxError'; + babelError.description = message; + babelError.loc = { + line: 2, + column: 15, + }; + babelError.codeFrame = snippet; + callback(babelError); }); return new Transformer(OPTIONS).loadFileAndTransform('foo-file.js') .catch(function(error) { expect(error.type).toEqual('TransformError'); - expect(error.snippet).toEqual([ - 'var answer = 1 = x;', - ' ^', - ].join('\n')); + expect(error.message).toBe('SyntaxError ' + message); + expect(error.lineNumber).toBe(2); + expect(error.column).toBe(15); + expect(error.filename).toBe('foo-file.js'); + expect(error.description).toBe(message); + expect(error.snippet).toBe(snippet); }); }); }); diff --git a/packager/react-packager/src/JSTransformer/index.js b/packager/react-packager/src/JSTransformer/index.js index 2dc5e20bd48071..513d4394e17f4e 100644 --- a/packager/react-packager/src/JSTransformer/index.js +++ b/packager/react-packager/src/JSTransformer/index.js @@ -99,7 +99,12 @@ Transformer.prototype.loadFileAndTransform = function(filePath) { }).then( function(res) { if (res.error) { - throw formatError(res.error, filePath, sourceCode); + console.warn( + 'Error property on the result value form the transformer', + 'module is deprecated and will be removed in future versions.', + 'Please pass an error object as the first argument to the callback' + ); + throw formatError(res.error, filePath); } return new ModuleTransport({ @@ -110,6 +115,8 @@ Transformer.prototype.loadFileAndTransform = function(filePath) { }); } ); + }).catch(function(err) { + throw formatError(err, filePath); }); }); }; @@ -118,8 +125,8 @@ function TransformError() {} util.inherits(TransformError, SyntaxError); function formatError(err, filename, source) { - if (err.lineNumber && err.column) { - return formatEsprimaError(err, filename, source); + if (err.loc) { + return formatBabelError(err, filename, source); } else { return formatGenericError(err, filename, source); } @@ -136,26 +143,16 @@ function formatGenericError(err, filename) { return error; } -function formatEsprimaError(err, filename, source) { - var stack = err.stack.split('\n'); - stack.shift(); - - var msg = 'TransformError: ' + err.description + ' ' + filename + ':' + - err.lineNumber + ':' + err.column; - var sourceLine = source.split('\n')[err.lineNumber - 1]; - var snippet = sourceLine + '\n' + new Array(err.column - 1).join(' ') + '^'; - - stack.unshift(msg); - +function formatBabelError(err, filename) { var error = new TransformError(); - error.message = msg; error.type = 'TransformError'; - error.stack = stack.join('\n'); - error.snippet = snippet; + error.message = (err.type || error.type) + ' ' + err.message; + error.stack = err.stack; + error.snippet = err.codeFrame; + error.lineNumber = err.loc.line; + error.column = err.loc.column; error.filename = filename; - error.lineNumber = err.lineNumber; - error.column = err.column; - error.description = err.description; + error.description = err.message; return error; } From 52725437d47c83a9c506798ceaf72b53d8cb18b1 Mon Sep 17 00:00:00 2001 From: Jared Forsyth Date: Fri, 22 May 2015 11:06:54 -0700 Subject: [PATCH 03/44] Upgrating linting to use babel-eslint --- .eslintrc | 34 +++++++++++++++-- lint/linterTransform.js | 84 ----------------------------------------- linter.js | 17 --------- package.json | 4 +- 4 files changed, 34 insertions(+), 105 deletions(-) delete mode 100644 lint/linterTransform.js delete mode 100644 linter.js diff --git a/.eslintrc b/.eslintrc index 84045365c85b89..e1ec93029f2bd1 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,8 +1,19 @@ { + "parser": "babel-eslint", + + "ecmaFeatures": { + "jsx": true + }, + "env": { + "es6": true, "jasmine": true, }, + "plugins": [ + "react" + ], + // Map from global var to bool specifying if it can be redefined "globals": { "__DEV__": true, @@ -36,10 +47,10 @@ }, "rules": { + "comma-dangle": 0, // disallow trailing commas in object literals "no-cond-assign": 1, // disallow assignment in conditional expressions "no-console": 0, // disallow use of console (off by default in the node environment) "no-constant-condition": 0, // disallow use of constant expressions in conditions - "no-comma-dangle": 0, // disallow trailing commas in object literals "no-control-regex": 1, // disallow control characters in regular expressions "no-debugger": 1, // disallow use of debugger "no-dupe-keys": 1, // disallow duplicate keys when creating object literals @@ -107,6 +118,7 @@ "no-warning-comments": 0, // disallow usage of configurable warning terms in comments": 1, // e.g. TODO or FIXME (off by default) "no-with": 1, // disallow use of the with statement "radix": 1, // require use of the second argument for parseInt() (off by default) + "semi-spacing": 1, // require a space after a semi-colon "vars-on-top": 0, // requires to declare all vars on top of their containing scope (off by default) "wrap-iife": 0, // require immediate function invocation to be wrapped in parentheses (off by default) "yoda": 1, // require or disallow Yoda conditions @@ -177,7 +189,7 @@ "space-in-parens": 0, // require or disallow spaces inside parentheses (off by default) "space-infix-ops": 1, // require spaces around operators "space-return-throw-case": 1, // require a space after return, throw, and case - "space-unary-word-ops": 1, // require a space around word operators such as typeof (off by default) + "space-unary-ops": [1, { "words": true, "nonwords": false }], // require or disallow spaces before/after unary operators (words on by default, nonwords off by default) "max-nested-callbacks": 0, // specify the maximum depth callbacks can be nested (off by default) "one-var": 0, // allow just one var statement per function (off by default) "wrap-regex": 0, // require regex literals to be wrapped in parentheses (off by default) @@ -190,6 +202,22 @@ "max-params": 0, // limits the number of parameters that can be used in the function declaration. (off by default) "max-statements": 0, // specify the maximum number of statement allowed in a function (off by default) "no-bitwise": 1, // disallow use of bitwise operators (off by default) - "no-plusplus": 0 // disallow use of unary operators, ++ and -- (off by default) + "no-plusplus": 0, // disallow use of unary operators, ++ and -- (off by default) + + "react/display-name": 0, + "react/jsx-boolean-value": 0, + "react/jsx-quotes": [1, "double", "avoid-escape"], + "react/jsx-no-undef": 1, + "react/jsx-sort-props": 0, + "react/jsx-uses-react": 0, + "react/jsx-uses-vars": 1, + "react/no-did-mount-set-state": [1, "allow-in-func"], + "react/no-did-update-set-state": [1, "allow-in-func"], + "react/no-multi-comp": 0, + "react/no-unknown-property": 0, + "react/prop-types": 0, + "react/react-in-jsx-scope": 0, + "react/self-closing-comp": 1, + "react/wrap-multilines": 0 } } diff --git a/lint/linterTransform.js b/lint/linterTransform.js deleted file mode 100644 index 669baf556c442c..00000000000000 --- a/lint/linterTransform.js +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ -'use strict'; - -var eslint = require('eslint'); - -var ignoredStylisticRules = { - 'key-spacing': false, - 'comma-spacing': true, - 'no-multi-spaces': true, - 'brace-style': true, - 'camelcase': true, - 'consistent-this': true, - 'eol-last': true, - 'func-names': true, - 'func-style': true, - 'new-cap': true, - 'new-parens': true, - 'no-nested-ternary': true, - 'no-array-constructor': true, - 'no-lonely-if': true, - 'no-new-object': true, - 'no-spaced-func': true, - 'no-space-before-semi': true, - 'no-ternary': true, - 'no-trailing-spaces': true, - 'no-underscore-dangle': true, - 'no-wrap-func': true, - 'no-mixed-spaces-and-tabs': true, - 'quotes': true, - 'quote-props': true, - 'semi': true, - 'sort-vars': true, - 'space-after-keywords': true, - 'space-in-brackets': true, - 'space-in-parens': true, - 'space-infix-ops': true, - 'space-return-throw-case': true, - 'space-unary-word-ops': true, - 'max-nested-callbacks': true, - 'one-var': true, - 'wrap-regex': true, - 'curly': true, - 'no-mixed-requires': true, -}; - -function setLinterTransform(transformSource) { - var originalVerify = eslint.linter.verify; - eslint.linter.verify = function(text, config, filename, saveState) { - var transformedText; - try { - transformedText = transformSource(text, filename); - } catch (e) { - return [{ - severity: 2, - line: e.lineNumber, - message: e.message, - source: text - }]; - } - var originalLines = text.split('\n'); - var transformedLines = transformedText.split('\n'); - var warnings = originalVerify.call(eslint.linter, transformedText, config, filename, saveState); - - // JSX and ES6 transforms usually generate pretty ugly code. Let's skip lint warnings - // about code style for lines that have been changed by transform step. - // Note that more important issues, like use of undefined vars, will still be reported. - return warnings.filter(function(error) { - var lineHasBeenTransformed = originalLines[error.line - 1] !== transformedLines[error.line - 1]; - var shouldIgnore = ignoredStylisticRules[error.ruleId] && lineHasBeenTransformed; - return !shouldIgnore; - }); - }; -} - -module.exports = { - setLinterTransform: setLinterTransform, -}; diff --git a/linter.js b/linter.js deleted file mode 100644 index f243a2d2fb8ce3..00000000000000 --- a/linter.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ -'use strict'; - -var transformSource = require('./jestSupport/scriptPreprocess.js').transformSource; -var linterTransform = require('./lint/linterTransform'); - -linterTransform.setLinterTransform(transformSource); - -// Run the original CLI -require('eslint/bin/eslint'); diff --git a/package.json b/package.json index a2c7a38b408da4..a4dd3946979468 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,8 @@ }, "devDependencies": { "jest-cli": "0.4.5", - "eslint": "0.9.2" + "babel-eslint": "3.1.5", + "eslint": "0.21.2", + "eslint-plugin-react": "2.3.0" } } From 62c556c7851d780df4cb9e27ed96c25b919a699b Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Fri, 22 May 2015 11:45:07 -0700 Subject: [PATCH 04/44] [ReactNative] Navigator context setHandler refer by index --- Libraries/CustomComponents/Navigator/Navigator.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Libraries/CustomComponents/Navigator/Navigator.js b/Libraries/CustomComponents/Navigator/Navigator.js index b951d7582b5ad5..912d4b9cb783f1 100644 --- a/Libraries/CustomComponents/Navigator/Navigator.js +++ b/Libraries/CustomComponents/Navigator/Navigator.js @@ -332,7 +332,7 @@ var Navigator = React.createClass({ this._subRouteFocus = []; this.navigatorContext = { // Actions for child navigators or interceptors: - setHandlerForRoute: this.setHandlerForRoute, + setHandlerForIndex: this.setHandlerForIndex, request: this.request, // Contextual utilities @@ -462,8 +462,8 @@ var Navigator = React.createClass({ return true; }, - setHandlerForRoute: function(route, handler) { - this._handlers[this.state.routeStack.indexOf(route)] = handler; + setHandlerForIndex: function(index, handler) { + this._handlers[index] = handler; }, componentDidMount: function() { @@ -1262,7 +1262,7 @@ var Navigator = React.createClass({ ...this.navigatorContext, route, setHandler: (handler) => { - this.navigatorContext.setHandlerForRoute(route, handler); + this.navigatorContext.setHandlerForIndex(i, handler); }, onWillFocus: (childRoute) => { this._subRouteFocus[i] = childRoute; From 5bad316678efb2702678def1f5b5065cd8337d37 Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Fri, 22 May 2015 11:56:04 -0700 Subject: [PATCH 05/44] [ReactNative] Navigator de-contextualize popToRoute --- .../CustomComponents/Navigator/Navigator.js | 34 +++---------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/Libraries/CustomComponents/Navigator/Navigator.js b/Libraries/CustomComponents/Navigator/Navigator.js index 912d4b9cb783f1..1ebbc70aa0220e 100644 --- a/Libraries/CustomComponents/Navigator/Navigator.js +++ b/Libraries/CustomComponents/Navigator/Navigator.js @@ -340,14 +340,13 @@ var Navigator = React.createClass({ getCurrentRoutes: this.getCurrentRoutes, // `route` is injected by NavigatorStaticContextContainer - // Contextual nav actions + // Contextual nav action pop: this.requestPop, - popToRoute: this.requestPopTo, - // Legacy, imperitive nav actions. Will transition these to contextual actions jumpBack: this.jumpBack, jumpForward: this.jumpForward, jumpTo: this.jumpTo, + popToRoute: this.popToRoute, push: this.push, replace: this.replace, replaceAtIndex: this.replaceAtIndex, @@ -410,8 +409,6 @@ var Navigator = React.createClass({ switch (action) { case 'pop': return this._handlePop(arg1); - case 'popTo': - return this._handlePopTo(arg1); case 'push': return this._handlePush(arg1); default: @@ -440,23 +437,6 @@ var Navigator = React.createClass({ return true; }, - _handlePopTo: function(destRoute) { - if (destRoute) { - var hasRoute = this.state.routeStack.indexOf(destRoute) !== -1; - if (hasRoute) { - this.popToRoute(destRoute); - return true; - } else { - return false; - } - } - if (this.state.presentedIndex === 0) { - return false; - } - this.pop(); - return true; - }, - _handlePush: function(route) { this.push(route); return true; @@ -1155,17 +1135,13 @@ var Navigator = React.createClass({ this.popToRoute(this.state.routeStack[0]); }, - _getNumToPopForRoute: function(route) { + popToRoute: function(route) { var indexOfRoute = this.state.routeStack.indexOf(route); invariant( indexOfRoute !== -1, - 'Calling pop to route for a route that doesn\'t exist!' + 'Calling popToRoute for a route that doesn\'t exist!' ); - return this.state.presentedIndex - indexOfRoute; - }, - - popToRoute: function(route) { - var numToPop = this._getNumToPopForRoute(route); + var numToPop = this.state.presentedIndex - indexOfRoute; this._popN(numToPop); }, From 56d6ee3f0fadeb66aeb6cf9156c723759f48f617 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Fri, 22 May 2015 12:16:15 -0700 Subject: [PATCH 06/44] [react-native] Replace jstransform with Babel in the OSS repo Summary: @public Replaces jstransform with Babel. Additionally, stops (using the deprecated) passing an error property back from the transformer, and instead passes an error in the first argument. This is because we were able to update node-worker-farm to handle custom properties on errors. Test Plan: 1. Export the oss project 2. npm install 3. Start the movies app 4. Make sure it works 5. Add a syntax error 6. Make sure the message is correct --- packager/transformer.js | 70 ++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/packager/transformer.js b/packager/transformer.js index 00beeaddeeda11..a2efcea3bb2b54 100644 --- a/packager/transformer.js +++ b/packager/transformer.js @@ -10,40 +10,37 @@ */ 'use strict'; -var jstransform = require('jstransform').transform; +var babel = require('babel'); -var reactVisitors = - require('react-tools/vendor/fbtransform/visitors').getAllVisitors(); -var staticTypeSyntax = - require('jstransform/visitors/type-syntax').visitorList; -var trailingCommaVisitors = - require('jstransform/visitors/es7-trailing-comma-visitors.js').visitorList; - -// Note that reactVisitors now handles ES6 classes, rest parameters, arrow -// functions, template strings, and object short notation. -var visitorList = reactVisitors.concat(trailingCommaVisitors); - -function transform(srcTxt, filename) { - var options = { - es3: true, - sourceType: 'nonStrictModule', +function transform(srcTxt, filename, options) { + var result = babel.transform(srcTxt, { + retainLines: true, + compact: true, + comments: false, filename: filename, + whitelist: [ + 'es6.arrowFunctions', + 'es6.blockScoping', + 'es6.classes', + 'es6.destructuring', + 'es6.parameters.rest', + 'es6.properties.computed', + 'es6.properties.shorthand', + 'es6.spread', + 'es6.templateLiterals', + 'es7.trailingFunctionCommas', + 'es7.objectRestSpread', + 'flow', + 'react', + ], + sourceFileName: filename, + sourceMaps: false, + extra: options || {}, + }); + + return { + code: result.code, }; - - // These tranforms mostly just erase type annotations and static typing - // related statements, but they were conflicting with other tranforms. - // Running them first solves that problem - var staticTypeSyntaxResult = jstransform( - staticTypeSyntax, - srcTxt, - options - ); - - return jstransform( - visitorList, - staticTypeSyntaxResult.code, - options - ); } module.exports = function(data, callback) { @@ -54,15 +51,8 @@ module.exports = function(data, callback) { data.filename ); } catch (e) { - return callback(null, { - error: { - lineNumber: e.lineNumber, - column: e.column, - message: e.message, - stack: e.stack, - description: e.description - } - }); + callback(e); + return; } callback(null, result); From aad54006e3baebf94923b237dbc22c41fa985ac1 Mon Sep 17 00:00:00 2001 From: Jared Forsyth Date: Fri, 22 May 2015 12:57:08 -0700 Subject: [PATCH 07/44] Observing "MemoryWarningNotification" and proxying it up to the DeviceEventEmitter --- Examples/UIExplorer/AppStateIOSExample.js | 18 ++++++++++ Libraries/AppStateIOS/AppStateIOS.ios.js | 41 ++++++++++++++++------- React/Modules/RCTAppState.m | 10 ++++++ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/Examples/UIExplorer/AppStateIOSExample.js b/Examples/UIExplorer/AppStateIOSExample.js index 9272f9e7830197..c2a011ceba96b8 100644 --- a/Examples/UIExplorer/AppStateIOSExample.js +++ b/Examples/UIExplorer/AppStateIOSExample.js @@ -28,13 +28,19 @@ var AppStateSubscription = React.createClass({ return { appState: AppStateIOS.currentState, previousAppStates: [], + memoryWarnings: 0, }; }, componentDidMount: function() { AppStateIOS.addEventListener('change', this._handleAppStateChange); + AppStateIOS.addEventListener('memoryWarning', this._handleMemoryWarning); }, componentWillUnmount: function() { AppStateIOS.removeEventListener('change', this._handleAppStateChange); + AppStateIOS.removeEventListener('memoryWarning', this._handleMemoryWarning); + }, + _handleMemoryWarning: function() { + this.setState({memoryWarnings: this.state.memoryWarnings + 1}) }, _handleAppStateChange: function(appState) { var previousAppStates = this.state.previousAppStates.slice(); @@ -45,6 +51,13 @@ var AppStateSubscription = React.createClass({ }); }, render() { + if (this.props.showMemoryWarnings) { + return ( + + {this.state.memoryWarnings} + + ); + } if (this.props.showCurrentOnly) { return ( @@ -77,4 +90,9 @@ exports.examples = [ title: 'Previous states:', render(): ReactElement { return ; } }, + { + title: 'Memory Warnings', + description: "In the simulator, hit Shift+Command+M to simulate a memory warning.", + render(): ReactElement { return ; } + }, ]; diff --git a/Libraries/AppStateIOS/AppStateIOS.ios.js b/Libraries/AppStateIOS/AppStateIOS.ios.js index 48db8415b9201a..b11c41a53694af 100644 --- a/Libraries/AppStateIOS/AppStateIOS.ios.js +++ b/Libraries/AppStateIOS/AppStateIOS.ios.js @@ -16,10 +16,12 @@ var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); var RCTAppState = NativeModules.AppState; var logError = require('logError'); +var invariant = require('invariant'); -var DEVICE_APPSTATE_EVENT = 'appStateDidChange'; - -var _appStateHandlers = {}; +var _eventHandlers = { + change: new Map(), + memoryWarning: new Map(), +}; /** * `AppStateIOS` can tell you if the app is in the foreground or background, @@ -82,12 +84,23 @@ var AppStateIOS = { type: string, handler: Function ) { - _appStateHandlers[handler] = RCTDeviceEventEmitter.addListener( - DEVICE_APPSTATE_EVENT, - (appStateData) => { - handler(appStateData.app_state); - } + invariant( + ['change', 'memoryWarning'].indexOf(type) !== -1, + 'Trying to subscribe to unknown event: "%s"', type ); + if (type === 'change') { + _eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener( + 'appStateDidChange', + (appStateData) => { + handler(appStateData.app_state); + } + )); + } else if (type === 'memoryWarning') { + _eventHandlers[type].set(handler, RCTDeviceEventEmitter.addListener( + 'memoryWarning', + handler + )); + } }, /** @@ -97,11 +110,15 @@ var AppStateIOS = { type: string, handler: Function ) { - if (!_appStateHandlers[handler]) { + invariant( + ['change', 'memoryWarning'].indexOf(type) !== -1, + 'Trying to remove listener for unknown event: "%s"', type + ); + if (!_eventHandlers[type].has(handler)) { return; } - _appStateHandlers[handler].remove(); - _appStateHandlers[handler] = null; + _eventHandlers[type].get(handler).remove(); + _eventHandlers[type].delete(handler); }, currentState: (null : ?String), @@ -109,7 +126,7 @@ var AppStateIOS = { }; RCTDeviceEventEmitter.addListener( - DEVICE_APPSTATE_EVENT, + 'appStateDidChange', (appStateData) => { AppStateIOS.currentState = appStateData.app_state; } diff --git a/React/Modules/RCTAppState.m b/React/Modules/RCTAppState.m index 9743e504209704..8c46655c662041 100644 --- a/React/Modules/RCTAppState.m +++ b/React/Modules/RCTAppState.m @@ -53,11 +53,21 @@ - (instancetype)init selector:@selector(handleAppStateDidChange) name:name object:nil]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleMemoryWarning) + name:UIApplicationDidReceiveMemoryWarningNotification + object:nil]; } } return self; } +- (void)handleMemoryWarning +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"memoryWarning" + body:nil]; +} - (void)dealloc { From 55caeadd8ea4b78a1fc46c084449b540eacbfe8a Mon Sep 17 00:00:00 2001 From: Jared Forsyth Date: Fri, 22 May 2015 14:40:03 -0700 Subject: [PATCH 08/44] add the hashtag directory that was missed when updating to babel-eslint From c2854dc3976ac4c83a3e3e7007ca794e603fe214 Mon Sep 17 00:00:00 2001 From: Jared Forsyth Date: Fri, 22 May 2015 15:50:14 -0700 Subject: [PATCH 09/44] [ReactNative] Add missing linter files From 53ea27c7b8f9b275c46392f7cb00752b1f72e094 Mon Sep 17 00:00:00 2001 From: Alex Kotliarskyi Date: Fri, 22 May 2015 16:16:47 -0700 Subject: [PATCH 10/44] [ReactNative] Warn when native component does not exist --- Libraries/ReactIOS/requireNativeComponent.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Libraries/ReactIOS/requireNativeComponent.js b/Libraries/ReactIOS/requireNativeComponent.js index c1c3b137662698..a61ae730b8580e 100644 --- a/Libraries/ReactIOS/requireNativeComponent.js +++ b/Libraries/ReactIOS/requireNativeComponent.js @@ -21,6 +21,7 @@ var pointsDiffer = require('pointsDiffer'); var matricesDiffer = require('matricesDiffer'); var sizesDiffer = require('sizesDiffer'); var verifyPropTypes = require('verifyPropTypes'); +var warning = require('warning'); /** * Used to create React components that directly wrap native component @@ -43,6 +44,7 @@ function requireNativeComponent( ): Function { var viewConfig = RCTUIManager[viewName]; if (!viewConfig || !viewConfig.nativeProps) { + warning(false, 'Native component for "%s" does not exist', viewName); return UnimplementedView; } var nativeProps = { From c6ed85afcc404aa0269d4ba21bc003f4374c5000 Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Fri, 22 May 2015 16:21:58 -0700 Subject: [PATCH 11/44] new XMLHttpRequest().status is a number, not a string Summary: See http://www.w3.org/TR/XMLHttpRequest/#interface-xmlhttprequest Closes https://github.com/facebook/react-native/pull/1356 Github Author: Philipp von Weitershausen Test Plan: Imported from GitHub, without a `Test Plan:` line. --- Libraries/Network/XMLHttpRequestBase.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Libraries/Network/XMLHttpRequestBase.js b/Libraries/Network/XMLHttpRequestBase.js index 99a327b5f236eb..83e44073e31297 100644 --- a/Libraries/Network/XMLHttpRequestBase.js +++ b/Libraries/Network/XMLHttpRequestBase.js @@ -23,7 +23,7 @@ class XMLHttpRequestBase { readyState: number; responseHeaders: ?Object; responseText: ?string; - status: ?string; + status: number; _method: ?string; _url: ?string; @@ -43,7 +43,7 @@ class XMLHttpRequestBase { this.readyState = this.UNSENT; this.responseHeaders = undefined; this.responseText = undefined; - this.status = undefined; + this.status = 0; this._method = null; this._url = null; @@ -127,7 +127,7 @@ class XMLHttpRequestBase { this._aborted = true; } - callback(status: string, responseHeaders: ?Object, responseText: string): void { + callback(status: number, responseHeaders: ?Object, responseText: string): void { if (this._aborted) { return; } From 82a082a79442cf00fea47bd2eee98f187704c0f9 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Fri, 22 May 2015 17:12:10 -0700 Subject: [PATCH 12/44] [react-packager] Introduce buildPackage API --- packager/react-packager/index.js | 35 ++++++++------ .../react-packager/src/Packager/Package.js | 9 ++-- .../src/Packager/__tests__/Package-test.js | 20 ++++++++ .../src/Server/__tests__/Server-test.js | 28 ++++++++++- packager/react-packager/src/Server/index.js | 47 +++++++++++++++---- 5 files changed, 111 insertions(+), 28 deletions(-) diff --git a/packager/react-packager/index.js b/packager/react-packager/index.js index a7b6264b7b740f..6be111997cca48 100644 --- a/packager/react-packager/index.js +++ b/packager/react-packager/index.js @@ -18,14 +18,17 @@ exports.middleware = function(options) { return server.processRequest.bind(server); }; -exports.buildPackageFromUrl = function(options, reqUrl) { - Activity.disable(); - // Don't start the filewatcher or the cache. - if (options.nonPersistent == null) { - options.nonPersistent = true; - } +exports.buildPackage = function(options, packageOptions) { + var server = createServer(options); + return server.buildPackage(packageOptions) + .then(function(p) { + server.end(); + return p; + }); +}; - var server = new Server(options); +exports.buildPackageFromUrl = function(options, reqUrl) { + var server = createServer(options); return server.buildPackageFromUrl(reqUrl) .then(function(p) { server.end(); @@ -34,13 +37,7 @@ exports.buildPackageFromUrl = function(options, reqUrl) { }; exports.getDependencies = function(options, main) { - Activity.disable(); - // Don't start the filewatcher or the cache. - if (options.nonPersistent == null) { - options.nonPersistent = true; - } - - var server = new Server(options); + var server = createServer(options); return server.getDependencies(main) .then(function(r) { server.end(); @@ -60,3 +57,13 @@ function useGracefulFs() { } }); } + +function createServer(options) { + Activity.disable(); + // Don't start the filewatcher or the cache. + if (options.nonPersistent == null) { + options.nonPersistent = true; + } + + return new Server(options); +} diff --git a/packager/react-packager/src/Packager/Package.js b/packager/react-packager/src/Packager/Package.js index 70f57c4ab23c6f..6b53894614932f 100644 --- a/packager/react-packager/src/Packager/Package.js +++ b/packager/react-packager/src/Packager/Package.js @@ -15,6 +15,8 @@ var ModuleTransport = require('../lib/ModuleTransport'); module.exports = Package; +var SOURCEMAPPING_URL = '\n\/\/@ sourceMappingURL='; + function Package(sourceMapUrl) { this._finalized = false; this._modules = []; @@ -96,12 +98,11 @@ Package.prototype.getSource = function(options) { } var source = this._getSource(); - source += '\n\/\/@ sourceMappingURL='; if (options.inlineSourceMap) { - source += this._getInlineSourceMap(); - } else { - source += this._sourceMapUrl; + source += SOURCEMAPPING_URL + this._getInlineSourceMap(); + } else if (this._sourceMapUrl) { + source += SOURCEMAPPING_URL + this._sourceMapUrl; } return source; diff --git a/packager/react-packager/src/Packager/__tests__/Package-test.js b/packager/react-packager/src/Packager/__tests__/Package-test.js index 1d8c3dd12c1d03..d43c65c0f8c06f 100644 --- a/packager/react-packager/src/Packager/__tests__/Package-test.js +++ b/packager/react-packager/src/Packager/__tests__/Package-test.js @@ -47,6 +47,26 @@ describe('Package', function() { ].join('\n')); }); + it('should be ok to leave out the source map url', function() { + var p = new Package(); + p.addModule(new ModuleTransport({ + code: 'transformed foo;', + sourceCode: 'source foo', + sourcePath: 'foo path', + })); + p.addModule(new ModuleTransport({ + code: 'transformed bar;', + sourceCode: 'source bar', + sourcePath: 'bar path', + })); + + p.finalize({}); + expect(p.getSource()).toBe([ + 'transformed foo;', + 'transformed bar;', + ].join('\n')); + }); + it('should create a package and add run module code', function() { ppackage.addModule(new ModuleTransport({ code: 'transformed foo;', diff --git a/packager/react-packager/src/Server/__tests__/Server-test.js b/packager/react-packager/src/Server/__tests__/Server-test.js index 58a1aca798cb6f..e4e7b5088767a0 100644 --- a/packager/react-packager/src/Server/__tests__/Server-test.js +++ b/packager/react-packager/src/Server/__tests__/Server-test.js @@ -230,7 +230,7 @@ describe('processRequest', function() { }); }); - describe.only('/assets endpoint', function() { + describe('/assets endpoint', function() { var AssetServer; beforeEach(function() { AssetServer = require('../../AssetServer'); @@ -257,4 +257,30 @@ describe('processRequest', function() { }); }); + + describe('buildPackage(options)', function() { + it('Calls the packager with the correct args', function() { + server.buildPackage({ + entryFile: 'foo file' + }); + expect(Packager.prototype.package).toBeCalledWith( + 'foo file', + true, + undefined, + true + ); + }); + }); + + describe('buildPackageFromUrl(options)', function() { + it('Calls the packager with the correct args', function() { + server.buildPackageFromUrl('/path/to/foo.bundle?dev=false&runModule=false'); + expect(Packager.prototype.package).toBeCalledWith( + 'path/to/foo.js', + false, + '/path/to/foo.map', + false + ); + }); + }); }); diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index d4da5ae106f4ce..0ce5c584715bd3 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -129,7 +129,7 @@ Server.prototype._onFileChange = function(type, filepath, root) { }; Server.prototype._rebuildPackages = function() { - var buildPackage = this._buildPackage.bind(this); + var buildPackage = this.buildPackage.bind(this); var packages = this._packages; Object.keys(packages).forEach(function(key) { var options = getOptionsFromUrl(key); @@ -171,18 +171,47 @@ Server.prototype.end = function() { ]); }; -Server.prototype._buildPackage = function(options) { +var packageOpts = declareOpts({ + sourceMapUrl: { + type: 'string', + required: false, + }, + entryFile: { + type: 'string', + required: true, + }, + dev: { + type: 'boolean', + default: true, + }, + minify: { + type: 'boolean', + default: false, + }, + runModule: { + type: 'boolean', + default: true, + }, + inlineSourceMap: { + type: 'boolean', + default: false, + }, +}); + +Server.prototype.buildPackage = function(options) { + var opts = packageOpts(options); + return this._packager.package( - options.main, - options.runModule, - options.sourceMapUrl, - options.dev + opts.entryFile, + opts.runModule, + opts.sourceMapUrl, + opts.dev ); }; Server.prototype.buildPackageFromUrl = function(reqUrl) { var options = getOptionsFromUrl(reqUrl); - return this._buildPackage(options); + return this.buildPackage(options); }; Server.prototype.getDependencies = function(main) { @@ -321,7 +350,7 @@ Server.prototype.processRequest = function(req, res, next) { var startReqEventId = Activity.startEvent('request:' + req.url); var options = getOptionsFromUrl(req.url); - var building = this._packages[req.url] || this._buildPackage(options); + var building = this._packages[req.url] || this.buildPackage(options); this._packages[req.url] = building; building.then( @@ -363,7 +392,7 @@ function getOptionsFromUrl(reqUrl) { return { sourceMapUrl: pathname.replace(/\.bundle$/, '.map'), - main: entryFile, + entryFile: entryFile, dev: getBoolOptionFromQuery(urlObj.query, 'dev', true), minify: getBoolOptionFromQuery(urlObj.query, 'minify'), runModule: getBoolOptionFromQuery(urlObj.query, 'runModule', true), From c91e2eb56797f35e0c24681fc16b36b7f198a55c Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Fri, 22 May 2015 19:33:21 -0700 Subject: [PATCH 13/44] Use JSC C API for better invocation speed Summary: This converts the existing JSEvaluateScript call for `require('')..apply(null, );` to native JSC C API methods which shaves off about 10-15% of invocation time on average, I used pretty primitive profiling methods to track the minimum, maximum and average invocation time so would appreciate any extra eyes on the performance. If the argument count is zero the method is invoked directly with no arguments, if the argument count is 1 it's invoked directly with just that argument. If there is more than 1 argument then apply is used and the arguments are passed as the second parameter. Ensured all existing tests pass and instruments leaks shows nothing is leaking. Closes https://github.com/facebook/react-native/pull/1037 Github Author: Robert Payne Test Plan: Imported from GitHub, without a `Test Plan:` line. --- React/Executors/RCTContextExecutor.m | 77 ++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/React/Executors/RCTContextExecutor.m b/React/Executors/RCTContextExecutor.m index ab5efe5a6079ad..89c273481734fd 100644 --- a/React/Executors/RCTContextExecutor.m +++ b/React/Executors/RCTContextExecutor.m @@ -291,21 +291,78 @@ - (void)executeJSCall:(NSString *)name return; } NSError *error; - NSString *argsString = RCTJSONStringify(arguments, &error); + NSString *argsString = (arguments.count == 1) ? RCTJSONStringify(arguments[0], &error) : RCTJSONStringify(arguments, &error); if (!argsString) { RCTLogError(@"Cannot convert argument to string: %@", error); onComplete(nil, error); return; } - NSString *execString = [NSString stringWithFormat:@"require('%@').%@.apply(null, %@);", name, method, argsString]; - JSValueRef jsError = NULL; - JSStringRef execJSString = JSStringCreateWithCFString((__bridge CFStringRef)execString); - JSValueRef result = JSEvaluateScript(strongSelf->_context.ctx, execJSString, NULL, NULL, 0, &jsError); - JSStringRelease(execJSString); + JSValueRef errorJSRef = NULL; + JSValueRef resultJSRef = NULL; + JSGlobalContextRef contextJSRef = JSContextGetGlobalContext(strongSelf->_context.ctx); + JSObjectRef globalObjectJSRef = JSContextGetGlobalObject(strongSelf->_context.ctx); + + // get require + JSStringRef requireNameJSStringRef = JSStringCreateWithUTF8CString("require"); + JSValueRef requireJSRef = JSObjectGetProperty(contextJSRef, globalObjectJSRef, requireNameJSStringRef, &errorJSRef); + JSStringRelease(requireNameJSStringRef); + + if (requireJSRef != NULL && errorJSRef == NULL) { + + // get module + JSStringRef moduleNameJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)name); + JSValueRef moduleNameJSRef = JSValueMakeString(contextJSRef, moduleNameJSStringRef); + JSValueRef moduleJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)requireJSRef, NULL, 1, (const JSValueRef *)&moduleNameJSRef, &errorJSRef); + JSStringRelease(moduleNameJSStringRef); + + if (moduleJSRef != NULL && errorJSRef == NULL) { + + // get method + JSStringRef methodNameJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)method); + JSValueRef methodJSRef = JSObjectGetProperty(contextJSRef, (JSObjectRef)moduleJSRef, methodNameJSStringRef, &errorJSRef); + JSStringRelease(methodNameJSStringRef); + + if (methodJSRef != NULL && errorJSRef == NULL) { + + // direct method invoke with no arguments + if (arguments.count == 0) { + resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)methodJSRef, (JSObjectRef)moduleJSRef, 0, NULL, &errorJSRef); + } + // direct method invoke with 1 argument + else if(arguments.count == 1) { + JSStringRef argsJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)argsString); + JSValueRef argsJSRef = JSValueMakeFromJSONString(contextJSRef, argsJSStringRef); + resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)methodJSRef, (JSObjectRef)moduleJSRef, 1, &argsJSRef, &errorJSRef); + JSStringRelease(argsJSStringRef); + } else { + // apply invoke with array of arguments + + // get apply + JSStringRef applyNameJSStringRef = JSStringCreateWithUTF8CString("apply"); + JSValueRef applyJSRef = JSObjectGetProperty(contextJSRef, (JSObjectRef)methodJSRef, applyNameJSStringRef, &errorJSRef); + JSStringRelease(applyNameJSStringRef); + + if (applyJSRef != NULL && errorJSRef == NULL) { + // invoke apply + JSStringRef argsJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)argsString); + JSValueRef argsJSRef = JSValueMakeFromJSONString(contextJSRef, argsJSStringRef); + + JSValueRef args[2]; + args[0] = JSValueMakeNull(contextJSRef); + args[1] = argsJSRef; + + resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)applyJSRef, (JSObjectRef)methodJSRef, 2, args, &errorJSRef); + JSStringRelease(argsJSStringRef); + } + } + } + } + + } - if (!result) { - onComplete(nil, RCTNSErrorFromJSError(strongSelf->_context.ctx, jsError)); + if (!resultJSRef) { + onComplete(nil, RCTNSErrorFromJSError(contextJSRef, errorJSRef)); return; } @@ -315,8 +372,8 @@ - (void)executeJSCall:(NSString *)name id objcValue; // We often return `null` from JS when there is nothing for native side. JSONKit takes an extra hundred microseconds // to handle this simple case, so we are adding a shortcut to make executeJSCall method even faster - if (!JSValueIsNull(strongSelf->_context.ctx, result)) { - JSStringRef jsJSONString = JSValueCreateJSONString(strongSelf->_context.ctx, result, 0, nil); + if (!JSValueIsNull(contextJSRef, resultJSRef)) { + JSStringRef jsJSONString = JSValueCreateJSONString(contextJSRef, resultJSRef, 0, nil); if (jsJSONString) { NSString *objcJSONString = (__bridge_transfer NSString *)JSStringCopyCFString(kCFAllocatorDefault, jsJSONString); JSStringRelease(jsJSONString); From 9062bda79b4a7b381da5611c94de090ad33dcb4d Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Mon, 25 May 2015 05:19:53 -0700 Subject: [PATCH 14/44] [ReactNative] Add RCTAssertThread and restrict -[UIManager addUIBlock:] to _shadowQueue Summary: @public Add `RCTAssertThread` to `RCTAssert.h` for convenience when checking the current/queue, it accepts either a `NSString *`, `NSThread *` or `dispatch_queue_t` as the object to be checked Also add a check to `-[RCTUIManager addUIBlock:]` - There was a discussion on github (https://github.com/facebook/react-native/issues/1365) due to the weird behavior caused by calling it from a different thread/queue (it might be added after `batchDidComplete` has been called and will just be dispatched on the next call from JS to objc) Test Plan: Change `-[RCTAnimationExperimentalManager methodQueue]` to return `dispatch_get_main_queue()` and run the 2048 example, it should dispatch with a helpful message (screenshot on the comments) --- React/Base/RCTAssert.h | 28 ++++++++++++++++++++++++++++ React/Base/RCTAssert.m | 17 +++++++++++++++++ React/Base/RCTLog.h | 6 ------ React/Base/RCTLog.m | 25 ++++--------------------- React/Base/RCTProfile.m | 4 ++-- React/Modules/RCTUIManager.m | 4 ++++ 6 files changed, 55 insertions(+), 29 deletions(-) diff --git a/React/Base/RCTAssert.h b/React/Base/RCTAssert.h index b0a3c5c52f553f..1fc5b9d321f587 100644 --- a/React/Base/RCTAssert.h +++ b/React/Base/RCTAssert.h @@ -62,3 +62,31 @@ RCT_EXTERN RCTAssertFunction RCTGetAssertFunction(void); * assert info to an extra service without changing the default behavior. */ RCT_EXTERN void RCTAddAssertFunction(RCTAssertFunction assertFunction); + +/** + * Get the current thread's name (or the current queue, if in debug mode) + */ +RCT_EXTERN NSString *RCTCurrentThreadName(void); + +/** + * Convenience macro to assert which thread is currently running (DEBUG mode only) + */ +#if DEBUG + +#define RCTAssertThread(thread, format...) \ +_Pragma("clang diagnostic push") \ +_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \ +RCTAssert( \ + [(id)thread isKindOfClass:[NSString class]] ? \ + [RCTCurrentThreadName() isEqualToString:(NSString *)thread] : \ + [(id)thread isKindOfClass:[NSThread class]] ? \ + [NSThread currentThread] == (NSThread *)thread : \ + dispatch_get_current_queue() == (dispatch_queue_t)thread, \ + format); \ +_Pragma("clang diagnostic pop") + +#else + +#define RCTAssertThread(thread, format...) + +#endif diff --git a/React/Base/RCTAssert.m b/React/Base/RCTAssert.m index 86d71cd80af37c..41369406c4983f 100644 --- a/React/Base/RCTAssert.m +++ b/React/Base/RCTAssert.m @@ -60,3 +60,20 @@ void RCTAddAssertFunction(RCTAssertFunction assertFunction) RCTCurrentAssertFunction = assertFunction; } } + +NSString *RCTCurrentThreadName(void) +{ + NSThread *thread = [NSThread currentThread]; + NSString *threadName = [thread isMainThread] ? @"main" : thread.name; + if (threadName.length == 0) { +#if DEBUG // This is DEBUG not RCT_DEBUG because it *really* must not ship in RC +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + threadName = @(dispatch_queue_get_label(dispatch_get_current_queue())); +#pragma clang diagnostic pop +#else + threadName = [NSString stringWithFormat:@"%p", thread]; +#endif + } + return threadName; +} diff --git a/React/Base/RCTLog.h b/React/Base/RCTLog.h index 75cbe722e09b43..5dead10be900a4 100644 --- a/React/Base/RCTLog.h +++ b/React/Base/RCTLog.h @@ -42,18 +42,12 @@ typedef void (^RCTLogFunction)( NSString *message ); -/** - * Get a given thread's name (or the current queue, if in debug mode) - */ -RCT_EXTERN NSString *RCTThreadName(NSThread *); - /** * A method to generate a string from a collection of log data. To omit any * particular data from the log, just pass nil or zero for the argument. */ RCT_EXTERN NSString *RCTFormatLog( NSDate *timestamp, - NSThread *thread, RCTLogLevel level, NSString *fileName, NSNumber *lineNumber, diff --git a/React/Base/RCTLog.m b/React/Base/RCTLog.m index fb70fe6d3053df..d99bb4fe111d21 100644 --- a/React/Base/RCTLog.m +++ b/React/Base/RCTLog.m @@ -53,7 +53,7 @@ static void RCTLogSetup() ) { NSString *log = RCTFormatLog( - [NSDate date], [NSThread currentThread], level, fileName, lineNumber, message + [NSDate date], level, fileName, lineNumber, message ); fprintf(stderr, "%s\n", log.UTF8String); fflush(stderr); @@ -99,25 +99,8 @@ void RCTPerformBlockWithLogPrefix(void (^block)(void), NSString *prefix) [prefixStack removeLastObject]; } -NSString *RCTThreadName(NSThread *thread) -{ - NSString *threadName = [thread isMainThread] ? @"main" : thread.name; - if (threadName.length == 0) { -#if DEBUG // This is DEBUG not RCT_DEBUG because it *really* must not ship in RC -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - threadName = @(dispatch_queue_get_label(dispatch_get_current_queue())); -#pragma clang diagnostic pop -#else - threadName = [NSString stringWithFormat:@"%p", thread]; -#endif - } - return threadName; -} - NSString *RCTFormatLog( NSDate *timestamp, - NSThread *thread, RCTLogLevel level, NSString *fileName, NSNumber *lineNumber, @@ -137,9 +120,9 @@ void RCTPerformBlockWithLogPrefix(void (^block)(void), NSString *prefix) if (level) { [log appendFormat:@"[%s]", RCTLogLevels[level - 1]]; } - if (thread) { - [log appendFormat:@"[tid:%@]", RCTThreadName(thread)]; - } + + [log appendFormat:@"[tid:%@]", RCTCurrentThreadName()]; + if (fileName) { fileName = [fileName lastPathComponent]; if (lineNumber) { diff --git a/React/Base/RCTProfile.m b/React/Base/RCTProfile.m index 19c6900c73abbc..29e606e8644197 100644 --- a/React/Base/RCTProfile.m +++ b/React/Base/RCTProfile.m @@ -13,8 +13,8 @@ #import +#import "RCTAssert.h" #import "RCTDefines.h" -#import "RCTLog.h" #import "RCTUtils.h" #if RCT_DEV @@ -43,7 +43,7 @@ #define RCTProfileAddEvent(type, props...) \ [RCTProfileInfo[type] addObject:@{ \ @"pid": @([[NSProcessInfo processInfo] processIdentifier]), \ - @"tid": RCTThreadName([NSThread currentThread]), \ + @"tid": RCTCurrentThreadName(), \ props \ }]; diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index e9734c20489cd1..ec12aa6fb143a4 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -390,6 +390,10 @@ - (void)_purgeChildren:(NSArray *)children fromRegistry:(RCTSparseArray *)regist - (void)addUIBlock:(RCTViewManagerUIBlock)block { + RCTAssertThread(_shadowQueue, + @"-[RCTUIManager addUIBlock:] should only be called from the " + "UIManager's _shadowQueue (it may be accessed via `bridge.uiManager.methodQueue`)"); + if (!self.isValid) { return; } From 81401064e5bc0e50de5d77ec9e2384a70e43e2f4 Mon Sep 17 00:00:00 2001 From: Alex Akers Date: Tue, 26 May 2015 02:12:28 -0700 Subject: [PATCH 15/44] [React Native] Fix view clipping when border radius is set --- Examples/UIExplorer/BorderExample.js | 18 +++++ React/Views/RCTView.m | 112 ++++++++++++++++++--------- 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/Examples/UIExplorer/BorderExample.js b/Examples/UIExplorer/BorderExample.js index f49329faef0118..7789459f80bc8e 100644 --- a/Examples/UIExplorer/BorderExample.js +++ b/Examples/UIExplorer/BorderExample.js @@ -80,6 +80,13 @@ var styles = StyleSheet.create({ borderTopLeftRadius: 100, }, + border7: { + borderRadius: 20, + }, + border7_inner: { + backgroundColor: 'blue', + flex: 1, + }, }); exports.title = 'Border'; @@ -134,4 +141,15 @@ exports.examples = [ return ; } }, + { + title: 'Custom Borders', + description: 'borderRadius & clipping', + render() { + return ( + + + + ); + } + }, ]; diff --git a/React/Views/RCTView.m b/React/Views/RCTView.m index 61f8240e892dfb..08f713d9d22555 100644 --- a/React/Views/RCTView.m +++ b/React/Views/RCTView.m @@ -15,6 +15,8 @@ #import "RCTUtils.h" #import "UIView+React.h" +static const CGFloat RCTViewBorderThreshold = 0.001; + static UIView *RCTViewHitTest(UIView *view, CGPoint point, UIEvent *event) { for (UIView *subview in [view.subviews reverseObjectEnumerator]) { @@ -436,19 +438,21 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor if ([_backgroundColor isEqual:backgroundColor]) { return; } + _backgroundColor = backgroundColor; [self.layer setNeedsDisplay]; } -- (UIImage *)generateBorderImage:(out CGRect *)contentsCenter +- (UIImage *)borderImage:(out CGRect *)contentsCenter { - static const CGFloat threshold = 0.001; - - const CGFloat maxRadius = MIN(self.bounds.size.height, self.bounds.size.width); + const CGFloat maxRadius = ({ + const CGRect bounds = self.bounds; + MIN(bounds.size.height, bounds.size.width); + }); const CGFloat radius = MAX(0, _borderRadius); - const CGFloat topLeftRadius = MIN(_borderTopLeftRadius >= 0 ? _borderTopLeftRadius : radius, maxRadius); - const CGFloat topRightRadius = MIN(_borderTopRightRadius >= 0 ? _borderTopRightRadius : radius, maxRadius); - const CGFloat bottomLeftRadius = MIN(_borderBottomLeftRadius >= 0 ? _borderBottomLeftRadius : radius, maxRadius); + const CGFloat topLeftRadius = MIN(_borderTopLeftRadius >= 0 ? _borderTopLeftRadius : radius, maxRadius); + const CGFloat topRightRadius = MIN(_borderTopRightRadius >= 0 ? _borderTopRightRadius : radius, maxRadius); + const CGFloat bottomLeftRadius = MIN(_borderBottomLeftRadius >= 0 ? _borderBottomLeftRadius : radius, maxRadius); const CGFloat bottomRightRadius = MIN(_borderBottomRightRadius >= 0 ? _borderBottomRightRadius : radius, maxRadius); const CGFloat borderWidth = MAX(0, _borderWidth); @@ -457,14 +461,10 @@ - (UIImage *)generateBorderImage:(out CGRect *)contentsCenter const CGFloat bottomWidth = _borderBottomWidth >= 0 ? _borderBottomWidth : borderWidth; const CGFloat leftWidth = _borderLeftWidth >= 0 ? _borderLeftWidth : borderWidth; - if (topLeftRadius < threshold && - topRightRadius < threshold && - bottomLeftRadius < threshold && - bottomRightRadius < threshold && - topWidth < threshold && - rightWidth < threshold && - bottomWidth < threshold && - leftWidth < threshold) { + if (topLeftRadius < RCTViewBorderThreshold && topRightRadius < RCTViewBorderThreshold && + bottomLeftRadius < RCTViewBorderThreshold && bottomRightRadius < RCTViewBorderThreshold && + topWidth < RCTViewBorderThreshold && rightWidth < RCTViewBorderThreshold && + bottomWidth < RCTViewBorderThreshold && leftWidth < RCTViewBorderThreshold) { return nil; } @@ -486,14 +486,21 @@ - (UIImage *)generateBorderImage:(out CGRect *)contentsCenter UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); CGContextRef ctx = UIGraphicsGetCurrentContext(); - const CGRect rect = {CGPointZero, size}; - CGPathRef path = RCTPathCreateWithRoundedRect(rect, topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomLeftRadius, bottomLeftRadius, bottomRightRadius, bottomRightRadius, NULL); + const CGRect rect = {.size = size}; + + CGPathRef path; + const BOOL hasClipping = self.clipsToBounds; + if (hasClipping) { + path = CGPathCreateWithRect(rect, NULL); + } else { + path = RCTPathCreateWithRoundedRect(rect, topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomLeftRadius, bottomLeftRadius, bottomRightRadius, bottomRightRadius, NULL); + } if (_backgroundColor) { CGContextSaveGState(ctx); - CGContextAddPath(ctx, path); CGContextSetFillColorWithColor(ctx, _backgroundColor.CGColor); + CGContextAddPath(ctx, path); CGContextFillPath(ctx); CGContextRestoreGState(ctx); @@ -502,29 +509,18 @@ - (UIImage *)generateBorderImage:(out CGRect *)contentsCenter CGContextAddPath(ctx, path); CGPathRelease(path); - BOOL hasRadius = topLeftRadius > 0 || topRightRadius > 0 || bottomLeftRadius > 0 || bottomRightRadius > 0; - if (hasRadius && topWidth > 0 && rightWidth > 0 && bottomWidth > 0 && leftWidth > 0) { - const UIEdgeInsets insetEdgeInsets = UIEdgeInsetsMake(topWidth, leftWidth, bottomWidth, rightWidth); - const CGRect insetRect = UIEdgeInsetsInsetRect(rect, insetEdgeInsets); - CGPathRef insetPath = RCTPathCreateWithRoundedRect(insetRect, innerTopLeftRadiusX, innerTopLeftRadiusY, innerTopRightRadiusX, innerTopRightRadiusY, innerBottomLeftRadiusX, innerBottomLeftRadiusY, innerBottomRightRadiusX, innerBottomRightRadiusY, NULL); - CGContextAddPath(ctx, insetPath); - CGPathRelease(insetPath); - } + const BOOL hasRadius = topLeftRadius > 0 || topRightRadius > 0 || bottomLeftRadius > 0 || bottomRightRadius > 0; + const UIEdgeInsets insetEdgeInsets = UIEdgeInsetsMake(topWidth, leftWidth, bottomWidth, rightWidth); + CGPathRef insetPath = RCTPathCreateWithRoundedRect(UIEdgeInsetsInsetRect(rect, insetEdgeInsets), innerTopLeftRadiusX, innerTopLeftRadiusY, innerTopRightRadiusX, innerTopRightRadiusY, innerBottomLeftRadiusX, innerBottomLeftRadiusY, innerBottomRightRadiusX, innerBottomRightRadiusY, NULL); + CGContextAddPath(ctx, insetPath); CGContextEOClip(ctx); BOOL hasEqualColor = !_borderTopColor && !_borderRightColor && !_borderBottomColor && !_borderLeftColor; - BOOL hasEqualBorder = _borderWidth >= 0 && _borderTopWidth < 0 && _borderRightWidth < 0 && _borderBottomWidth < 0 && _borderLeftWidth < 0; - if (!hasRadius && hasEqualBorder && hasEqualColor) { - CGContextSetStrokeColorWithColor(ctx, _borderColor); - CGContextSetLineWidth(ctx, 2 * _borderWidth); - CGContextClipToRect(ctx, rect); - CGContextStrokeRect(ctx, rect); - } else if (!hasRadius && hasEqualColor) { + if ((hasClipping || !hasRadius) && hasEqualColor) { CGContextSetFillColorWithColor(ctx, _borderColor); CGContextAddRect(ctx, rect); - const CGRect insetRect = UIEdgeInsetsInsetRect(rect, edgeInsets); - CGContextAddRect(ctx, insetRect); + CGContextAddPath(ctx, insetPath); CGContextEOFillPath(ctx); } else { BOOL didSet = NO; @@ -660,6 +656,8 @@ - (UIImage *)generateBorderImage:(out CGRect *)contentsCenter } } + CGPathRelease(insetPath); + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); @@ -669,8 +667,8 @@ - (UIImage *)generateBorderImage:(out CGRect *)contentsCenter - (void)displayLayer:(CALayer *)layer { - CGRect contentsCenter = (CGRect){CGPointZero, {1, 1}}; - UIImage *image = [self generateBorderImage:&contentsCenter]; + CGRect contentsCenter = {.size = {1, 1}}; + UIImage *image = [self borderImage:&contentsCenter]; if (image && RCTRunningInTestEnvironment()) { const CGSize size = self.bounds.size; @@ -685,6 +683,46 @@ - (void)displayLayer:(CALayer *)layer layer.contentsCenter = contentsCenter; layer.contentsScale = image.scale ?: 1.0; layer.magnificationFilter = kCAFilterNearest; + layer.needsDisplayOnBoundsChange = image != nil; + + [self updateClippingForLayer:layer]; +} + +- (void)updateClippingForLayer:(CALayer *)layer +{ + CALayer *mask = nil; + CGFloat cornerRadius = 0; + + if (self.clipsToBounds) { + if (_borderRadius > 0 && _borderTopLeftRadius < 0 && _borderTopRightRadius < 0 && _borderBottomLeftRadius < 0 && _borderBottomRightRadius < 0) { + cornerRadius = _borderRadius; + } else { + const CGRect bounds = layer.bounds; + const CGFloat maxRadius = MIN(bounds.size.height, bounds.size.width); + const CGFloat radius = MAX(0, _borderRadius); + const CGFloat topLeftRadius = MIN(_borderTopLeftRadius >= 0 ? _borderTopLeftRadius : radius, maxRadius); + const CGFloat topRightRadius = MIN(_borderTopRightRadius >= 0 ? _borderTopRightRadius : radius, maxRadius); + const CGFloat bottomLeftRadius = MIN(_borderBottomLeftRadius >= 0 ? _borderBottomLeftRadius : radius, maxRadius); + const CGFloat bottomRightRadius = MIN(_borderBottomRightRadius >= 0 ? _borderBottomRightRadius : radius, maxRadius); + + if (ABS(topLeftRadius - topRightRadius) < RCTViewBorderThreshold && + ABS(topLeftRadius - bottomLeftRadius) < RCTViewBorderThreshold && + ABS(topLeftRadius - bottomRightRadius) < RCTViewBorderThreshold) { + cornerRadius = topLeftRadius; + } else { + CAShapeLayer *shapeLayer = [CAShapeLayer layer]; + + CGPathRef path = RCTPathCreateWithRoundedRect(bounds, topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomLeftRadius, bottomLeftRadius, bottomRightRadius, bottomRightRadius, NULL); + shapeLayer.path = path; + CGPathRelease(path); + + mask = shapeLayer; + } + } + } + + layer.cornerRadius = cornerRadius; + layer.mask = mask; } #pragma mark Border Color From b7c669aa737e06f474bc582a296911bcb35d5781 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Tue, 26 May 2015 04:14:31 -0700 Subject: [PATCH 16/44] Fixed root view background color propagation --- Libraries/Text/RCTTextManager.m | 6 +--- React/Base/RCTRootView.m | 28 ++++++++++++++--- React/Modules/RCTUIManager.h | 8 ++++- React/Modules/RCTUIManager.m | 35 ++++++++++++++++----- React/Views/RCTShadowView.h | 1 - React/Views/RCTShadowView.m | 2 ++ React/Views/RCTViewManager.m | 54 +++++++++++++++------------------ 7 files changed, 86 insertions(+), 48 deletions(-) diff --git a/Libraries/Text/RCTTextManager.m b/Libraries/Text/RCTTextManager.m index b2832a80066a9c..31ae67c039bb81 100644 --- a/Libraries/Text/RCTTextManager.m +++ b/Libraries/Text/RCTTextManager.m @@ -51,11 +51,7 @@ - (RCTShadowView *)shadowView RCT_EXPORT_SHADOW_PROPERTY(shadowOffset, CGSize) RCT_EXPORT_SHADOW_PROPERTY(textAlign, NSTextAlignment) RCT_REMAP_SHADOW_PROPERTY(backgroundColor, textBackgroundColor, UIColor) -RCT_CUSTOM_SHADOW_PROPERTY(containerBackgroundColor, UIColor, RCTShadowText) -{ - view.backgroundColor = json ? [RCTConvert UIColor:json] : defaultView.backgroundColor; - view.isBGColorExplicitlySet = json ? YES : defaultView.isBGColorExplicitlySet; -} +RCT_REMAP_SHADOW_PROPERTY(containerBackgroundColor, backgroundColor, UIColor) RCT_CUSTOM_SHADOW_PROPERTY(numberOfLines, NSInteger, RCTShadowText) { NSLineBreakMode truncationMode = NSLineBreakByClipping; diff --git a/React/Base/RCTRootView.m b/React/Base/RCTRootView.m index 980d253d317ff4..23100f5e259126 100644 --- a/React/Base/RCTRootView.m +++ b/React/Base/RCTRootView.m @@ -48,10 +48,11 @@ @implementation RCTRootView RCTBridge *_bridge; NSString *_moduleName; NSDictionary *_launchOptions; + UIColor *_backgroundColor; RCTRootContentView *_contentView; } - - (instancetype)initWithBridge:(RCTBridge *)bridge +- (instancetype)initWithBridge:(RCTBridge *)bridge moduleName:(NSString *)moduleName { RCTAssertMainThread(); @@ -87,6 +88,17 @@ - (instancetype)initWithBundleURL:(NSURL *)bundleURL return [self initWithBridge:bridge moduleName:moduleName]; } +- (void)setBackgroundColor:(UIColor *)backgroundColor +{ + _backgroundColor = backgroundColor; + _contentView.backgroundColor = backgroundColor; +} + +- (UIColor *)backgroundColor +{ + return _backgroundColor; +} + - (UIViewController *)backingViewController { return _backingViewController ?: [super backingViewController]; @@ -100,7 +112,6 @@ - (BOOL)canBecomeFirstResponder RCT_IMPORT_METHOD(AppRegistry, runApplication) RCT_IMPORT_METHOD(ReactNative, unmountComponentAtNodeAndRemoveContainer) - - (void)javaScriptDidLoad:(NSNotification *)notification { RCTBridge *bridge = notification.userInfo[@"bridge"]; @@ -124,6 +135,7 @@ - (void)bundleFinishedLoading:(RCTBridge *)bridge [_contentView removeFromSuperview]; _contentView = [[RCTRootContentView alloc] initWithFrame:self.bounds bridge:bridge]; + _contentView.backgroundColor = _backgroundColor; [self addSubview:_contentView]; NSString *moduleName = _moduleName ?: @""; @@ -187,9 +199,17 @@ - (instancetype)initWithFrame:(CGRect)frame - (void)setFrame:(CGRect)frame { - [super setFrame:frame]; + super.frame = frame; + if (self.reactTag && _bridge.isValid) { + [_bridge.uiManager setFrame:frame forRootView:self]; + } +} + +- (void)setBackgroundColor:(UIColor *)backgroundColor +{ + super.backgroundColor = backgroundColor; if (self.reactTag && _bridge.isValid) { - [_bridge.uiManager setFrame:self.bounds forRootView:self]; + [_bridge.uiManager setBackgroundColor:backgroundColor forRootView:self]; } } diff --git a/React/Modules/RCTUIManager.h b/React/Modules/RCTUIManager.h index 6fa0e3249185d3..829c1aeff1cca7 100644 --- a/React/Modules/RCTUIManager.h +++ b/React/Modules/RCTUIManager.h @@ -36,10 +36,16 @@ /** * Update the frame of a root view. This might be in response to a screen rotation - * or some other layout event outsde of the React-managed view hierarchy. + * or some other layout event outside of the React-managed view hierarchy. */ - (void)setFrame:(CGRect)frame forRootView:(UIView *)rootView; +/** + * Update the background color of a root view. This is usually triggered by + * manually setting the background color of the root view with native code. + */ +- (void)setBackgroundColor:(UIColor *)color forRootView:(UIView *)rootView; + /** * Schedule a block to be executed on the UI thread. Useful if you need to execute * view logic after all currently queued view updates have completed. diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index ec12aa6fb143a4..6b88f05d3cadec 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -338,17 +338,14 @@ - (void)registerRootView:(UIView *)rootView; _viewRegistry[reactTag] = rootView; CGRect frame = rootView.frame; - // Register manager (TODO: should we do this, or leave it nil?) - _viewManagerRegistry[reactTag] = _viewManagers[@"RCTView"]; - // Register shadow view dispatch_async(_shadowQueue, ^{ RCTShadowView *shadowView = [[RCTShadowView alloc] init]; shadowView.reactTag = reactTag; shadowView.frame = frame; - shadowView.backgroundColor = [UIColor whiteColor]; + shadowView.backgroundColor = rootView.backgroundColor; + shadowView.viewName = NSStringFromClass([rootView class]); _shadowViewRegistry[shadowView.reactTag] = shadowView; - [_rootViewTags addObject:reactTag]; }); } @@ -372,6 +369,22 @@ - (void)setFrame:(CGRect)frame forRootView:(UIView *)rootView }); } +- (void)setBackgroundColor:(UIColor *)color forRootView:(UIView *)rootView +{ + RCTAssertMainThread(); + + NSNumber *reactTag = rootView.reactTag; + RCTAssert(RCTIsReactRootView(reactTag), @"Specified view %@ is not a root view", reactTag); + + dispatch_async(_shadowQueue, ^{ + RCTShadowView *rootShadowView = _shadowViewRegistry[reactTag]; + RCTAssert(rootShadowView != nil, @"Could not locate root view with tag #%@", reactTag); + rootShadowView.backgroundColor = color; + [self _amendPendingUIBlocksWithStylePropagationUpdateForRootView:rootShadowView]; + [self flushUIBlocks]; + }); +} + /** * Unregisters views from registries */ @@ -799,6 +812,11 @@ static void RCTSetShadowViewProps(NSDictionary *props, RCTShadowView *shadowView } _shadowViewRegistry[reactTag] = shadowView; + // Shadow view is the source of truth for background color this is a little + // bit counter-intuitive if people try to set background color when setting up + // the view, but it's the only way that makes sense given our threading model + UIColor *backgroundColor = shadowView.backgroundColor; + [self addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry){ RCTAssertMainThread(); @@ -807,14 +825,15 @@ static void RCTSetShadowViewProps(NSDictionary *props, RCTShadowView *shadowView // Generate default view, used for resetting default props if (!uiManager->_defaultViews[viewName]) { - // Note the default is setup after the props are read for the first time ever - // for this className - this is ok because we only use the default for restoring - // defaults, which never happens on first creation. + // Note the default is setup after the props are read for the first time + // ever for this className - this is ok because we only use the default + // for restoring defaults, which never happens on first creation. uiManager->_defaultViews[viewName] = [manager view]; } // Set properties view.reactTag = reactTag; + view.backgroundColor = backgroundColor; if ([view isKindOfClass:[UIView class]]) { view.multipleTouchEnabled = YES; view.userInteractionEnabled = YES; // required for touch handling diff --git a/React/Views/RCTShadowView.h b/React/Views/RCTShadowView.h index 83350ac469cb7e..c2e10750d3a5a8 100644 --- a/React/Views/RCTShadowView.h +++ b/React/Views/RCTShadowView.h @@ -38,7 +38,6 @@ typedef void (^RCTApplierBlock)(RCTSparseArray *); @property (nonatomic, weak, readonly) RCTShadowView *superview; @property (nonatomic, assign, readonly) css_node_t *cssNode; @property (nonatomic, copy) NSString *viewName; -@property (nonatomic, assign) BOOL isBGColorExplicitlySet; // Used to propagate to children @property (nonatomic, strong) UIColor *backgroundColor; // Used to propagate to children @property (nonatomic, assign) RCTUpdateLifecycle layoutLifecycle; @property (nonatomic, assign) BOOL hasOnLayout; diff --git a/React/Views/RCTShadowView.m b/React/Views/RCTShadowView.m index e5975dff269dbc..7a0e9e421389db 100644 --- a/React/Views/RCTShadowView.m +++ b/React/Views/RCTShadowView.m @@ -38,6 +38,7 @@ @implementation RCTShadowView NSMutableArray *_reactSubviews; BOOL _recomputePadding; BOOL _recomputeMargin; + BOOL _isBGColorExplicitlySet; float _paddingMetaProps[META_PROP_COUNT]; float _marginMetaProps[META_PROP_COUNT]; } @@ -515,6 +516,7 @@ - (type)getProp \ - (void)setBackgroundColor:(UIColor *)color { _backgroundColor = color; + _isBGColorExplicitlySet = YES; [self dirtyPropagation]; } diff --git a/React/Views/RCTViewManager.m b/React/Views/RCTViewManager.m index 8230e398d915d4..311167761090b4 100644 --- a/React/Views/RCTViewManager.m +++ b/React/Views/RCTViewManager.m @@ -228,36 +228,38 @@ - (RCTViewEventHandler)eventHandlerWithName:(NSString *)eventName json:(id)json #pragma mark - ShadowView properties -RCT_EXPORT_SHADOW_PROPERTY(top, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(right, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(bottom, CGFloat); +RCT_EXPORT_SHADOW_PROPERTY(backgroundColor, UIColor) + +RCT_EXPORT_SHADOW_PROPERTY(top, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(right, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(bottom, CGFloat) RCT_EXPORT_SHADOW_PROPERTY(left, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(width, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(height, CGFloat); +RCT_EXPORT_SHADOW_PROPERTY(width, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(height, CGFloat) -RCT_EXPORT_SHADOW_PROPERTY(borderTopWidth, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(borderRightWidth, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(borderBottomWidth, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(borderLeftWidth, CGFloat); +RCT_EXPORT_SHADOW_PROPERTY(borderTopWidth, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(borderRightWidth, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(borderBottomWidth, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(borderLeftWidth, CGFloat) RCT_CUSTOM_SHADOW_PROPERTY(borderWidth, CGFloat, RCTShadowView) { [view setBorderWidth:[RCTConvert CGFloat:json]]; } -RCT_EXPORT_SHADOW_PROPERTY(marginTop, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(marginRight, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(marginBottom, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(marginLeft, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(marginVertical, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(marginHorizontal, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(margin, CGFloat); - -RCT_EXPORT_SHADOW_PROPERTY(paddingTop, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(paddingRight, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(paddingBottom, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(paddingLeft, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(paddingVertical, CGFloat); -RCT_EXPORT_SHADOW_PROPERTY(paddingHorizontal, CGFloat); +RCT_EXPORT_SHADOW_PROPERTY(marginTop, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(marginRight, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(marginBottom, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(marginLeft, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(marginVertical, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(marginHorizontal, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(margin, CGFloat) + +RCT_EXPORT_SHADOW_PROPERTY(paddingTop, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(paddingRight, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(paddingBottom, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(paddingLeft, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(paddingVertical, CGFloat) +RCT_EXPORT_SHADOW_PROPERTY(paddingHorizontal, CGFloat) RCT_EXPORT_SHADOW_PROPERTY(padding, CGFloat); RCT_EXPORT_SHADOW_PROPERTY(flex, CGFloat) @@ -268,12 +270,6 @@ - (RCTViewEventHandler)eventHandlerWithName:(NSString *)eventName json:(id)json RCT_EXPORT_SHADOW_PROPERTY(alignSelf, css_align_t) RCT_REMAP_SHADOW_PROPERTY(position, positionType, css_position_type_t) -RCT_CUSTOM_SHADOW_PROPERTY(backgroundColor, UIColor, RCTShadowView) -{ - view.backgroundColor = json ? [RCTConvert UIColor:json] : defaultView.backgroundColor; - view.isBGColorExplicitlySet = json ? YES : defaultView.isBGColorExplicitlySet; -} - RCT_REMAP_SHADOW_PROPERTY(onLayout, hasOnLayout, BOOL) @end From 7069e4cd3f1228e0508988ecdee2afb3899aedfc Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Tue, 26 May 2015 08:12:59 -0700 Subject: [PATCH 17/44] XMLHttpRequest: ignore case for request headers Summary: HTTP headers are case-insensitive, so we should treat them that way when they're being set on `XMLHttpRequest`. Closes https://github.com/facebook/react-native/pull/1381 Github Author: Philipp von Weitershausen Test Plan: Imported from GitHub, without a `Test Plan:` line. --- Libraries/Network/XMLHttpRequestBase.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Network/XMLHttpRequestBase.js b/Libraries/Network/XMLHttpRequestBase.js index 83e44073e31297..3570e4bf29ef36 100644 --- a/Libraries/Network/XMLHttpRequestBase.js +++ b/Libraries/Network/XMLHttpRequestBase.js @@ -73,7 +73,7 @@ class XMLHttpRequestBase { } setRequestHeader(header: string, value: any): void { - this._headers[header] = value; + this._headers[header.toLowerCase()] = value; } open(method: string, url: string, async: ?boolean): void { From 4273af9e29ba9f2191a1d29d2961bcdad206399e Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Tue, 26 May 2015 10:28:33 -0700 Subject: [PATCH 18/44] Allow uploading a native file (e.g. photo) via XMLHttpRequest Summary: With this in place, it's possible to upload a picture from the `CameraRoll` to Parse, for instance: xhr = new XMLHttpRequest(); xhr.onload = function() { data = JSON.parse(xhr.responseText); var parseFile = new Parse.File(data.name); parseFile._url = data.url; callback(parseFile); }; xhr.setRequestHeader('X-Parse-Application-Id', appID); xhr.setRequestHeader('X-Parse-JavaScript-Key', appKey); xhr.open('POST', 'https://api.parse.com/1/files/image.jpg'); // assetURI as provided e.g. by the CameraRoll API xhr.send(new NativeFile(assetURI)); Closes https://github.com/facebook/react-native/pull/1357 Github Author: Philipp von Weitershausen Test Plan: Imported from GitHub, without a `Test Plan:` line. --- Libraries/Image/RCTImageLoader.h | 2 + Libraries/Image/RCTImageLoader.m | 7 +++ Libraries/Network/RCTDataManager.m | 43 ++++++++++++++++--- .../RCTNetwork.xcodeproj/project.pbxproj | 2 + Libraries/Network/XMLHttpRequest.ios.js | 11 +++-- 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/Libraries/Image/RCTImageLoader.h b/Libraries/Image/RCTImageLoader.h index 186a53cd1046b0..4d23a628e733e5 100644 --- a/Libraries/Image/RCTImageLoader.h +++ b/Libraries/Image/RCTImageLoader.h @@ -23,4 +23,6 @@ + (void)loadImageWithTag:(NSString *)tag callback:(void (^)(NSError *error, id /* UIImage or CAAnimation */ image))callback; ++ (BOOL)isSystemImageURI:(NSString *)uri; + @end diff --git a/Libraries/Image/RCTImageLoader.m b/Libraries/Image/RCTImageLoader.m index 7525b37d0a3273..25cb9598817b93 100644 --- a/Libraries/Image/RCTImageLoader.m +++ b/Libraries/Image/RCTImageLoader.m @@ -154,4 +154,11 @@ + (void)loadImageWithTag:(NSString *)imageTag callback:(void (^)(NSError *error, } } ++ (BOOL)isSystemImageURI:(NSString *)uri +{ + return uri != nil && ( + [uri hasPrefix:@"assets-library"] || + [uri hasPrefix:@"ph://"]); +} + @end diff --git a/Libraries/Network/RCTDataManager.m b/Libraries/Network/RCTDataManager.m index 1d0a793de8e454..f6479548235cd2 100644 --- a/Libraries/Network/RCTDataManager.m +++ b/Libraries/Network/RCTDataManager.m @@ -13,6 +13,7 @@ #import "RCTConvert.h" #import "RCTLog.h" #import "RCTUtils.h" +#import "RCTImageLoader.h" @implementation RCTDataManager @@ -34,8 +35,43 @@ @implementation RCTDataManager NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = [RCTConvert NSString:query[@"method"]] ?: @"GET"; request.allHTTPHeaderFields = [RCTConvert NSDictionary:query[@"headers"]]; - request.HTTPBody = [RCTConvert NSData:query[@"data"]]; + if (query[@"data"] != [NSNull null]) { + NSDictionary *data = [RCTConvert NSDictionary:query[@"data"]]; + NSData *body = [RCTConvert NSData:data[@"string"]]; + if (body != nil) { + request.HTTPBody = body; + [RCTDataManager sendRequest:request responseSender:responseSender]; + return; + } + NSString *uri = [RCTConvert NSString:data[@"uri"]]; + if (uri != nil) { + if ([RCTImageLoader isSystemImageURI:uri]) { + [RCTImageLoader loadImageWithTag:(NSString *)uri callback:^(NSError *error, UIImage *image) { + if (error) { + RCTLogError(@"Error loading image URI: %@", error); + // We should really circle back to JS here and notify an error/abort on the request. + return; + } + NSData *imageData = UIImageJPEGRepresentation(image, 1.0); + request.HTTPBody = imageData; + [RCTDataManager sendRequest:request responseSender:responseSender]; + }]; + } else { + RCTLogError(@"Cannot resolve URI: %@", uri); + } + return; + } + } + + // There was no data payload, or we couldn't understand it. + [RCTDataManager sendRequest:request responseSender:responseSender]; + } else { + RCTLogError(@"unsupported query type %@", queryType); + } +} + ++ (void)sendRequest:(NSURLRequest *)request responseSender:(RCTResponseSenderBlock)responseSender { // Build data task NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *connectionError) { @@ -71,11 +107,6 @@ @implementation RCTDataManager }]; [task resume]; - - } else { - - RCTLogError(@"unsupported query type %@", queryType); - } } @end diff --git a/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj b/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj index 1dca7fe6de27bf..28bd23c8622a20 100644 --- a/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj +++ b/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj @@ -209,6 +209,7 @@ "$(inherited)", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, "$(SRCROOT)/../../React/**", + "$(SRCROOT)/../Image/**", ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; @@ -226,6 +227,7 @@ "$(inherited)", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, "$(SRCROOT)/../../React/**", + "$(SRCROOT)/../Image/**", ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; diff --git a/Libraries/Network/XMLHttpRequest.ios.js b/Libraries/Network/XMLHttpRequest.ios.js index 6c7367c18db725..fca690e5955c6f 100644 --- a/Libraries/Network/XMLHttpRequest.ios.js +++ b/Libraries/Network/XMLHttpRequest.ios.js @@ -20,13 +20,16 @@ var XMLHttpRequestBase = require('XMLHttpRequestBase'); class XMLHttpRequest extends XMLHttpRequestBase { sendImpl(method: ?string, url: ?string, headers: Object, data: any): void { + if (typeof data === 'string') { + data = {string: data}; + } RCTDataManager.queryData( 'http', { - method: method, - url: url, - data: data, - headers: headers, + method, + url, + data, + headers, }, // TODO: Do we need this? is it used anywhere? 'h' + crc32(method + '|' + url + '|' + data), From cfa4b1347291a099ccbd759e8351eab64e6fb01b Mon Sep 17 00:00:00 2001 From: Alex Kotliarskyi Date: Tue, 26 May 2015 11:16:25 -0700 Subject: [PATCH 19/44] [ReactNative] Element Inspector Summary: This adds new development feature to React Native that provides information about selected element (see the demo in Test Plan). This is how it works: App's root component is rendered to a container that also has a hidden layer called ``. When activated, it shows full screen view and captures all touches. On every touch we ask UIManager to find a view for given {x,y} coordinates. Then, we use React's internals to find corresponding React component. `setRootInstance` is used to remember the top level component to start search from, lmk if you have a better idea how to do this. Given a component, we can climb up its owners tree to provice more context on how/where the component is used. In future we could use the `hierarchy` array to inspect and print their props/state. Known bugs and limitations: * InspectorOverlay sometimes receives touches with incorrect coordinates (wtf) * Not integrated with React Chrome Devtools (maybe in followup diffs) * Doesn't work with popovers (maybe put the element inspector into an ``?) @public Test Plan: https://www.facebook.com/pxlcld/mn5k Works nicely with scrollviews --- Libraries/Inspector.js | 62 +++++++++++ Libraries/ReactIOS/InspectorOverlay.js | 112 ++++++++++++++++++++ Libraries/ReactIOS/renderApplication.ios.js | 51 +++++++-- React/Base/RCTDevMenu.m | 9 +- React/Modules/RCTUIManager.m | 25 +++++ 5 files changed, 252 insertions(+), 7 deletions(-) create mode 100644 Libraries/Inspector.js create mode 100644 Libraries/ReactIOS/InspectorOverlay.js diff --git a/Libraries/Inspector.js b/Libraries/Inspector.js new file mode 100644 index 00000000000000..5c70be2e8686a2 --- /dev/null +++ b/Libraries/Inspector.js @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule Inspector + */ +'use strict'; + +var ReactInstanceHandles = require('ReactInstanceHandles'); +var ReactInstanceMap = require('ReactInstanceMap'); +var ReactNativeMount = require('ReactNativeMount'); +var ReactNativeTagHandles = require('ReactNativeTagHandles'); + +function traverseOwnerTreeUp(hierarchy, instance) { + if (instance) { + hierarchy.unshift(instance); + traverseOwnerTreeUp(hierarchy, instance._currentElement._owner); + } +} + +function findInstance(component, targetID) { + if (targetID === findRootNodeID(component)) { + return component; + } + if (component._renderedComponent) { + return findInstance(component._renderedComponent, targetID); + } else { + for (var key in component._renderedChildren) { + var child = component._renderedChildren[key]; + if (ReactInstanceHandles.isAncestorIDOf(findRootNodeID(child), targetID)) { + var instance = findInstance(child, targetID); + if (instance) { + return instance; + } + } + } + } +} + +function findRootNodeID(component) { + var internalInstance = ReactInstanceMap.get(component); + return internalInstance ? internalInstance._rootNodeID : component._rootNodeID; +} + +function findInstanceByNativeTag(rootTag, nativeTag) { + var containerID = ReactNativeTagHandles.tagToRootNodeID[rootTag]; + var rootInstance = ReactNativeMount._instancesByContainerID[containerID]; + var targetID = ReactNativeTagHandles.tagToRootNodeID[nativeTag]; + return findInstance(rootInstance, targetID); +} + +function getOwnerHierarchy(instance) { + var hierarchy = []; + traverseOwnerTreeUp(hierarchy, instance); + return hierarchy; +} + +module.exports = {findInstanceByNativeTag, getOwnerHierarchy}; diff --git a/Libraries/ReactIOS/InspectorOverlay.js b/Libraries/ReactIOS/InspectorOverlay.js new file mode 100644 index 00000000000000..a70d3b85d43f84 --- /dev/null +++ b/Libraries/ReactIOS/InspectorOverlay.js @@ -0,0 +1,112 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule InspectorOverlay + */ +'use strict'; + +var Dimensions = require('Dimensions'); +var Inspector = require('Inspector'); +var React = require('React'); +var StyleSheet = require('StyleSheet'); +var Text = require('Text'); +var UIManager = require('NativeModules').UIManager; +var View = require('View'); + +var InspectorOverlay = React.createClass({ + getInitialState: function() { + return { + frame: null, + hierarchy: [], + }; + }, + + findViewForTouchEvent: function(e) { + var {locationX, locationY} = e.nativeEvent.touches[0]; + UIManager.findSubviewIn( + this.props.inspectedViewTag, + [locationX, locationY], + (nativeViewTag, left, top, width, height) => { + var instance = Inspector.findInstanceByNativeTag(this.props.rootTag, nativeViewTag); + var hierarchy = Inspector.getOwnerHierarchy(instance); + this.setState({ + hierarchy, + frame: {left, top, width, height} + }); + } + ); + }, + + shouldSetResponser: function(e) { + this.findViewForTouchEvent(e); + return true; + }, + + render: function() { + var content = []; + + if (this.state.frame) { + var distanceToTop = this.state.frame.top; + var distanceToBottom = Dimensions.get('window').height - + (this.state.frame.top + this.state.frame.height); + + var justifyContent = distanceToTop > distanceToBottom + ? 'flex-start' + : 'flex-end'; + + content.push(); + content.push(); + } + return ( + + {content} + + ); + } +}); + +var ElementProperties = React.createClass({ + render: function() { + var path = this.props.hierarchy.map((instance) => instance.getName()).join(' > '); + return ( + + + {path} + + + ); + } +}); + +var styles = StyleSheet.create({ + inspector: { + backgroundColor: 'rgba(255,255,255,0.8)', + position: 'absolute', + left: 0, + top: 0, + right: 0, + bottom: 0, + }, + frame: { + position: 'absolute', + backgroundColor: 'rgba(155,155,255,0.3)', + }, + info: { + backgroundColor: 'rgba(0, 0, 0, 0.7)', + padding: 10, + }, + path: { + color: 'white', + fontSize: 9, + } +}); + +module.exports = InspectorOverlay; diff --git a/Libraries/ReactIOS/renderApplication.ios.js b/Libraries/ReactIOS/renderApplication.ios.js index 16052c6fa69605..1cb24aa713df8a 100644 --- a/Libraries/ReactIOS/renderApplication.ios.js +++ b/Libraries/ReactIOS/renderApplication.ios.js @@ -7,17 +7,59 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule renderApplication - * @flow */ 'use strict'; +var InspectorOverlay = require('InspectorOverlay'); +var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); var React = require('React'); var StyleSheet = require('StyleSheet'); +var Subscribable = require('Subscribable'); var View = require('View'); var WarningBox = require('WarningBox'); var invariant = require('invariant'); +var AppContainer = React.createClass({ + mixins: [Subscribable.Mixin], + + getInitialState: function() { + return { inspector: null }; + }, + + toggleElementInspector: function() { + var inspector = this.state.inspector + ? null + : ; + this.setState({inspector}); + }, + + componentDidMount: function() { + this.addListenerOn( + RCTDeviceEventEmitter, + 'toggleElementInspector', + this.toggleElementInspector + ); + }, + + render: function() { + var shouldRenderWarningBox = __DEV__ && console.yellowBoxEnabled; + var warningBox = shouldRenderWarningBox ? : null; + return ( + + + {this.props.children} + + {warningBox} + {this.state.inspector} + + ); + } +}); + function renderApplication( RootComponent: ReactClass, initialProps: P, @@ -27,15 +69,12 @@ function renderApplication( rootTag, 'Expect to have a valid rootTag, instead got ', rootTag ); - var shouldRenderWarningBox = __DEV__ && console.yellowBoxEnabled; - var warningBox = shouldRenderWarningBox ? : null; React.render( - + - {warningBox} - , + , rootTag ); } diff --git a/React/Base/RCTDevMenu.m b/React/Base/RCTDevMenu.m index 421380e5b42ac7..2416e974c1757f 100644 --- a/React/Base/RCTDevMenu.m +++ b/React/Base/RCTDevMenu.m @@ -11,6 +11,7 @@ #import "RCTBridge.h" #import "RCTDefines.h" +#import "RCTEventDispatcher.h" #import "RCTKeyCommands.h" #import "RCTLog.h" #import "RCTPerfStats.h" @@ -241,6 +242,8 @@ - (void)toggle destructiveButtonTitle:nil otherButtonTitles:@"Reload", debugTitleChrome, debugTitleSafari, fpsMonitor, nil]; + [actionSheet addButtonWithTitle:@"Inspect Element"]; + if (_liveReloadURL) { NSString *liveReloadTitle = _liveReloadEnabled ? @"Disable Live Reload" : @"Enable Live Reload"; @@ -300,10 +303,14 @@ - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger break; } case 4: { - self.liveReloadEnabled = !_liveReloadEnabled; + [_bridge.eventDispatcher sendDeviceEventWithName:@"toggleElementInspector" body:nil]; break; } case 5: { + self.liveReloadEnabled = !_liveReloadEnabled; + break; + } + case 6: { self.profilingEnabled = !_profilingEnabled; break; } diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index 6b88f05d3cadec..e0698fef1fc295 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -882,6 +882,31 @@ static void RCTSetShadowViewProps(NSDictionary *props, RCTShadowView *shadowView }]; } +RCT_EXPORT_METHOD(findSubviewIn:(NSNumber *)reactTag atPoint:(CGPoint)point callback:(RCTResponseSenderBlock)callback) { + if (!reactTag) { + callback(@[[NSNull null]]); + return; + } + + [self addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) { + UIView *view = viewRegistry[reactTag]; + UIView *target = [view hitTest:point withEvent:nil]; + CGRect frame = [target convertRect:target.bounds toView:view]; + + while (target.reactTag == nil && target.superview != nil) { + target = [target superview]; + } + + callback(@[ + target.reactTag ?: [NSNull null], + @(frame.origin.x), + @(frame.origin.y), + @(frame.size.width), + @(frame.size.height), + ]); + }]; +} + - (void)batchDidComplete { // Gather blocks to be executed now that all view hierarchy manipulations have From 3269af227f89bf03232e986d36f1190f21d965ce Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Tue, 26 May 2015 13:29:41 -0700 Subject: [PATCH 20/44] Back out D2098670: Allow uploading a native file (e.g. photo) via XMLHttpRequest --- Libraries/Image/RCTImageLoader.h | 2 - Libraries/Image/RCTImageLoader.m | 7 --- Libraries/Network/RCTDataManager.m | 43 +++---------------- .../RCTNetwork.xcodeproj/project.pbxproj | 2 - Libraries/Network/XMLHttpRequest.ios.js | 11 ++--- 5 files changed, 10 insertions(+), 55 deletions(-) diff --git a/Libraries/Image/RCTImageLoader.h b/Libraries/Image/RCTImageLoader.h index 4d23a628e733e5..186a53cd1046b0 100644 --- a/Libraries/Image/RCTImageLoader.h +++ b/Libraries/Image/RCTImageLoader.h @@ -23,6 +23,4 @@ + (void)loadImageWithTag:(NSString *)tag callback:(void (^)(NSError *error, id /* UIImage or CAAnimation */ image))callback; -+ (BOOL)isSystemImageURI:(NSString *)uri; - @end diff --git a/Libraries/Image/RCTImageLoader.m b/Libraries/Image/RCTImageLoader.m index 25cb9598817b93..7525b37d0a3273 100644 --- a/Libraries/Image/RCTImageLoader.m +++ b/Libraries/Image/RCTImageLoader.m @@ -154,11 +154,4 @@ + (void)loadImageWithTag:(NSString *)imageTag callback:(void (^)(NSError *error, } } -+ (BOOL)isSystemImageURI:(NSString *)uri -{ - return uri != nil && ( - [uri hasPrefix:@"assets-library"] || - [uri hasPrefix:@"ph://"]); -} - @end diff --git a/Libraries/Network/RCTDataManager.m b/Libraries/Network/RCTDataManager.m index f6479548235cd2..1d0a793de8e454 100644 --- a/Libraries/Network/RCTDataManager.m +++ b/Libraries/Network/RCTDataManager.m @@ -13,7 +13,6 @@ #import "RCTConvert.h" #import "RCTLog.h" #import "RCTUtils.h" -#import "RCTImageLoader.h" @implementation RCTDataManager @@ -35,43 +34,8 @@ @implementation RCTDataManager NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = [RCTConvert NSString:query[@"method"]] ?: @"GET"; request.allHTTPHeaderFields = [RCTConvert NSDictionary:query[@"headers"]]; + request.HTTPBody = [RCTConvert NSData:query[@"data"]]; - if (query[@"data"] != [NSNull null]) { - NSDictionary *data = [RCTConvert NSDictionary:query[@"data"]]; - NSData *body = [RCTConvert NSData:data[@"string"]]; - if (body != nil) { - request.HTTPBody = body; - [RCTDataManager sendRequest:request responseSender:responseSender]; - return; - } - NSString *uri = [RCTConvert NSString:data[@"uri"]]; - if (uri != nil) { - if ([RCTImageLoader isSystemImageURI:uri]) { - [RCTImageLoader loadImageWithTag:(NSString *)uri callback:^(NSError *error, UIImage *image) { - if (error) { - RCTLogError(@"Error loading image URI: %@", error); - // We should really circle back to JS here and notify an error/abort on the request. - return; - } - NSData *imageData = UIImageJPEGRepresentation(image, 1.0); - request.HTTPBody = imageData; - [RCTDataManager sendRequest:request responseSender:responseSender]; - }]; - } else { - RCTLogError(@"Cannot resolve URI: %@", uri); - } - return; - } - } - - // There was no data payload, or we couldn't understand it. - [RCTDataManager sendRequest:request responseSender:responseSender]; - } else { - RCTLogError(@"unsupported query type %@", queryType); - } -} - -+ (void)sendRequest:(NSURLRequest *)request responseSender:(RCTResponseSenderBlock)responseSender { // Build data task NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *connectionError) { @@ -107,6 +71,11 @@ + (void)sendRequest:(NSURLRequest *)request responseSender:(RCTResponseSenderBlo }]; [task resume]; + + } else { + + RCTLogError(@"unsupported query type %@", queryType); + } } @end diff --git a/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj b/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj index 28bd23c8622a20..1dca7fe6de27bf 100644 --- a/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj +++ b/Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj @@ -209,7 +209,6 @@ "$(inherited)", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, "$(SRCROOT)/../../React/**", - "$(SRCROOT)/../Image/**", ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; @@ -227,7 +226,6 @@ "$(inherited)", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, "$(SRCROOT)/../../React/**", - "$(SRCROOT)/../Image/**", ); LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = "-ObjC"; diff --git a/Libraries/Network/XMLHttpRequest.ios.js b/Libraries/Network/XMLHttpRequest.ios.js index fca690e5955c6f..6c7367c18db725 100644 --- a/Libraries/Network/XMLHttpRequest.ios.js +++ b/Libraries/Network/XMLHttpRequest.ios.js @@ -20,16 +20,13 @@ var XMLHttpRequestBase = require('XMLHttpRequestBase'); class XMLHttpRequest extends XMLHttpRequestBase { sendImpl(method: ?string, url: ?string, headers: Object, data: any): void { - if (typeof data === 'string') { - data = {string: data}; - } RCTDataManager.queryData( 'http', { - method, - url, - data, - headers, + method: method, + url: url, + data: data, + headers: headers, }, // TODO: Do we need this? is it used anywhere? 'h' + crc32(method + '|' + url + '|' + data), From e5b134368b16bc875a0befc512c9d7678f079374 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Tue, 26 May 2015 12:40:26 -0700 Subject: [PATCH 21/44] Fixed crash due to nil backgroundColor --- React/Views/RCTShadowView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/React/Views/RCTShadowView.m b/React/Views/RCTShadowView.m index 7a0e9e421389db..6fc7f58b129a09 100644 --- a/React/Views/RCTShadowView.m +++ b/React/Views/RCTShadowView.m @@ -181,7 +181,7 @@ - (NSDictionary *)processBackgroundColor:(NSMutableSet *)applierBlocks parentPro // Update parent properties for children NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary:parentProperties]; CGFloat alpha = CGColorGetAlpha(_backgroundColor.CGColor); - if (alpha < 1.0 && alpha > 0.0) { + if (alpha < 1.0) { // If we see partial transparency, start propagating full transparency properties[RCTBackgroundColorProp] = [UIColor clearColor]; } else { From 769efdfcefd55cfed4fc16752e06bea1e7604a00 Mon Sep 17 00:00:00 2001 From: James Ide Date: Tue, 26 May 2015 14:16:47 -0700 Subject: [PATCH 22/44] [Packager] Fix the --root, --assetRoots, and --platform options Summary: Looks like these options were handled as booleans when they should be handled as strings. Explicitly specify them as strings. Closes https://github.com/facebook/react-native/pull/1377 Github Author: James Ide Test Plan: `packager/packager.sh --root A --root B` works. Also tested `packager/packager.sh --root A,B`. --- packager/packager.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packager/packager.js b/packager/packager.js index eb90b04e080638..d630d06e385d30 100644 --- a/packager/packager.js +++ b/packager/packager.js @@ -36,14 +36,18 @@ var webSocketProxy = require('./webSocketProxy.js'); var options = parseCommandLine([{ command: 'port', default: 8081, + type: 'string', }, { command: 'root', + type: 'string', description: 'add another root(s) to be used by the packager in this project', }, { command: 'assetRoots', + type: 'string', description: 'specify the root directories of app assets' }, { command: 'platform', + type: 'string', default: 'ios', description: 'Specify the platform-specific blacklist (ios, android, web).' }, { @@ -65,13 +69,13 @@ if (options.projectRoots) { } if (options.root) { - if (typeof options.root === 'string') { - options.projectRoots.push(path.resolve(options.root)); - } else { - options.root.forEach(function(root) { - options.projectRoots.push(path.resolve(root)); - }); + if (!Array.isArray(options.root)) { + options.root = options.root.split(','); } + + options.root.forEach(function(root) { + options.projectRoots.push(path.resolve(root)); + }); } if (options.assetRoots) { From 2a76ca635e6524498bf10d72cf6bfe028e0a4b2c Mon Sep 17 00:00:00 2001 From: henter Date: Tue, 26 May 2015 14:54:32 -0700 Subject: [PATCH 23/44] [GeoLocation] invalidate timer after success or error callback Summary: Just trying to [getCurrentPosition](https://github.com/facebook/react-native/blob/master/Libraries/Geolocation/Geolocation.js#L45) , and found the `errorBlock` of location request in timeout handler would cause red error like this: ``` 2015-05-10 17:50:39.607 [warn][tid:com.facebook.React.JavaScript] "Warning: Cannot find callback with CBID 5. Native module may have invoked both the success callback and the error callback." 2015-05-10 17:50:39.610 [error][tid:com.facebook.React.JavaScript] "Error: null is not an object (evaluating 'cb.apply') stack: _invokeCallback index.ios.bundle:7593 index.ios.bundle:7656 index.ios.bundle:7648 perform index.ios.bundle:6157 batchedUpdates index.ios.bundle:13786 batchedUpdates index.ios.bundle:4689 index.ios.bundle:7647 applyWithGuard index.ios.bundle:882 guardReturn index.ios.bundle:7421 processBatch index.ios.bundle:7646 URL: http://192.168.100.182:8081/index Closes https://github.com/facebook/react-native/pull/1226 Github Author: henter Test Plan: Imported from GitHub, without a `Test Plan:` line. --- Libraries/Geolocation/RCTLocationObserver.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Libraries/Geolocation/RCTLocationObserver.m b/Libraries/Geolocation/RCTLocationObserver.m index f21233dbfa0576..0fe5fed756808b 100644 --- a/Libraries/Geolocation/RCTLocationObserver.m +++ b/Libraries/Geolocation/RCTLocationObserver.m @@ -85,7 +85,9 @@ @implementation RCTLocationRequest - (void)dealloc { - [_timeoutTimer invalidate]; + if (_timeoutTimer.valid) { + [_timeoutTimer invalidate]; + } } @end @@ -273,6 +275,7 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray // Fire all queued callbacks for (RCTLocationRequest *request in _pendingRequests) { request.successBlock(@[_lastLocationEvent]); + [request.timeoutTimer invalidate]; } [_pendingRequests removeAllObjects]; @@ -311,6 +314,7 @@ - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError * // Fire all queued error callbacks for (RCTLocationRequest *request in _pendingRequests) { request.errorBlock(@[jsError]); + [request.timeoutTimer invalidate]; } [_pendingRequests removeAllObjects]; From b839ab32beaa119f5c685fed371ffaac8599cb73 Mon Sep 17 00:00:00 2001 From: Jared Forsyth Date: Tue, 26 May 2015 15:06:20 -0700 Subject: [PATCH 24/44] add missing require("Map") --- Libraries/AppStateIOS/AppStateIOS.ios.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Libraries/AppStateIOS/AppStateIOS.ios.js b/Libraries/AppStateIOS/AppStateIOS.ios.js index b11c41a53694af..e123d23be0f1fa 100644 --- a/Libraries/AppStateIOS/AppStateIOS.ios.js +++ b/Libraries/AppStateIOS/AppStateIOS.ios.js @@ -11,6 +11,7 @@ */ 'use strict'; +var Map = require('Map'); var NativeModules = require('NativeModules'); var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); var RCTAppState = NativeModules.AppState; From 84783dbac4b311e8fe99ba8cde1f42bd804dfbfb Mon Sep 17 00:00:00 2001 From: Changgeng Li Date: Tue, 26 May 2015 15:16:42 -0700 Subject: [PATCH 25/44] Add renderSeparator support to ListView --- Examples/Movies/MovieCell.js | 6 ++- .../Movies/Movies.xcodeproj/project.pbxproj | 30 ++++++++++++++ Examples/Movies/SearchScreen.js | 32 ++++++++++++++- .../Touchable/TouchableHighlight.js | 10 +++++ .../CustomComponents/ListView/ListView.js | 41 +++++++++++++++++-- 5 files changed, 112 insertions(+), 7 deletions(-) diff --git a/Examples/Movies/MovieCell.js b/Examples/Movies/MovieCell.js index 7e2b3df17cee42..e6a9b10782966a 100644 --- a/Examples/Movies/MovieCell.js +++ b/Examples/Movies/MovieCell.js @@ -34,7 +34,10 @@ var MovieCell = React.createClass({ var criticsScore = this.props.movie.ratings.critics_score; return ( - + - ); } diff --git a/Examples/Movies/Movies.xcodeproj/project.pbxproj b/Examples/Movies/Movies.xcodeproj/project.pbxproj index e86674f6821456..21fc0cb888fff8 100644 --- a/Examples/Movies/Movies.xcodeproj/project.pbxproj +++ b/Examples/Movies/Movies.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 140D9B661AC36C42004F25EE /* libRCTLinking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14312D241AC3654D00CDC950 /* libRCTLinking.a */; }; 14A2D4421AC3E43800CC738A /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14A2D4411AC3E41A00CC738A /* libReact.a */; }; 58C5726B1AA6239E00CDF9C8 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 58C5725B1AA6236500CDF9C8 /* libRCTText.a */; }; + 67C95F201B0E64A30040BCE2 /* libRCTWebSocket.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 67C95F1E1B0E647A0040BCE2 /* libRCTWebSocket.a */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -54,6 +55,13 @@ remoteGlobalIDString = 58B5119B1A9E6C1200147676; remoteInfo = RCTText; }; + 67C95F1D1B0E647A0040BCE2 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 67C95F151B0E647A0040BCE2 /* RCTWebSocket.xcodeproj */; + proxyType = 2; + remoteGlobalIDString = 3C86DF461ADF2C930047B81A; + remoteInfo = RCTWebSocket; + }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ @@ -69,6 +77,7 @@ 14312D1E1AC3654D00CDC950 /* RCTLinking.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTLinking.xcodeproj; path = ../../Libraries/LinkingIOS/RCTLinking.xcodeproj; sourceTree = ""; }; 14A2D43C1AC3E41A00CC738A /* React.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = React.xcodeproj; path = ../../React/React.xcodeproj; sourceTree = ""; }; 587650F61A9EB120008B8F17 /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = ../../Libraries/Text/RCTText.xcodeproj; sourceTree = SOURCE_ROOT; }; + 67C95F151B0E647A0040BCE2 /* RCTWebSocket.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTWebSocket.xcodeproj; path = ../../Libraries/WebSocket/RCTWebSocket.xcodeproj; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -76,6 +85,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 67C95F201B0E64A30040BCE2 /* libRCTWebSocket.a in Frameworks */, 14A2D4421AC3E43800CC738A /* libReact.a in Frameworks */, 140D9B661AC36C42004F25EE /* libRCTLinking.a in Frameworks */, 1341801E1AA91750003F314A /* libRCTNetwork.a in Frameworks */, @@ -135,6 +145,7 @@ 58C571FC1AA6124500CDF9C8 /* Libraries */ = { isa = PBXGroup; children = ( + 67C95F151B0E647A0040BCE2 /* RCTWebSocket.xcodeproj */, 14A2D43C1AC3E41A00CC738A /* React.xcodeproj */, 14312D1E1AC3654D00CDC950 /* RCTLinking.xcodeproj */, 134180151AA91740003F314A /* RCTNetwork.xcodeproj */, @@ -152,6 +163,14 @@ name = Products; sourceTree = ""; }; + 67C95F161B0E647A0040BCE2 /* Products */ = { + isa = PBXGroup; + children = ( + 67C95F1E1B0E647A0040BCE2 /* libRCTWebSocket.a */, + ); + name = Products; + sourceTree = ""; + }; 83CBB9F61A601CBA00E9B192 = { isa = PBXGroup; children = ( @@ -228,6 +247,10 @@ ProductGroup = 58C572571AA6236500CDF9C8 /* Products */; ProjectRef = 587650F61A9EB120008B8F17 /* RCTText.xcodeproj */; }, + { + ProductGroup = 67C95F161B0E647A0040BCE2 /* Products */; + ProjectRef = 67C95F151B0E647A0040BCE2 /* RCTWebSocket.xcodeproj */; + }, { ProductGroup = 14A2D43D1AC3E41A00CC738A /* Products */; ProjectRef = 14A2D43C1AC3E41A00CC738A /* React.xcodeproj */; @@ -276,6 +299,13 @@ remoteRef = 58C5725A1AA6236500CDF9C8 /* PBXContainerItemProxy */; sourceTree = BUILT_PRODUCTS_DIR; }; + 67C95F1E1B0E647A0040BCE2 /* libRCTWebSocket.a */ = { + isa = PBXReferenceProxy; + fileType = archive.ar; + path = libRCTWebSocket.a; + remoteRef = 67C95F1D1B0E647A0040BCE2 /* PBXContainerItemProxy */; + sourceTree = BUILT_PRODUCTS_DIR; + }; /* End PBXReferenceProxy section */ /* Begin PBXResourcesBuildPhase section */ diff --git a/Examples/Movies/SearchScreen.js b/Examples/Movies/SearchScreen.js index e484d0f6591ce0..05ab8018732c4c 100644 --- a/Examples/Movies/SearchScreen.js +++ b/Examples/Movies/SearchScreen.js @@ -237,10 +237,31 @@ var SearchScreen = React.createClass({ return ; }, - renderRow: function(movie: Object) { + renderSeparator: function( + sectionID: number | string, + rowID: number | string, + adjacentRowHighlighted: boolean + ) { + var style = styles.rowSeparator; + if (adjacentRowHighlighted) { + style = [style, styles.rowSeparatorHide]; + } + return ( + + ); + }, + + renderRow: function( + movie: Object, + sectionID: number | string, + rowID: number | string, + highlightRowFunc: (sectionID: ?number | string, rowID: ?number | string) => void, + ) { return ( this.selectMovie(movie)} + onHighlight={() => highlightRowFunc(sectionID, rowID)} + onUnhighlight={() => highlightRowFunc(null, null)} movie={movie} /> ); @@ -254,6 +275,7 @@ var SearchScreen = React.createClass({ /> :