react-tree-walker
Advanced tools
Comparing version 2.0.0-alpha.1 to 2.0.0-alpha.2
@@ -17,2 +17,4 @@ 'use strict'; | ||
/* eslint-disable no-console */ | ||
// Inspired by the awesome work done by the Apollo team. | ||
@@ -29,16 +31,25 @@ // See https://github.com/apollostack/react-apollo/blob/master/src/server.ts | ||
// If visitor returns `false`, don't call the element's render function | ||
// or recurse into its child elements | ||
// or recurse into its child elements | ||
function reactTreeWalker(element, visitor, context) { | ||
return new Promise(function (resolve) { | ||
var handleVisitResult = function handleVisitResult(getChild, visitorResult, childContext, isChildren) { | ||
var tryContinue = function tryContinue() { | ||
// Returned true, indicating a desire to continue traversal immediately. | ||
var child = getChild(); | ||
var doVisit = function doVisit(getChildren, visitorResult, childContext, isChildren) { | ||
var doTraverse = function doTraverse(shouldContinue) { | ||
if (!shouldContinue) { | ||
// We recieved a false, which indicates a desire to stop traversal. | ||
resolve(); | ||
} | ||
var child = getChildren(); | ||
var theChildContext = typeof childContext === 'function' ? childContext() : childContext; | ||
if (child == null) { | ||
// If no children then we can't traverse. We've reached the leaf. | ||
resolve(); | ||
} else if (isChildren) { | ||
// If its a react Children collection we need to breadth-first | ||
// traverse each of them. | ||
var mapper = function mapper(aChild) { | ||
return aChild ? reactTreeWalker(aChild, visitor, childContext) : undefined; | ||
return aChild ? reactTreeWalker(aChild, visitor, theChildContext) : undefined; | ||
}; | ||
// pMapSeries allows us to do depth-first traversal. Thanks @sindresorhus! | ||
(0, _pMapSeries2.default)(_react.Children.map(child, function (cur) { | ||
@@ -48,3 +59,4 @@ return cur; | ||
} else { | ||
reactTreeWalker(child, visitor, childContext).then(resolve); | ||
// Otherwise we pass the individual child to the next recursion. | ||
reactTreeWalker(child, visitor, theChildContext).then(resolve); | ||
} | ||
@@ -57,5 +69,15 @@ }; | ||
} else if (isPromise(visitorResult)) { | ||
visitorResult.then(tryContinue); | ||
// We need to execute the result and pass it's result through to our | ||
// continuer. | ||
visitorResult.then(doTraverse).catch(function (e) { | ||
console.log('Error occurred in Promise based visitor result provided to react-tree-walker.'); | ||
if (e) { | ||
console.log(e); | ||
if (e.stack) { | ||
console.log(e.stack); | ||
} | ||
} | ||
}); | ||
} else { | ||
tryContinue(); | ||
doTraverse(true); | ||
} | ||
@@ -69,3 +91,2 @@ }; | ||
var props = Object.assign({}, Component.defaultProps, element.props); | ||
var childContext = context; | ||
@@ -77,13 +98,16 @@ // Is this a class component? (http://bit.ly/2j9Ifk3) | ||
(function () { | ||
var instanceFactory = function instanceFactory() { | ||
var instance = new Component(props, context); | ||
// In case the user doesn't pass these to super in the constructor | ||
instance.props = instance.props || props; | ||
instance.context = instance.context || context; | ||
// React class component | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
var instance = new Component(props, context); | ||
// In case the user doesn't pass these to super in the constructor | ||
instance.props = instance.props || props; | ||
instance.context = instance.context || context; | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
doVisit(function () { | ||
// Call componentWillMount if it exists. | ||
@@ -94,23 +118,14 @@ if (instance.componentWillMount) { | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
if (instance.getChildContext) { | ||
childContext = Object.assign({}, context, instance.getChildContext()); | ||
} | ||
return instance; | ||
}; | ||
var instance = instanceFactory(); | ||
// Hit up our visitor! | ||
handleVisitResult(function () { | ||
return instance.render(); | ||
}, visitor(element, instance, context), childContext); | ||
}, visitor(element, instance, context), function () { | ||
return ( | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
instance.getChildContext ? Object.assign({}, context, instance.getChildContext()) : context | ||
); | ||
}); | ||
})(); | ||
} else { | ||
// Stateless Functional Component | ||
// Hit up our visitor! | ||
handleVisitResult(function () { | ||
doVisit(function () { | ||
return Component(props, context); | ||
@@ -122,5 +137,3 @@ }, visitor(element, null, context), context); | ||
// This must be a basic element, such as a string or dom node. | ||
// Hit up our visitor! | ||
handleVisitResult(function () { | ||
doVisit(function () { | ||
return element.props && element.props.children ? element.props.children : undefined; | ||
@@ -127,0 +140,0 @@ }, visitor(element, null, context), context, true); |
{ | ||
"name": "react-tree-walker", | ||
"version": "2.0.0-alpha.1", | ||
"version": "2.0.0-alpha.2", | ||
"description": "Walk a React element tree, executing a provided function against each node.", | ||
@@ -27,2 +27,3 @@ "license": "MIT", | ||
"scripts": { | ||
"precommit": "lint-staged && npm run test", | ||
"build": "babel-node ./tools/scripts/build.js", | ||
@@ -37,2 +38,19 @@ "check": "yarn run lint && yarn run test", | ||
}, | ||
"jest": { | ||
"collectCoverageFrom": [ | ||
"src/**/*.{js,jsx}" | ||
], | ||
"snapshotSerializers": [ | ||
"<rootDir>/node_modules/enzyme-to-json/serializer" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"<rootDir>/(commonjs|coverage|flow-typed|node_modules|tools|umd)/" | ||
] | ||
}, | ||
"lint-staged": { | ||
"*.js": [ | ||
"prettier-eslint --write", | ||
"git add" | ||
] | ||
}, | ||
"peerDependencies": { | ||
@@ -63,6 +81,10 @@ "react": "^0.14.0 || ^15.0.0-0" | ||
"eslint-plugin-react": "6.10.0", | ||
"ghooks": "2.0.0", | ||
"gzip-size": "3.0.0", | ||
"husky": "0.13.2", | ||
"in-publish": "2.0.0", | ||
"jest": "19.0.2", | ||
"lint-staged": "3.4.0", | ||
"prettier": "0.22.0", | ||
"prettier-eslint": "4.2.1", | ||
"prettier-eslint-cli": "3.1.2", | ||
"pretty-bytes": "4.0.2", | ||
@@ -80,18 +102,2 @@ "ramda": "0.23.0", | ||
}, | ||
"config": { | ||
"ghooks": { | ||
"pre-commit": "yarn run check" | ||
} | ||
}, | ||
"jest": { | ||
"collectCoverageFrom": [ | ||
"src/**/*.{js,jsx}" | ||
], | ||
"snapshotSerializers": [ | ||
"<rootDir>/node_modules/enzyme-to-json/serializer" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"<rootDir>/(commonjs|coverage|flow-typed|node_modules|tools|umd)/" | ||
] | ||
}, | ||
"dependencies": { | ||
@@ -98,0 +104,0 @@ "p-map-series": "^1.0.0" |
@@ -156,2 +156,4 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
/* eslint-disable no-console */ | ||
// Inspired by the awesome work done by the Apollo team. | ||
@@ -168,16 +170,25 @@ // See https://github.com/apollostack/react-apollo/blob/master/src/server.ts | ||
// If visitor returns `false`, don't call the element's render function | ||
// or recurse into its child elements | ||
// or recurse into its child elements | ||
function reactTreeWalker(element, visitor, context) { | ||
return new Promise(function (resolve) { | ||
var handleVisitResult = function handleVisitResult(getChild, visitorResult, childContext, isChildren) { | ||
var tryContinue = function tryContinue() { | ||
// Returned true, indicating a desire to continue traversal immediately. | ||
var child = getChild(); | ||
var doVisit = function doVisit(getChildren, visitorResult, childContext, isChildren) { | ||
var doTraverse = function doTraverse(shouldContinue) { | ||
if (!shouldContinue) { | ||
// We recieved a false, which indicates a desire to stop traversal. | ||
resolve(); | ||
} | ||
var child = getChildren(); | ||
var theChildContext = typeof childContext === 'function' ? childContext() : childContext; | ||
if (child == null) { | ||
// If no children then we can't traverse. We've reached the leaf. | ||
resolve(); | ||
} else if (isChildren) { | ||
// If its a react Children collection we need to breadth-first | ||
// traverse each of them. | ||
var mapper = function mapper(aChild) { | ||
return aChild ? reactTreeWalker(aChild, visitor, childContext) : undefined; | ||
return aChild ? reactTreeWalker(aChild, visitor, theChildContext) : undefined; | ||
}; | ||
// pMapSeries allows us to do depth-first traversal. Thanks @sindresorhus! | ||
(0, _pMapSeries2.default)(_react.Children.map(child, function (cur) { | ||
@@ -187,3 +198,4 @@ return cur; | ||
} else { | ||
reactTreeWalker(child, visitor, childContext).then(resolve); | ||
// Otherwise we pass the individual child to the next recursion. | ||
reactTreeWalker(child, visitor, theChildContext).then(resolve); | ||
} | ||
@@ -196,5 +208,15 @@ }; | ||
} else if (isPromise(visitorResult)) { | ||
visitorResult.then(tryContinue); | ||
// We need to execute the result and pass it's result through to our | ||
// continuer. | ||
visitorResult.then(doTraverse).catch(function (e) { | ||
console.log('Error occurred in Promise based visitor result provided to react-tree-walker.'); | ||
if (e) { | ||
console.log(e); | ||
if (e.stack) { | ||
console.log(e.stack); | ||
} | ||
} | ||
}); | ||
} else { | ||
tryContinue(); | ||
doTraverse(true); | ||
} | ||
@@ -208,3 +230,2 @@ }; | ||
var props = Object.assign({}, Component.defaultProps, element.props); | ||
var childContext = context; | ||
@@ -216,13 +237,16 @@ // Is this a class component? (http://bit.ly/2j9Ifk3) | ||
(function () { | ||
var instanceFactory = function instanceFactory() { | ||
var instance = new Component(props, context); | ||
// In case the user doesn't pass these to super in the constructor | ||
instance.props = instance.props || props; | ||
instance.context = instance.context || context; | ||
// React class component | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
var instance = new Component(props, context); | ||
// In case the user doesn't pass these to super in the constructor | ||
instance.props = instance.props || props; | ||
instance.context = instance.context || context; | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
doVisit(function () { | ||
// Call componentWillMount if it exists. | ||
@@ -233,23 +257,14 @@ if (instance.componentWillMount) { | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
if (instance.getChildContext) { | ||
childContext = Object.assign({}, context, instance.getChildContext()); | ||
} | ||
return instance; | ||
}; | ||
var instance = instanceFactory(); | ||
// Hit up our visitor! | ||
handleVisitResult(function () { | ||
return instance.render(); | ||
}, visitor(element, instance, context), childContext); | ||
}, visitor(element, instance, context), function () { | ||
return ( | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
instance.getChildContext ? Object.assign({}, context, instance.getChildContext()) : context | ||
); | ||
}); | ||
})(); | ||
} else { | ||
// Stateless Functional Component | ||
// Hit up our visitor! | ||
handleVisitResult(function () { | ||
doVisit(function () { | ||
return Component(props, context); | ||
@@ -261,5 +276,3 @@ }, visitor(element, null, context), context); | ||
// This must be a basic element, such as a string or dom node. | ||
// Hit up our visitor! | ||
handleVisitResult(function () { | ||
doVisit(function () { | ||
return element.props && element.props.children ? element.props.children : undefined; | ||
@@ -266,0 +279,0 @@ }, visitor(element, null, context), context, true); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.ReactTreeWalker=e(require("react")):t.ReactTreeWalker=e(t.React)}(this,function(t){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=3)}([function(t,e,n){"use strict";var r=n(2);t.exports=function(t,e){var n=[];return r(t,function(t,r,o){return Promise.resolve(e(r,o)).then(function(t){n.push(t)})}).then(function(){return n})}},function(e,n){e.exports=t},function(t,e,n){"use strict";t.exports=function(t,e,n){return new Promise(function(r,o){var u=t[Symbol.iterator](),i=0,c=function t(n){var c=u.next();return c.done?void r(n):void Promise.all([n,c.value]).then(function(n){t(e(n[0],n[1],i++))}).catch(o)};c(n)})}},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function o(t,e,n){return new Promise(function(r){var i=function(t,n,i,s){var p=function(){var n=t();if(null==n)r();else if(s){var f=function(t){return t?o(t,e,i):void 0};(0,c.default)(u.Children.map(n,function(t){return t}),f).then(r)}else o(n,e,i).then(r)};n===!1?r():f(n)?n.then(p):p()};"function"==typeof t.type?!function(){var r=t.type,o=Object.assign({},r.defaultProps,t.props),u=n,c=r.prototype&&(r.prototype.isReactComponent||r.prototype.isPureReactComponent);c?!function(){var c=function(){var t=new r(o,n);return t.props=t.props||o,t.context=t.context||n,t.setState=function(e){t.state=Object.assign({},t.state,e)},t.componentWillMount&&t.componentWillMount(),t.getChildContext&&(u=Object.assign({},n,t.getChildContext())),t},f=c();i(function(){return f.render()},e(t,f,n),u)}():i(function(){return r(o,n)},e(t,null,n),n)}():i(function(){return t.props&&t.props.children?t.props.children:void 0},e(t,null,n),n,!0)})}Object.defineProperty(e,"__esModule",{value:!0}),e.isPromise=void 0,e.default=o;var u=n(1),i=n(0),c=r(i),f=e.isPromise=function(t){return null!=t&&"function"==typeof t.then}}])}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.ReactTreeWalker=e(require("react")):t.ReactTreeWalker=e(t.React)}(this,function(t){return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=3)}([function(t,e,n){"use strict";var o=n(2);t.exports=function(t,e){var n=[];return o(t,function(t,o,r){return Promise.resolve(e(o,r)).then(function(t){n.push(t)})}).then(function(){return n})}},function(e,n){e.exports=t},function(t,e,n){"use strict";t.exports=function(t,e,n){return new Promise(function(o,r){var u=t[Symbol.iterator](),i=0,c=function t(n){var c=u.next();return c.done?void o(n):void Promise.all([n,c.value]).then(function(n){t(e(n[0],n[1],i++))}).catch(r)};c(n)})}},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e,n){return new Promise(function(o){var i=function(t,n,i,s){var a=function(n){n||o();var f=t(),a="function"==typeof i?i():i;if(null==f)o();else if(s){var l=function(t){return t?r(t,e,a):void 0};(0,c.default)(u.Children.map(f,function(t){return t}),l).then(o)}else r(f,e,a).then(o)};n===!1?o():f(n)?n.then(a).catch(function(t){console.log("Error occurred in Promise based visitor result provided to react-tree-walker."),t&&(console.log(t),t.stack&&console.log(t.stack))}):a(!0)};"function"==typeof t.type?!function(){var o=t.type,r=Object.assign({},o.defaultProps,t.props),u=o.prototype&&(o.prototype.isReactComponent||o.prototype.isPureReactComponent);u?!function(){var u=new o(r,n);u.props=u.props||r,u.context=u.context||n,u.setState=function(t){u.state=Object.assign({},u.state,t)},i(function(){return u.componentWillMount&&u.componentWillMount(),u.render()},e(t,u,n),function(){return u.getChildContext?Object.assign({},n,u.getChildContext()):n})}():i(function(){return o(r,n)},e(t,null,n),n)}():i(function(){return t.props&&t.props.children?t.props.children:void 0},e(t,null,n),n,!0)})}Object.defineProperty(e,"__esModule",{value:!0}),e.isPromise=void 0,e.default=r;var u=n(1),i=n(0),c=o(i),f=e.isPromise=function(t){return null!=t&&"function"==typeof t.then}}])}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23563
337
0
40