From 46327e0ca57f03531344ba68b18c5ce827492fcf Mon Sep 17 00:00:00 2001 From: Sam Parsons Date: Tue, 20 Mar 2018 09:36:34 -0700 Subject: [PATCH] add options to callbacks --- commonjs/index.js | 2 +- src/__tests__/asyncBootstrapper.test.js | 36 +++++++++++++++++++++++++ src/index.js | 2 +- umd/react-async-bootstrapper.js | 2 +- umd/react-async-bootstrapper.min.js | 2 +- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/commonjs/index.js b/commonjs/index.js index 3cd44fb..4c390d5 100644 --- a/commonjs/index.js +++ b/commonjs/index.js @@ -14,7 +14,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de function asyncBootstrapper(app, options) { var visitor = function visitor(element, instance) { if (instance && typeof instance.asyncBootstrap === 'function') { - return instance.asyncBootstrap(); + return instance.asyncBootstrap(options); } return true; }; diff --git a/src/__tests__/asyncBootstrapper.test.js b/src/__tests__/asyncBootstrapper.test.js index 3c9dd6b..788e61e 100644 --- a/src/__tests__/asyncBootstrapper.test.js +++ b/src/__tests__/asyncBootstrapper.test.js @@ -34,4 +34,40 @@ describe('asyncBootstrapper()', () => { expect(values).toEqual([1, 2, 4, 3]), ) }) + + it('passes options through if given', () => { + const values = [] + + class Foo extends Component { + asyncBootstrap(options) { + values.push({ ...options, id: this.props.id }) + return true + } + + render() { + return
{this.props.children}
+ } + } + + const app = ( + +
+

Test

+
+ + + + + + ) + + return asyncBootstrapper(app, { key1: 'val1' }).then(() => + expect(values).toEqual([ + { id: 1, key1: 'val1' }, + { id: 2, key1: 'val1' }, + { id: 4, key1: 'val1' }, + { id: 3, key1: 'val1' }, + ]), + ) + }) }) diff --git a/src/index.js b/src/index.js index dc4f027..d185f8a 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ import reactTreeWalker from 'react-tree-walker' export default function asyncBootstrapper(app, options) { const visitor = (element, instance) => { if (instance && typeof instance.asyncBootstrap === 'function') { - return instance.asyncBootstrap() + return instance.asyncBootstrap(options) } return true } diff --git a/umd/react-async-bootstrapper.js b/umd/react-async-bootstrapper.js index 24d17c7..37937b0 100644 --- a/umd/react-async-bootstrapper.js +++ b/umd/react-async-bootstrapper.js @@ -94,7 +94,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de function asyncBootstrapper(app, options) { var visitor = function visitor(element, instance) { if (instance && typeof instance.asyncBootstrap === 'function') { - return instance.asyncBootstrap(); + return instance.asyncBootstrap(options); } return true; }; diff --git a/umd/react-async-bootstrapper.min.js b/umd/react-async-bootstrapper.min.js index f517cb6..adf9e92 100644 --- a/umd/react-async-bootstrapper.min.js +++ b/umd/react-async-bootstrapper.min.js @@ -1 +1 @@ -!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n(require("react")):"function"==typeof define&&define.amd?define(["react"],n):"object"==typeof exports?exports["react-async-bootstrapper"]=n(require("react")):t["react-async-bootstrapper"]=n(t.React)}(this,function(t){return function(t){function n(o){if(e[o])return e[o].exports;var r=e[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}var e={};return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:o})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=0)}([function(t,n,e){"use strict";function o(t,n){var e=function(t,n){return!n||"function"!=typeof n.asyncBootstrap||n.asyncBootstrap()};return(0,c.default)(t,e,{},n)}Object.defineProperty(n,"__esModule",{value:!0}),n.default=o;var r=e(1),c=function(t){return t&&t.__esModule?t:{default:t}}(r)},function(t,n,e){"use strict";function o(t,n,e){var u=arguments.length>3&&void 0!==arguments[3]?arguments[3]:c;return new Promise(function(c){var s=function(t,e,s){var a=function(e){e||c();var l=t(),a="function"==typeof s?s():s;if(null==l)c();else if(r.Children.count(l)){var f=function(t){return t?o(t,n,a,u):void 0};i(r.Children.map(l,function(t){return t}),f).then(c)}else o(l,n,a,u).then(c)};!1===e?c():l(e)?e.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)};if("function"==typeof t.type){var a=t.type,f=Object.assign({},a.defaultProps,t.props);if(a.prototype&&(a.prototype.isReactComponent||a.prototype.isPureReactComponent)){var p=new a(f,e);p.props=p.props||f,p.context=p.context||e,p.setState=function(t){p.state=Object.assign({},p.state,t)},s(function(){p.componentWillMount&&p.componentWillMount();var t=p.render();if(u.componentWillUnmount&&p.componentWillUnmount)try{p.componentWillUnmount()}catch(t){console.warn("Error calling componentWillUnmount whilst walking your react tree"),console.warn(t)}return t},n(t,p,e),function(){return p.getChildContext?Object.assign({},e,p.getChildContext()):e})}else s(function(){return a(f,e)},n(t,null,e),e)}else s(function(){return t.props&&t.props.children?t.props.children:void 0},n(t,null,e),e)}).catch(function(t){console.error("Error walking your react tree"),console.error(t)})}Object.defineProperty(n,"__esModule",{value:!0}),n.isPromise=void 0,n.default=o;var r=e(2),c={componentWillUnmount:!1},u=function(t,n,e){return new Promise(function(o,r){var c=t[Symbol.iterator](),u=0;!function t(e){var i=c.next();if(i.done)return void o(e);Promise.all([e,i.value]).then(function(e){t(n(e[0],e[1],u++))}).catch(r)}(e)})},i=function(t,n){var e=[];return u(t,function(t,o,r){return Promise.resolve(n(o,r)).then(function(t){e.push(t)})}).then(function(){return e})},l=n.isPromise=function(t){return null!=t&&"function"==typeof t.then}},function(n,e){n.exports=t}])}); \ No newline at end of file +!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n(require("react")):"function"==typeof define&&define.amd?define(["react"],n):"object"==typeof exports?exports["react-async-bootstrapper"]=n(require("react")):t["react-async-bootstrapper"]=n(t.React)}(this,function(t){return function(t){function n(o){if(e[o])return e[o].exports;var r=e[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}var e={};return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:o})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=0)}([function(t,n,e){"use strict";function o(t,n){var e=function(t,e){return!e||"function"!=typeof e.asyncBootstrap||e.asyncBootstrap(n)};return(0,c.default)(t,e,{},n)}Object.defineProperty(n,"__esModule",{value:!0}),n.default=o;var r=e(1),c=function(t){return t&&t.__esModule?t:{default:t}}(r)},function(t,n,e){"use strict";function o(t,n,e){var u=arguments.length>3&&void 0!==arguments[3]?arguments[3]:c;return new Promise(function(c){var s=function(t,e,s){var a=function(e){e||c();var l=t(),a="function"==typeof s?s():s;if(null==l)c();else if(r.Children.count(l)){var f=function(t){return t?o(t,n,a,u):void 0};i(r.Children.map(l,function(t){return t}),f).then(c)}else o(l,n,a,u).then(c)};!1===e?c():l(e)?e.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)};if("function"==typeof t.type){var a=t.type,f=Object.assign({},a.defaultProps,t.props);if(a.prototype&&(a.prototype.isReactComponent||a.prototype.isPureReactComponent)){var p=new a(f,e);p.props=p.props||f,p.context=p.context||e,p.setState=function(t){p.state=Object.assign({},p.state,t)},s(function(){p.componentWillMount&&p.componentWillMount();var t=p.render();if(u.componentWillUnmount&&p.componentWillUnmount)try{p.componentWillUnmount()}catch(t){console.warn("Error calling componentWillUnmount whilst walking your react tree"),console.warn(t)}return t},n(t,p,e),function(){return p.getChildContext?Object.assign({},e,p.getChildContext()):e})}else s(function(){return a(f,e)},n(t,null,e),e)}else s(function(){return t.props&&t.props.children?t.props.children:void 0},n(t,null,e),e)}).catch(function(t){console.error("Error walking your react tree"),console.error(t)})}Object.defineProperty(n,"__esModule",{value:!0}),n.isPromise=void 0,n.default=o;var r=e(2),c={componentWillUnmount:!1},u=function(t,n,e){return new Promise(function(o,r){var c=t[Symbol.iterator](),u=0;!function t(e){var i=c.next();if(i.done)return void o(e);Promise.all([e,i.value]).then(function(e){t(n(e[0],e[1],u++))}).catch(r)}(e)})},i=function(t,n){var e=[];return u(t,function(t,o,r){return Promise.resolve(n(o,r)).then(function(t){e.push(t)})}).then(function(){return e})},l=n.isPromise=function(t){return null!=t&&"function"==typeof t.then}},function(n,e){n.exports=t}])}); \ No newline at end of file