UNPKG

react-service-container

Version:

A simple, robust, idiomatic service locator library for React applications

166 lines (165 loc) 7.55 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useService = exports.ServiceContainer = exports.ServiceContainerContext = exports.ServiceContainerRegistry = void 0; var react_1 = __importStar(require("react")); var UNINSTANTIATED = Symbol.for("uninstantiated"); var ServiceContainerRegistry = /** @class */ (function () { function ServiceContainerRegistry(parent) { this.parent = parent; this.providers = new Map(); this.parent = parent; this.providers = new Map(); } ServiceContainerRegistry.prototype.get = function (serviceToken) { if (this.providers.has(serviceToken)) { var initFn = this.providers.get(serviceToken); try { return initFn(); } catch (err) { console.error("[react-service-container] Provider for " + String(serviceToken) + " threw an error. " + "Please check the error output below for more information."); throw err; } } if (this.parent != null) { // This will recursively call each parent registry, until the base environment is hit in // which case the base case is thrown below. return this.parent.get(serviceToken); } var errorMsg = "[react-service-container] Could not find provider for token " + String(serviceToken) + ". " + '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(errorMsg); }; ServiceContainerRegistry.prototype.add = function (provider) { var _this = this; if (!("provide" in provider)) { var errorMsg = "[react-service-container] Missing \"provide\" key in object with key(s): " + stringifyKeys(provider) + ". " + 'Each provider must specify a "provide" key as well as one of the correct use* values.'; throw new Error(errorMsg); } var instance = UNINSTANTIATED; var initFn = function () { if (instance !== UNINSTANTIATED) { return instance; } var init; switch (true) { case "useValue" in provider: var value_1 = provider.useValue; init = function () { return value_1; }; break; case "useClass" in provider: var Ctor_1 = provider.useClass; init = function () { return new Ctor_1(); }; break; case "useFactory" in provider: init = provider.useFactory; break; case "useExisting" in provider: var resolvedAlias_1 = provider .useExisting; init = function () { try { return _this.get(resolvedAlias_1); } catch (_) { var errorMessage = "[react-service-container] Failed alias lookup for useExisting provider " + String(provider) + ". " + "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(errorMessage); } }; break; default: var errorMsg = "[create-service-container] Provider missing proper use* value in key(s): " + stringifyKeys(provider, function (k) { return k !== "provide"; }) + ". " + 'Possible values are: ["useValue", "useClass", "useFactory", "useExisting"]'; throw new Error(errorMsg); } instance = init(); return instance; }; this.providers.set(provider.provide, initFn); }; return ServiceContainerRegistry; }()); exports.ServiceContainerRegistry = ServiceContainerRegistry; exports.ServiceContainerContext = react_1.default.createContext(null); function ServiceContainer(_a) { var providers = _a.providers, children = _a.children; var parent = react_1.useContext(exports.ServiceContainerContext); var registry = buildRegistry(providers, parent); return (react_1.default.createElement(exports.ServiceContainerContext.Provider, { value: readonlyProxy(registry) }, children)); } exports.ServiceContainer = ServiceContainer; function useService(serviceToken) { var container = react_1.useContext(exports.ServiceContainerContext); if (!container) { var errorMsg = "[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"; throw new Error(errorMsg); } return container.get(serviceToken); } exports.useService = useService; function readonlyProxy(registry) { var proxy = { get: function (serviceToken) { return registry.get(serviceToken); }, }; return proxy; } function buildRegistry(providers, parent) { var registry = new ServiceContainerRegistry(parent); addProviders(registry, providers); return registry; } function addProviders(registry, providers) { normalize(providers).forEach(function (provider) { registry.add(provider); }); } function normalize(providers) { return providers.map(function (provider) { var assumeClassShorthand = typeof provider === "function"; if (assumeClassShorthand) { return { provide: provider, useClass: provider, }; } return provider; }); } function stringifyKeys(obj, filter) { if (filter === void 0) { filter = function (k) { return true; }; } return Object.keys(obj) .filter(filter) .map(function (k) { return "\"" + k + "\""; }) .join(", "); }