Skip to content

Commit

Permalink
Preps 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlplusb committed Apr 6, 2017
1 parent 98733da commit 1f12e3c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
36 changes: 28 additions & 8 deletions commonjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ exports.default = reactTreeWalker;

var _react = require('react');

var defaultOptions = {
componentWillUnmount: false
};

// Lifted from https://github.com/sindresorhus/p-reduce
// Thanks @sindresorhus!
/* eslint-disable no-console */

// Inspired by the awesome work done by the Apollo team.
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
// This version has been adapted to be promise based.

// eslint-disable-next-line import/no-extraneous-dependencies
var pReduce = function pReduce(iterable, reducer, initVal) {
return new Promise(function (resolve, reject) {
var iterator = iterable[Symbol.iterator]();
Expand All @@ -35,13 +46,6 @@ var pReduce = function pReduce(iterable, reducer, initVal) {

// Lifted from https://github.com/sindresorhus/p-map-series
// Thanks @sindresorhus!
/* eslint-disable no-console */

// Inspired by the awesome work done by the Apollo team.
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
// This version has been adapted to be promise based.

// eslint-disable-next-line import/no-extraneous-dependencies
var pMapSeries = function pMapSeries(iterable, iterator) {
var ret = [];

Expand All @@ -62,6 +66,8 @@ var isPromise = exports.isPromise = function isPromise(x) {
// If visitor returns `false`, don't call the element's render function
// or recurse into its child elements
function reactTreeWalker(element, visitor, context) {
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultOptions;

return new Promise(function (resolve) {
var doVisit = function doVisit(getChildren, visitorResult, childContext, isChildren) {
var doTraverse = function doTraverse(shouldContinue) {
Expand Down Expand Up @@ -140,7 +146,21 @@ function reactTreeWalker(element, visitor, context) {
instance.componentWillMount();
}

return instance.render();
var children = instance.render();

if (options.componentWillUnmount && instance.componentWillUnmount) {
try {
instance.componentWillUnmount();
} catch (err) {
// This is an experimental feature, we don't want to break
// the bootstrapping process, but lets warn the user it
// occurred.
console.warn('Error calling componentWillUnmount whilst walking your react tree');
console.warn(err);
}
}

return children;
}, visitor(element, instance, context), function () {
return (
// Ensure the child context is initialised if it is available. We will
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-tree-walker",
"version": "2.0.1",
"version": "2.1.0",
"description": "Walk a React element tree, executing a provided function against each node.",
"license": "MIT",
"main": "commonjs/index.js",
Expand Down
36 changes: 28 additions & 8 deletions umd/react-tree-walker.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,19 @@ exports.default = reactTreeWalker;

var _react = __webpack_require__(0);

var defaultOptions = {
componentWillUnmount: false
};

// Lifted from https://github.com/sindresorhus/p-reduce
// Thanks @sindresorhus!
/* eslint-disable no-console */

// Inspired by the awesome work done by the Apollo team.
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
// This version has been adapted to be promise based.

// eslint-disable-next-line import/no-extraneous-dependencies
var pReduce = function pReduce(iterable, reducer, initVal) {
return new Promise(function (resolve, reject) {
var iterator = iterable[Symbol.iterator]();
Expand All @@ -124,13 +135,6 @@ var pReduce = function pReduce(iterable, reducer, initVal) {

// Lifted from https://github.com/sindresorhus/p-map-series
// Thanks @sindresorhus!
/* eslint-disable no-console */

// Inspired by the awesome work done by the Apollo team.
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
// This version has been adapted to be promise based.

// eslint-disable-next-line import/no-extraneous-dependencies
var pMapSeries = function pMapSeries(iterable, iterator) {
var ret = [];

Expand All @@ -151,6 +155,8 @@ var isPromise = exports.isPromise = function isPromise(x) {
// If visitor returns `false`, don't call the element's render function
// or recurse into its child elements
function reactTreeWalker(element, visitor, context) {
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultOptions;

return new Promise(function (resolve) {
var doVisit = function doVisit(getChildren, visitorResult, childContext, isChildren) {
var doTraverse = function doTraverse(shouldContinue) {
Expand Down Expand Up @@ -229,7 +235,21 @@ function reactTreeWalker(element, visitor, context) {
instance.componentWillMount();
}

return instance.render();
var children = instance.render();

if (options.componentWillUnmount && instance.componentWillUnmount) {
try {
instance.componentWillUnmount();
} catch (err) {
// This is an experimental feature, we don't want to break
// the bootstrapping process, but lets warn the user it
// occurred.
console.warn('Error calling componentWillUnmount whilst walking your react tree');
console.warn(err);
}
}

return children;
}, visitor(element, instance, context), function () {
return (
// Ensure the child context is initialised if it is available. We will
Expand Down
2 changes: 1 addition & 1 deletion umd/react-tree-walker.min.js

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

0 comments on commit 1f12e3c

Please sign in to comment.