UNPKG

react-on-rails

Version:

react-on-rails JavaScript for react_on_rails Ruby gem

93 lines (89 loc) 4.76 kB
/** * @deprecated Use `capabilities/core.ts` instead. This file is kept for backward compatibility * with older versions of react-on-rails-pro that import from `react-on-rails/@internal/base/client`. */ import { createCoreCapability } from "../capabilities/core.js"; // Pro-only methods that `createCoreCapability` includes as stubs but the historical base // surface omits. This list drives the runtime key-stripping in `createBaseClientObject`. // `satisfies readonly (keyof ReactOnRailsInternal)[]` makes a typo or a stale name a compile // error, and the `assertProOnlyMethodsMatchOmit` guard inside `createBaseClientObject` pins this // list to the `Omit<...>` in `BaseClientObjectType` so the runtime list and the exported type can // never drift. const PRO_ONLY_METHODS = [ 'getOrWaitForComponent', 'getOrWaitForStore', 'getOrWaitForStoreGenerator', 'reactOnRailsStoreLoaded', 'isServerStreamingSupported', 'streamServerRenderedReactComponent', 'serverRenderRSCReactComponent', 'addAsyncPropsCapabilityToComponentProps', 'getOrCreateAsyncPropsManager', ]; // Cache to track created objects and their registries let cachedObject = null; let cachedRegistries = null; export function createBaseClientObject(registries, currentObject = null) { // Error detection: currentObject is null but we have a cached object // This indicates webpack misconfiguration (multiple runtime chunks) if (currentObject === null && cachedObject !== null) { throw new Error(`\ ReactOnRails was already initialized, but a new initialization was attempted without passing the existing global. This usually means Webpack's optimization.runtimeChunk is set to "true" or "multiple" instead of "single". Fix: Set optimization.runtimeChunk to "single" in your webpack configuration. See: https://github.com/shakacode/react_on_rails/issues/1558`); } // Error detection: currentObject exists but doesn't match cached object // This could indicate: // 1. Global was contaminated by external code // 2. Mixing core and pro packages if (currentObject !== null && cachedObject !== null && currentObject !== cachedObject) { throw new Error(`\ ReactOnRails global object mismatch detected. The current global ReactOnRails object is different from the one created by this package. This usually means: 1. You're mixing react-on-rails (core) with react-on-rails-pro 2. Another library is interfering with the global ReactOnRails object Fix: Use only one package (core OR pro) consistently throughout your application.`); } // Error detection: Different registries with existing cache // This indicates mixing core and pro packages if (cachedRegistries !== null) { if (registries.ComponentRegistry !== cachedRegistries.ComponentRegistry || registries.StoreRegistry !== cachedRegistries.StoreRegistry) { throw new Error(`\ Cannot mix react-on-rails (core) with react-on-rails-pro. Different registries detected - the packages use incompatible registries. Fix: Use only react-on-rails OR react-on-rails-pro, not both.`); } } // If we have a cached object, return it (all checks passed above) if (cachedObject !== null) { return cachedObject; } const assertProOnlyMethodsMatchOmit = true; void assertProOnlyMethodsMatchOmit; // Delegate the core method implementations to `createCoreCapability` (the canonical source), // then re-shape to the historical base surface: strip the Pro-only stubs (using the shared // `PRO_ONLY_METHODS` list that the drift guard above pins to `BaseClientObjectType`, so runtime // and type can never drift) and add the client-side lifecycle stubs that `createReactOnRails` // overrides at initialization. const proOnlyMethods = new Set(PRO_ONLY_METHODS); const core = createCoreCapability(registries); const obj = Object.fromEntries(Object.entries(core).filter(([key]) => !proOnlyMethods.has(key))); // =================================================================== // CLIENT-SIDE RENDERING STUBS - To be overridden by createReactOnRails // =================================================================== obj.reactOnRailsPageLoaded = () => { throw new Error('ReactOnRails.reactOnRailsPageLoaded is not initialized. This is a bug in react-on-rails.'); }; obj.reactOnRailsComponentLoaded = (domId) => { void domId; // Mark as used throw new Error('ReactOnRails.reactOnRailsComponentLoaded is not initialized. This is a bug in react-on-rails.'); }; // Cache the object and registries cachedObject = obj; cachedRegistries = registries; return obj; } //# sourceMappingURL=client.js.map