react-service-container
Version:
A simple, robust, idiomatic service locator library for React applications
2 lines • 5.14 kB
JavaScript
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r(require("react")):"function"==typeof define&&define.amd?define(["react"],r):"object"==typeof exports?exports.ReactServiceContainer=r(require("react")):e.ReactServiceContainer=r(e.React)}(window,(function(e){return function(e){var r={};function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var i in e)t.d(n,i,function(r){return e[r]}.bind(null,i));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){"use strict";var n=this&&this.__createBinding||(Object.create?function(e,r,t,n){void 0===n&&(n=t),Object.defineProperty(e,n,{enumerable:!0,get:function(){return r[t]}})}:function(e,r,t,n){void 0===n&&(n=t),e[n]=r[t]}),i=this&&this.__setModuleDefault||(Object.create?function(e,r){Object.defineProperty(e,"default",{enumerable:!0,value:r})}:function(e,r){e.default=r}),o=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(null!=e)for(var t in e)"default"!==t&&Object.hasOwnProperty.call(e,t)&&n(r,e,t);return i(r,e),r};Object.defineProperty(r,"__esModule",{value:!0}),r.useService=r.ServiceContainer=r.ServiceContainerContext=r.ServiceContainerRegistry=void 0;var a=o(t(1)),u=Symbol.for("uninstantiated"),s=function(){function e(e){this.parent=e,this.providers=new Map,this.parent=e,this.providers=new Map}return e.prototype.get=function(e){if(this.providers.has(e)){var r=this.providers.get(e);try{return r()}catch(r){throw console.error("[react-service-container] Provider for "+String(e)+" threw an error. Please check the error output below for more information."),r}}if(null!=this.parent)return this.parent.get(e);var t="[react-service-container] Could not find provider for token "+String(e)+'. Think of this as a "missing variable" error. Ensure that in one of your parent service containers, you have configured your providers array to provide a value for this type.';throw new Error(t)},e.prototype.add=function(e){var r=this;if(!("provide"in e)){var t='[react-service-container] Missing "provide" key in object with key(s): '+f(e)+'. Each provider must specify a "provide" key as well as one of the correct use* values.';throw new Error(t)}var n=u;this.providers.set(e.provide,(function(){if(n!==u)return n;var t;switch(!0){case"useValue"in e:var i=e.useValue;t=function(){return i};break;case"useClass"in e:var o=e.useClass;t=function(){return new o};break;case"useFactory"in e:t=e.useFactory;break;case"useExisting"in e:var a=e.useExisting;t=function(){try{return r.get(a)}catch(r){var t="[react-service-container] Failed alias lookup for useExisting provider "+String(e)+". It looks like you passed a token to `useExisting` that was not registered as a provider. Ensure that the token given is registered *before* the alias is referenced. If the value reference by the alias is provided within the same providers array as the alias, ensure that it comes before the alias in the providers array.";throw new Error(t)}};break;default:var s="[create-service-container] Provider missing proper use* value in key(s): "+f(e,(function(e){return"provide"!==e}))+'. Possible values are: ["useValue", "useClass", "useFactory", "useExisting"]';throw new Error(s)}return n=t()}))},e}();function c(e){return{get:function(r){return e.get(r)}}}function f(e,r){return void 0===r&&(r=function(e){return!0}),Object.keys(e).filter(r).map((function(e){return'"'+e+'"'})).join(", ")}r.ServiceContainerRegistry=s,r.ServiceContainerContext=a.default.createContext(null),r.ServiceContainer=function(e){var t=e.providers,n=e.children,i=function(e,r){var t=new s(r);return function(e,r){(function(e){return e.map((function(e){return"function"==typeof e?{provide:e,useClass:e}:e}))})(r).forEach((function(r){e.add(r)}))}(t,e),t}(t,a.useContext(r.ServiceContainerContext));return a.default.createElement(r.ServiceContainerContext.Provider,{value:c(i)},n)},r.useService=function(e){var t=a.useContext(r.ServiceContainerContext);if(!t){throw new Error("[react-service-container] Could not find service container context. It looks like you may have used the useService() hook in a component that is not a child of a <ServiceContainer>...</>. Take a look at your component tree and ensure that somewhere in the hierarchy before this component is rendered, there is a <ServiceContainer> available")}return t.get(e)}},function(r,t){r.exports=e}])}));
//# sourceMappingURL=react-service-container.min.js.map