UNPKG

react-injectables

Version:

Explicitly inject Components into any part of your React render tree.

1 lines 12.4 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define("react-injectables",["react"],t):"object"==typeof exports?exports["react-injectables"]=t(require("react")):e["react-injectables"]=t(e.React)}(this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return e[r].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(t,"__esModule",{value:!0}),t.Injector=t.Injectable=t.InjectablesProvider=void 0;var i=n(5),o=r(i),c=n(4),u=r(c),a=n(6),s=r(a);t.InjectablesProvider=o.default,t.Injectable=u.default,t.Injector=s.default},function(t,n){t.exports=e},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function i(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);e.length>t;t++)n[t]=e[t];return n}return Array.from(e)}function o(){for(var e=arguments.length,t=Array(e),n=0;e>n;n++)t[n]=arguments[n];return function(){if(0===t.length)return arguments.length>0?arguments[0]:void 0;var e=t[t.length-1],n=t.slice(0,-1);return n.reduceRight(function(e,t){return t(e)},e.apply(void 0,arguments))}}function c(e){var t=e.children;return a.Children.only(t)}function u(e,t){var n=0;return t.map(function(t){return n++,s.default.createElement(c,{key:e+"_"+n},t)})}Object.defineProperty(t,"__esModule",{value:!0}),t.find=t.map=t.concatAll=t.containsUniq=t.uniqBy=t.withoutAll=t.without=t.all=t.filter=void 0,t.compose=o,t.keyedElements=u;var a=n(1),s=r(a),f=t.filter=function(e){return function(t){return t.filter(e)}},l=t.all=function(e){return function(t){for(var n=0;t.length>n;n++)if(!e(t[n]))return!1;return!0}},j=(t.without=function(e){return function(t){return f(function(t){return!Object.is(t,e)})(t)}},t.withoutAll=function(e){return function(t){return f(function(t){return l(function(e){return!Object.is(t,e)})(e)})(t)}});t.uniqBy=function(e){return function(t){var n=new Set,r=[];return t.forEach(function(t){var i=t[e];n.has(i)||(n.add(i),r.push(t))}),r}},t.containsUniq=function(e){return function(t){return j(e)(t).length>0}},t.concatAll=function(e){return e.reduce(function(e,t){return[].concat(i(e),i(t))},[])},t.map=function(e){return function(t){return t.map(e)}},t.find=function(e){return function(t){return t.find(e)}}},function(e,t,n){"use strict";var r=function(e,t,n,r,i,o,c,u){if(!e){var a;if(void 0===t)a=Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var s=[n,r,i,o,c,u],f=0;a=Error(t.replace(/%s/g,function(){return s[f++]})),a.name="Invariant Violation"}throw a.framesToPop=1,a}};e.exports=r},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function c(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var u=Object.assign||function(e){for(var t=1;arguments.length>t;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},a=function(){function e(e,t){for(var n=0;t.length>n;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),s=n(1),f=r(s),l=n(2),j=0,p=function(e){j++;var t="injectionId_"+j,n=function(n){function r(){var e,t,n,c;i(this,r);for(var u=arguments.length,a=Array(u),s=0;u>s;s++)a[s]=arguments[s];return t=n=o(this,(e=Object.getPrototypeOf(r)).call.apply(e,[this].concat(a))),n.state={injections:[]},n.consume=function(e){(e.length!==n.state.injections.length||(0,l.containsUniq)(n.state.injections,e))&&n.setState({injections:e})},c=t,o(n,c)}return c(r,n),a(r,[{key:"componentWillMount",value:function(){var e=this;this.context.registerInjectable({injectionId:t,injectableInstance:this,receive:function(t){return e.consume(t)}})}},{key:"componentWillUnmount",value:function(){this.context.removeInjectable({injectionId:t,injectableInstance:this})}},{key:"render",value:function(){var t=(0,l.keyedElements)("injections",this.state.injections);return f.default.createElement(e,u({injections:t},this.props))}}]),r}(s.Component);return n.injectionId=t,n.contextTypes={registerInjectable:s.PropTypes.func.isRequired,removeInjectable:s.PropTypes.func.isRequired},n};t.default=p},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function i(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);e.length>t;t++)n[t]=e[t];return n}return Array.from(e)}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function u(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;t.length>n;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),s=n(1),f=n(2),l=n(3),j=r(l),p=function(e){function t(e,n){o(this,t);var r=c(this,Object.getPrototypeOf(t).call(this,e,n));return r.registrations=[],r}return u(t,e),a(t,[{key:"getChildContext",value:function(){var e=this;return{registerInjector:function(t){return e.registerInjector(t)},removeInjector:function(t){return e.removeInjector(t)},updateInjector:function(t){return e.updateInjector(t)},registerInjectable:function(t){return e.registerInjectable(t)},removeInjectable:function(t){return e.removeInjectable(t)}}}},{key:"getRegistration",value:function(e){var t=e.injectionId,n=(0,f.find)(function(e){return e.injectionId===t})(this.registrations);return n||(n={injectionId:t,injectables:[],injections:[]},this.registrations.push(n)),n}},{key:"runInjections",value:function(e){var t=e.registration,n=t.injectables,r=t.injections,i=(0,f.compose)((0,f.filter)(function(e){return null!==e&&void 0!==e}),(0,f.map)(function(e){return e.inject()}),(0,f.uniqBy)("injectorId"))(r);n.forEach(function(e){e.receive(i)})}},{key:"removeRegistration",value:function(e){var t=e.registration;this.registrations=(0,f.without)(t)(this.registrations)}},{key:"findInjectable",value:function(e){var t=e.registration,n=e.injectableInstance,r=function(e){return Object.is(e.instance,n)};return(0,f.find)(r)(t.injectables)}},{key:"clearRegistrationIfEmpty",value:function(e){var t=e.registration;0===t.injectables.length&&0===t.injections.length&&this.removeRegistration({registration:t})}},{key:"registerInjectable",value:function(e){var t=e.injectionId,n=e.injectableInstance,r=e.receive,o=this.getRegistration({injectionId:t}),c=this.findInjectable({registration:o,injectableInstance:n});if((0,f.withoutAll)(o.injectables)([c]).length>0){var u={instance:n,receive:r};o.injectables=[].concat(i(o.injectables),[u]),this.runInjections({registration:o})}}},{key:"removeInjectable",value:function(e){var t=e.injectionId,n=e.injectableInstance,r=this.getRegistration({injectionId:t}),i=this.findInjectable({registration:r,injectableInstance:n});r.injectables=(0,f.without)(i)(r.injectables),this.clearRegistrationIfEmpty({registration:r})}},{key:"findInjection",value:function(e){var t=e.registration,n=e.injectorInstance,r=function(e){return Object.is(e.instance,n)};return(0,f.find)(r)(t.injections)}},{key:"findInjector",value:function(e){var t=e.registration,n=e.injectorId,r=function(e){return e.injectorId===n};return(0,f.find)(r)(t.injections)}},{key:"registerInjector",value:function(e){var t=e.injectionId,n=e.injectorId,r=e.injectorInstance,o=e.inject,c=this.getRegistration({injectionId:t}),u=this.findInjection({registration:c,injectorInstance:r});(0,j.default)(!u,"An Injector instance is being registered multiple times.");var a={injectorId:n,instance:r,inject:o};c.injections=[].concat(i(c.injections),[a]),this.runInjections({registration:c})}},{key:"updateInjector",value:function(e){var t=e.injectionId,n=e.injectorInstance,r=e.inject,i=this.getRegistration({injectionId:t}),o=this.findInjection({registration:i,injectorInstance:n});(0,j.default)(o,"Trying to update an Injector that is not registered"),o.inject=r,this.runInjections({registration:i})}},{key:"removeInjector",value:function(e){var t=e.injectionId,n=e.injectorInstance,r=this.getRegistration({injectionId:t}),i=this.findInjection({registration:r,injectorInstance:n});(0,j.default)(!!i,"Trying to remove an injector which has not been registered"),r.injections=(0,f.without)(i)(r.injections),this.runInjections({registration:r}),this.clearRegistrationIfEmpty({registration:r})}},{key:"render",value:function(){return s.Children.only(this.props.children)}}]),t}(s.Component);p.childContextTypes={registerInjector:s.PropTypes.func.isRequired,removeInjector:s.PropTypes.func.isRequired,updateInjector:s.PropTypes.func.isRequired,registerInjectable:s.PropTypes.func.isRequired,removeInjectable:s.PropTypes.func.isRequired},p.propTypes={children:s.PropTypes.element},t.default=p},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function c(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var u=function(){function e(e,t){for(var n=0;t.length>n;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),a=n(1),s=r(a),f=n(3),l=r(f),j="Invalid Injectable target. Please provide a Component that has been wrapped Injectable wrapped Component.",p="Invalid injection value provided into Injector. You must supply a Component or stateless Component.",d=0,h=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.into;return(0,l.default)(t&&"function"==typeof t&&t.injectionId&&t.contextTypes&&t.contextTypes.registerInjectable&&t.contextTypes.removeInjectable,j),function(e){(0,l.default)(e&&"function"==typeof e,p),d++;var n="injector_"+d,r=function(r){function a(){return i(this,a),o(this,Object.getPrototypeOf(a).apply(this,arguments))}return c(a,r),u(a,[{key:"componentWillMount",value:function(){var r=this;this.context.registerInjector({injectionId:t.injectionId,injectorId:n,injectorInstance:this,inject:function(){return s.default.createElement(e,r.props)}})}},{key:"componentWillUpdate",value:function(r){this.context.updateInjector({injectionId:t.injectionId,injectorId:n,injectorInstance:this,inject:function(){return s.default.createElement(e,r)}})}},{key:"componentWillUnmount",value:function(){this.context.removeInjector({injectionId:t.injectionId,injectorId:n,injectorInstance:this})}},{key:"render",value:function(){return null}}]),a}(a.Component);return r.contextTypes={registerInjector:a.PropTypes.func.isRequired,updateInjector:a.PropTypes.func.isRequired,removeInjector:a.PropTypes.func.isRequired},r}};t.default=h}])});