UNPKG

react-on-rails

Version:

react-on-rails JavaScript for react_on_rails Ruby gem

50 lines (49 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const isRenderFunction_1 = require("./isRenderFunction"); const registeredComponents = new Map(); exports.default = { /** * @param components { component1: component1, component2: component2, etc. } */ register(components) { Object.keys(components).forEach(name => { if (registeredComponents.has(name)) { console.warn('Called register for component that is already registered', name); } const component = components[name]; if (!component) { throw new Error(`Called register with null component named ${name}`); } const renderFunction = (0, isRenderFunction_1.default)(component); const isRenderer = renderFunction && component.length === 3; registeredComponents.set(name, { name, component, renderFunction, isRenderer, }); }); }, /** * @param name * @returns { name, component, isRenderFunction, isRenderer } */ get(name) { const registeredComponent = registeredComponents.get(name); if (registeredComponent !== undefined) { return registeredComponent; } const keys = Array.from(registeredComponents.keys()).join(', '); throw new Error(`Could not find component registered with name ${name}. \ Registered component names include [ ${keys} ]. Maybe you forgot to register the component?`); }, /** * Get a Map containing all registered components. Useful for debugging. * @returns Map where key is the component name and values are the * { name, component, renderFunction, isRenderer} */ components() { return registeredComponents; }, };