Skip to content

Commit 1f12e3c

Browse files
committed
Preps 2.1.0
1 parent 98733da commit 1f12e3c

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

commonjs/index.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@ exports.default = reactTreeWalker;
88

99
var _react = require('react');
1010

11+
var defaultOptions = {
12+
componentWillUnmount: false
13+
};
14+
1115
// Lifted from https://github.com/sindresorhus/p-reduce
1216
// Thanks @sindresorhus!
17+
/* eslint-disable no-console */
18+
19+
// Inspired by the awesome work done by the Apollo team.
20+
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
21+
// This version has been adapted to be promise based.
22+
23+
// eslint-disable-next-line import/no-extraneous-dependencies
1324
var pReduce = function pReduce(iterable, reducer, initVal) {
1425
return new Promise(function (resolve, reject) {
1526
var iterator = iterable[Symbol.iterator]();
@@ -35,13 +46,6 @@ var pReduce = function pReduce(iterable, reducer, initVal) {
3546

3647
// Lifted from https://github.com/sindresorhus/p-map-series
3748
// Thanks @sindresorhus!
38-
/* eslint-disable no-console */
39-
40-
// Inspired by the awesome work done by the Apollo team.
41-
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
42-
// This version has been adapted to be promise based.
43-
44-
// eslint-disable-next-line import/no-extraneous-dependencies
4549
var pMapSeries = function pMapSeries(iterable, iterator) {
4650
var ret = [];
4751

@@ -62,6 +66,8 @@ var isPromise = exports.isPromise = function isPromise(x) {
6266
// If visitor returns `false`, don't call the element's render function
6367
// or recurse into its child elements
6468
function reactTreeWalker(element, visitor, context) {
69+
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultOptions;
70+
6571
return new Promise(function (resolve) {
6672
var doVisit = function doVisit(getChildren, visitorResult, childContext, isChildren) {
6773
var doTraverse = function doTraverse(shouldContinue) {
@@ -140,7 +146,21 @@ function reactTreeWalker(element, visitor, context) {
140146
instance.componentWillMount();
141147
}
142148

143-
return instance.render();
149+
var children = instance.render();
150+
151+
if (options.componentWillUnmount && instance.componentWillUnmount) {
152+
try {
153+
instance.componentWillUnmount();
154+
} catch (err) {
155+
// This is an experimental feature, we don't want to break
156+
// the bootstrapping process, but lets warn the user it
157+
// occurred.
158+
console.warn('Error calling componentWillUnmount whilst walking your react tree');
159+
console.warn(err);
160+
}
161+
}
162+
163+
return children;
144164
}, visitor(element, instance, context), function () {
145165
return (
146166
// Ensure the child context is initialised if it is available. We will

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-tree-walker",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "Walk a React element tree, executing a provided function against each node.",
55
"license": "MIT",
66
"main": "commonjs/index.js",

umd/react-tree-walker.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,19 @@ exports.default = reactTreeWalker;
9797

9898
var _react = __webpack_require__(0);
9999

100+
var defaultOptions = {
101+
componentWillUnmount: false
102+
};
103+
100104
// Lifted from https://github.com/sindresorhus/p-reduce
101105
// Thanks @sindresorhus!
106+
/* eslint-disable no-console */
107+
108+
// Inspired by the awesome work done by the Apollo team.
109+
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
110+
// This version has been adapted to be promise based.
111+
112+
// eslint-disable-next-line import/no-extraneous-dependencies
102113
var pReduce = function pReduce(iterable, reducer, initVal) {
103114
return new Promise(function (resolve, reject) {
104115
var iterator = iterable[Symbol.iterator]();
@@ -124,13 +135,6 @@ var pReduce = function pReduce(iterable, reducer, initVal) {
124135

125136
// Lifted from https://github.com/sindresorhus/p-map-series
126137
// Thanks @sindresorhus!
127-
/* eslint-disable no-console */
128-
129-
// Inspired by the awesome work done by the Apollo team.
130-
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
131-
// This version has been adapted to be promise based.
132-
133-
// eslint-disable-next-line import/no-extraneous-dependencies
134138
var pMapSeries = function pMapSeries(iterable, iterator) {
135139
var ret = [];
136140

@@ -151,6 +155,8 @@ var isPromise = exports.isPromise = function isPromise(x) {
151155
// If visitor returns `false`, don't call the element's render function
152156
// or recurse into its child elements
153157
function reactTreeWalker(element, visitor, context) {
158+
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultOptions;
159+
154160
return new Promise(function (resolve) {
155161
var doVisit = function doVisit(getChildren, visitorResult, childContext, isChildren) {
156162
var doTraverse = function doTraverse(shouldContinue) {
@@ -229,7 +235,21 @@ function reactTreeWalker(element, visitor, context) {
229235
instance.componentWillMount();
230236
}
231237

232-
return instance.render();
238+
var children = instance.render();
239+
240+
if (options.componentWillUnmount && instance.componentWillUnmount) {
241+
try {
242+
instance.componentWillUnmount();
243+
} catch (err) {
244+
// This is an experimental feature, we don't want to break
245+
// the bootstrapping process, but lets warn the user it
246+
// occurred.
247+
console.warn('Error calling componentWillUnmount whilst walking your react tree');
248+
console.warn(err);
249+
}
250+
}
251+
252+
return children;
233253
}, visitor(element, instance, context), function () {
234254
return (
235255
// Ensure the child context is initialised if it is available. We will

umd/react-tree-walker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)