react-on-rails 16.1.0 → 16.2.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/{node_package/lib → lib}/Authenticity.d.ts +1 -0
  2. package/{node_package/lib → lib}/Authenticity.js +1 -0
  3. package/lib/ClientRenderer.d.ts +18 -0
  4. package/lib/ClientRenderer.js +155 -0
  5. package/lib/ComponentRegistry.d.ts +31 -0
  6. package/lib/ComponentRegistry.js +64 -0
  7. package/{node_package/lib → lib}/ReactDOMServer.cjs +1 -0
  8. package/lib/ReactOnRails.client.d.ts +4 -0
  9. package/lib/ReactOnRails.client.js +7 -0
  10. package/lib/ReactOnRails.full.d.ts +4 -0
  11. package/lib/ReactOnRails.full.js +7 -0
  12. package/{node_package/lib → lib}/RenderUtils.d.ts +1 -0
  13. package/{node_package/lib → lib}/RenderUtils.js +1 -0
  14. package/lib/StoreRegistry.d.ts +58 -0
  15. package/lib/StoreRegistry.js +113 -0
  16. package/lib/base/client.d.ts +25 -0
  17. package/lib/base/client.js +207 -0
  18. package/lib/base/full.d.ts +15 -0
  19. package/lib/base/full.js +35 -0
  20. package/lib/base/full.rsc.d.ts +5 -0
  21. package/lib/base/full.rsc.js +27 -0
  22. package/{node_package/lib → lib}/buildConsoleReplay.d.ts +1 -0
  23. package/{node_package/lib → lib}/buildConsoleReplay.js +1 -0
  24. package/lib/clientStartup.d.ts +3 -0
  25. package/lib/clientStartup.js +27 -0
  26. package/{node_package/lib → lib}/context.d.ts +2 -1
  27. package/{node_package/lib → lib}/context.js +1 -0
  28. package/lib/createReactOnRails.d.ts +7 -0
  29. package/lib/createReactOnRails.js +68 -0
  30. package/{node_package/lib → lib}/createReactOutput.d.ts +1 -0
  31. package/{node_package/lib → lib}/createReactOutput.js +5 -4
  32. package/lib/generateRenderingErrorMessage.d.ts +4 -0
  33. package/{node_package/lib/handleError.js → lib/generateRenderingErrorMessage.js} +3 -10
  34. package/{node_package/lib → lib}/handleError.d.ts +1 -0
  35. package/lib/handleError.js +10 -0
  36. package/{node_package/lib → lib}/isRenderFunction.d.ts +1 -0
  37. package/{node_package/lib → lib}/isRenderFunction.js +1 -0
  38. package/{node_package/lib → lib}/isServerRenderResult.d.ts +1 -0
  39. package/{node_package/lib → lib}/isServerRenderResult.js +1 -0
  40. package/{node_package/lib → lib}/pageLifecycle.d.ts +1 -0
  41. package/{node_package/lib → lib}/pageLifecycle.js +1 -0
  42. package/{node_package/lib → lib}/reactApis.cjs +1 -0
  43. package/{node_package/lib → lib}/reactHydrateOrRender.d.ts +1 -0
  44. package/{node_package/lib → lib}/reactHydrateOrRender.js +1 -0
  45. package/{node_package/lib → lib}/scriptSanitizedVal.d.ts +1 -0
  46. package/{node_package/lib → lib}/scriptSanitizedVal.js +1 -0
  47. package/{node_package/lib → lib}/serverRenderReactComponent.d.ts +1 -0
  48. package/{node_package/lib → lib}/serverRenderReactComponent.js +9 -4
  49. package/{node_package/lib → lib}/serverRenderUtils.d.ts +1 -0
  50. package/{node_package/lib → lib}/serverRenderUtils.js +1 -0
  51. package/{node_package/lib → lib}/turbolinksUtils.d.ts +1 -0
  52. package/{node_package/lib → lib}/turbolinksUtils.js +1 -0
  53. package/{node_package/lib → lib}/types/index.d.ts +5 -1
  54. package/{node_package/lib → lib}/types/index.js +3 -0
  55. package/package.json +55 -101
  56. package/LICENSE.md +0 -47
  57. package/README.md +0 -217
  58. package/node_package/lib/ReactDOMServer.d.cts +0 -1
  59. package/node_package/lib/ReactOnRails.client.d.ts +0 -3
  60. package/node_package/lib/ReactOnRails.client.js +0 -134
  61. package/node_package/lib/ReactOnRails.full.d.ts +0 -3
  62. package/node_package/lib/ReactOnRails.full.js +0 -11
  63. package/node_package/lib/ReactOnRails.node.d.ts +0 -2
  64. package/node_package/lib/ReactOnRails.node.js +0 -6
  65. package/node_package/lib/clientStartup.d.ts +0 -2
  66. package/node_package/lib/clientStartup.js +0 -32
  67. package/node_package/lib/loadJsonFile.d.ts +0 -3
  68. package/node_package/lib/loadJsonFile.js +0 -22
  69. package/node_package/lib/pro/CallbackRegistry.d.ts +0 -17
  70. package/node_package/lib/pro/CallbackRegistry.js +0 -111
  71. package/node_package/lib/pro/ClientSideRenderer.d.ts +0 -7
  72. package/node_package/lib/pro/ClientSideRenderer.js +0 -238
  73. package/node_package/lib/pro/ComponentRegistry.d.ts +0 -19
  74. package/node_package/lib/pro/ComponentRegistry.js +0 -54
  75. package/node_package/lib/pro/PostSSRHookTracker.d.ts +0 -31
  76. package/node_package/lib/pro/PostSSRHookTracker.js +0 -69
  77. package/node_package/lib/pro/RSCProvider.d.ts +0 -51
  78. package/node_package/lib/pro/RSCProvider.js +0 -88
  79. package/node_package/lib/pro/RSCRequestTracker.d.ts +0 -74
  80. package/node_package/lib/pro/RSCRequestTracker.js +0 -129
  81. package/node_package/lib/pro/RSCRoute.d.ts +0 -29
  82. package/node_package/lib/pro/RSCRoute.js +0 -47
  83. package/node_package/lib/pro/ReactOnRailsRSC.d.ts +0 -3
  84. package/node_package/lib/pro/ReactOnRailsRSC.js +0 -81
  85. package/node_package/lib/pro/ServerComponentFetchError.d.ts +0 -14
  86. package/node_package/lib/pro/ServerComponentFetchError.js +0 -32
  87. package/node_package/lib/pro/StoreRegistry.d.ts +0 -52
  88. package/node_package/lib/pro/StoreRegistry.js +0 -101
  89. package/node_package/lib/pro/getReactServerComponent.client.d.ts +0 -46
  90. package/node_package/lib/pro/getReactServerComponent.client.js +0 -155
  91. package/node_package/lib/pro/getReactServerComponent.server.d.ts +0 -38
  92. package/node_package/lib/pro/getReactServerComponent.server.js +0 -69
  93. package/node_package/lib/pro/injectRSCPayload.d.ts +0 -30
  94. package/node_package/lib/pro/injectRSCPayload.js +0 -275
  95. package/node_package/lib/pro/registerServerComponent/client.d.ts +0 -32
  96. package/node_package/lib/pro/registerServerComponent/client.js +0 -42
  97. package/node_package/lib/pro/registerServerComponent/server.d.ts +0 -21
  98. package/node_package/lib/pro/registerServerComponent/server.js +0 -30
  99. package/node_package/lib/pro/registerServerComponent/server.rsc.d.ts +0 -23
  100. package/node_package/lib/pro/registerServerComponent/server.rsc.js +0 -34
  101. package/node_package/lib/pro/streamServerRenderedReactComponent.d.ts +0 -34
  102. package/node_package/lib/pro/streamServerRenderedReactComponent.js +0 -261
  103. package/node_package/lib/pro/transformRSCNodeStream.d.ts +0 -15
  104. package/node_package/lib/pro/transformRSCNodeStream.js +0 -55
  105. package/node_package/lib/pro/transformRSCStreamAndReplayConsoleLogs.d.ts +0 -15
  106. package/node_package/lib/pro/transformRSCStreamAndReplayConsoleLogs.js +0 -82
  107. package/node_package/lib/pro/wrapServerComponentRenderer/client.d.ts +0 -22
  108. package/node_package/lib/pro/wrapServerComponentRenderer/client.js +0 -79
  109. package/node_package/lib/pro/wrapServerComponentRenderer/server.d.ts +0 -22
  110. package/node_package/lib/pro/wrapServerComponentRenderer/server.js +0 -58
  111. package/node_package/lib/pro/wrapServerComponentRenderer/server.rsc.d.ts +0 -2
  112. package/node_package/lib/pro/wrapServerComponentRenderer/server.rsc.js +0 -18
  113. package/node_package/lib/reactApis.d.cts +0 -11
  114. package/node_package/lib/tsconfig.tsbuildinfo +0 -1
  115. package/node_package/lib/utils.d.ts +0 -25
  116. package/node_package/lib/utils.js +0 -42
@@ -1,3 +1,4 @@
1
1
  import type { AuthenticityHeaders } from './types/index.ts';
2
2
  export declare function authenticityToken(): string | null;
3
3
  export declare const authenticityHeaders: (otherHeaders?: Record<string, string>) => AuthenticityHeaders;
4
+ //# sourceMappingURL=Authenticity.d.ts.map
@@ -9,3 +9,4 @@ export const authenticityHeaders = (otherHeaders = {}) => Object.assign(otherHea
9
9
  'X-CSRF-Token': authenticityToken(),
10
10
  'X-Requested-With': 'XMLHttpRequest',
11
11
  });
12
+ //# sourceMappingURL=Authenticity.js.map
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Render a single component by its DOM ID.
3
+ * This is the main entry point for rendering individual components.
4
+ * @public
5
+ */
6
+ export declare function renderComponent(domId: string): void;
7
+ /**
8
+ * Render all components on the page.
9
+ * Core package renders all components after page load.
10
+ */
11
+ export declare function renderAllComponents(): void;
12
+ /**
13
+ * Public API function that can be called to render a component after it has been loaded.
14
+ * This is the function that should be exported and used by the Rails integration.
15
+ * Returns a Promise for API compatibility with pro version.
16
+ */
17
+ export declare function reactOnRailsComponentLoaded(domId: string): Promise<void>;
18
+ //# sourceMappingURL=ClientRenderer.d.ts.map
@@ -0,0 +1,155 @@
1
+ import ComponentRegistry from "./ComponentRegistry.js";
2
+ import StoreRegistry from "./StoreRegistry.js";
3
+ import createReactOutput from "./createReactOutput.js";
4
+ import reactHydrateOrRender from "./reactHydrateOrRender.js";
5
+ import { getRailsContext } from "./context.js";
6
+ import { isServerRenderHash } from "./isServerRenderResult.js";
7
+ import { onPageUnloaded } from "./pageLifecycle.js";
8
+ import { supportsRootApi, unmountComponentAtNode } from "./reactApis.cjs";
9
+ const REACT_ON_RAILS_STORE_ATTRIBUTE = 'data-js-react-on-rails-store';
10
+ // Track all rendered roots for cleanup
11
+ const renderedRoots = new Map();
12
+ function initializeStore(el, railsContext) {
13
+ const name = el.getAttribute(REACT_ON_RAILS_STORE_ATTRIBUTE) || '';
14
+ const props = el.textContent !== null ? JSON.parse(el.textContent) : {};
15
+ const storeGenerator = StoreRegistry.getStoreGenerator(name);
16
+ const store = storeGenerator(props, railsContext);
17
+ StoreRegistry.setStore(name, store);
18
+ }
19
+ function forEachStore(railsContext) {
20
+ const els = document.querySelectorAll(`[${REACT_ON_RAILS_STORE_ATTRIBUTE}]`);
21
+ for (let i = 0; i < els.length; i += 1) {
22
+ initializeStore(els[i], railsContext);
23
+ }
24
+ }
25
+ function domNodeIdForEl(el) {
26
+ return el.getAttribute('data-dom-id') || '';
27
+ }
28
+ function delegateToRenderer(componentObj, props, railsContext, domNodeId, trace) {
29
+ const { name, component, isRenderer } = componentObj;
30
+ if (isRenderer) {
31
+ if (trace) {
32
+ console.log(`\
33
+ DELEGATING TO RENDERER ${name} for dom node with id: ${domNodeId} with props, railsContext:`, props, railsContext);
34
+ }
35
+ // Call the renderer function with the expected signature
36
+ component(props, railsContext, domNodeId);
37
+ return true;
38
+ }
39
+ return false;
40
+ }
41
+ /**
42
+ * Used for client rendering by ReactOnRails. Either calls ReactDOM.hydrate, ReactDOM.render, or
43
+ * delegates to a renderer registered by the user.
44
+ */
45
+ function renderElement(el, railsContext) {
46
+ // This must match lib/react_on_rails/helper.rb
47
+ const name = el.getAttribute('data-component-name') || '';
48
+ const domNodeId = domNodeIdForEl(el);
49
+ const props = el.textContent !== null ? JSON.parse(el.textContent) : {};
50
+ const trace = el.getAttribute('data-trace') === 'true';
51
+ try {
52
+ const domNode = document.getElementById(domNodeId);
53
+ if (domNode) {
54
+ const componentObj = ComponentRegistry.get(name);
55
+ if (delegateToRenderer(componentObj, props, railsContext, domNodeId, trace)) {
56
+ return;
57
+ }
58
+ // Hydrate if available and was server rendered
59
+ const shouldHydrate = !!domNode.innerHTML;
60
+ const reactElementOrRouterResult = createReactOutput({
61
+ componentObj,
62
+ props,
63
+ domNodeId,
64
+ trace,
65
+ railsContext,
66
+ shouldHydrate,
67
+ });
68
+ if (isServerRenderHash(reactElementOrRouterResult)) {
69
+ throw new Error(`\
70
+ You returned a server side type of react-router error: ${JSON.stringify(reactElementOrRouterResult)}
71
+ You should return a React.Component always for the client side entry point.`);
72
+ }
73
+ else {
74
+ const root = reactHydrateOrRender(domNode, reactElementOrRouterResult, shouldHydrate);
75
+ // Track the root for cleanup
76
+ renderedRoots.set(domNodeId, { root, domNode });
77
+ }
78
+ }
79
+ }
80
+ catch (e) {
81
+ const error = e;
82
+ console.error(error.message);
83
+ error.message = `ReactOnRails encountered an error while rendering component: ${name}. See above error message.`;
84
+ throw error;
85
+ }
86
+ }
87
+ /**
88
+ * Render a single component by its DOM ID.
89
+ * This is the main entry point for rendering individual components.
90
+ * @public
91
+ */
92
+ export function renderComponent(domId) {
93
+ const railsContext = getRailsContext();
94
+ // If no react on rails context
95
+ if (!railsContext)
96
+ return;
97
+ // Initialize stores first
98
+ forEachStore(railsContext);
99
+ // Find the element with the matching data-dom-id
100
+ const el = document.querySelector(`[data-dom-id="${domId}"]`);
101
+ if (!el)
102
+ return;
103
+ renderElement(el, railsContext);
104
+ }
105
+ /**
106
+ * Render all components on the page.
107
+ * Core package renders all components after page load.
108
+ */
109
+ export function renderAllComponents() {
110
+ const railsContext = getRailsContext();
111
+ if (!railsContext)
112
+ return;
113
+ // Initialize all stores first
114
+ forEachStore(railsContext);
115
+ // Render all components
116
+ const componentElements = document.querySelectorAll('.js-react-on-rails-component');
117
+ for (let i = 0; i < componentElements.length; i += 1) {
118
+ renderElement(componentElements[i], railsContext);
119
+ }
120
+ }
121
+ /**
122
+ * Public API function that can be called to render a component after it has been loaded.
123
+ * This is the function that should be exported and used by the Rails integration.
124
+ * Returns a Promise for API compatibility with pro version.
125
+ */
126
+ export function reactOnRailsComponentLoaded(domId) {
127
+ renderComponent(domId);
128
+ return Promise.resolve();
129
+ }
130
+ /**
131
+ * Unmount all rendered React components and clear roots.
132
+ * This should be called on page unload to prevent memory leaks.
133
+ */
134
+ function unmountAllComponents() {
135
+ renderedRoots.forEach(({ root, domNode }) => {
136
+ try {
137
+ if (supportsRootApi && root && typeof root === 'object' && 'unmount' in root) {
138
+ // React 18+ Root API
139
+ root.unmount();
140
+ }
141
+ else {
142
+ // React 16-17 legacy API
143
+ // eslint-disable-next-line @typescript-eslint/no-deprecated
144
+ unmountComponentAtNode(domNode);
145
+ }
146
+ }
147
+ catch (error) {
148
+ console.error('Error unmounting component:', error);
149
+ }
150
+ });
151
+ renderedRoots.clear();
152
+ }
153
+ // Register cleanup on page unload
154
+ onPageUnloaded(unmountAllComponents);
155
+ //# sourceMappingURL=ClientRenderer.js.map
@@ -0,0 +1,31 @@
1
+ import type { RegisteredComponent, ReactComponentOrRenderFunction } from './types/index.ts';
2
+ declare const _default: {
3
+ /**
4
+ * @param components { component1: component1, component2: component2, etc. }
5
+ */
6
+ register(components: Record<string, ReactComponentOrRenderFunction>): void;
7
+ /**
8
+ * @param name
9
+ * @returns { name, component, renderFunction, isRenderer }
10
+ */
11
+ get(name: string): RegisteredComponent;
12
+ /**
13
+ * Get a Map containing all registered components. Useful for debugging.
14
+ * @returns Map where key is the component name and values are the
15
+ * { name, component, renderFunction, isRenderer}
16
+ */
17
+ components(): Map<string, RegisteredComponent>;
18
+ /**
19
+ * Pro-only method that waits for component registration
20
+ * @param _name Component name to wait for
21
+ * @throws Always throws error indicating pro package is required
22
+ */
23
+ getOrWaitForComponent(_name: string): never;
24
+ /**
25
+ * Clear all registered components (for testing purposes)
26
+ * @private
27
+ */
28
+ clear(): void;
29
+ };
30
+ export default _default;
31
+ //# sourceMappingURL=ComponentRegistry.d.ts.map
@@ -0,0 +1,64 @@
1
+ import isRenderFunction from "./isRenderFunction.js";
2
+ const registeredComponents = new Map();
3
+ export default {
4
+ /**
5
+ * @param components { component1: component1, component2: component2, etc. }
6
+ */
7
+ register(components) {
8
+ Object.keys(components).forEach((name) => {
9
+ if (registeredComponents.has(name)) {
10
+ console.warn('Called register for component that is already registered', name);
11
+ }
12
+ const component = components[name];
13
+ if (!component) {
14
+ throw new Error(`Called register with null component named ${name}`);
15
+ }
16
+ const renderFunction = isRenderFunction(component);
17
+ const isRenderer = renderFunction && component.length === 3;
18
+ registeredComponents.set(name, {
19
+ name,
20
+ component,
21
+ renderFunction,
22
+ isRenderer,
23
+ });
24
+ });
25
+ },
26
+ /**
27
+ * @param name
28
+ * @returns { name, component, renderFunction, isRenderer }
29
+ */
30
+ get(name) {
31
+ const registeredComponent = registeredComponents.get(name);
32
+ if (registeredComponent !== undefined) {
33
+ return registeredComponent;
34
+ }
35
+ const keys = Array.from(registeredComponents.keys()).join(', ');
36
+ throw new Error(`Could not find component registered with name ${name}. \
37
+ Registered component names include [ ${keys} ]. Maybe you forgot to register the component?`);
38
+ },
39
+ /**
40
+ * Get a Map containing all registered components. Useful for debugging.
41
+ * @returns Map where key is the component name and values are the
42
+ * { name, component, renderFunction, isRenderer}
43
+ */
44
+ components() {
45
+ return registeredComponents;
46
+ },
47
+ /**
48
+ * Pro-only method that waits for component registration
49
+ * @param _name Component name to wait for
50
+ * @throws Always throws error indicating pro package is required
51
+ */
52
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
53
+ getOrWaitForComponent(_name) {
54
+ throw new Error('getOrWaitForComponent requires react-on-rails-pro package');
55
+ },
56
+ /**
57
+ * Clear all registered components (for testing purposes)
58
+ * @private
59
+ */
60
+ clear() {
61
+ registeredComponents.clear();
62
+ },
63
+ };
64
+ //# sourceMappingURL=ComponentRegistry.js.map
@@ -7,3 +7,4 @@ exports.renderToString = exports.renderToPipeableStream = void 0;
7
7
  var server_1 = require("react-dom/server");
8
8
  Object.defineProperty(exports, "renderToPipeableStream", { enumerable: true, get: function () { return server_1.renderToPipeableStream; } });
9
9
  Object.defineProperty(exports, "renderToString", { enumerable: true, get: function () { return server_1.renderToString; } });
10
+ //# sourceMappingURL=ReactDOMServer.cjs.map
@@ -0,0 +1,4 @@
1
+ declare const ReactOnRails: import("./types/index.ts").ReactOnRailsInternal;
2
+ export * from './types/index.ts';
3
+ export default ReactOnRails;
4
+ //# sourceMappingURL=ReactOnRails.client.d.ts.map
@@ -0,0 +1,7 @@
1
+ import { createBaseClientObject } from "./base/client.js";
2
+ import createReactOnRails from "./createReactOnRails.js";
3
+ const currentGlobal = globalThis.ReactOnRails || null;
4
+ const ReactOnRails = createReactOnRails(createBaseClientObject, currentGlobal);
5
+ export * from "./types/index.js";
6
+ export default ReactOnRails;
7
+ //# sourceMappingURL=ReactOnRails.client.js.map
@@ -0,0 +1,4 @@
1
+ declare const ReactOnRails: import("./types/index.ts").ReactOnRailsInternal;
2
+ export * from './types/index.ts';
3
+ export default ReactOnRails;
4
+ //# sourceMappingURL=ReactOnRails.full.d.ts.map
@@ -0,0 +1,7 @@
1
+ import { createBaseFullObject } from "./base/full.js";
2
+ import createReactOnRails from "./createReactOnRails.js";
3
+ const currentGlobal = globalThis.ReactOnRails || null;
4
+ const ReactOnRails = createReactOnRails(createBaseFullObject, currentGlobal);
5
+ export * from "./types/index.js";
6
+ export default ReactOnRails;
7
+ //# sourceMappingURL=ReactOnRails.full.js.map
@@ -1 +1,2 @@
1
1
  export declare function wrapInScriptTags(scriptId: string, scriptBody: string): string;
2
+ //# sourceMappingURL=RenderUtils.d.ts.map
@@ -8,3 +8,4 @@ export function wrapInScriptTags(scriptId, scriptBody) {
8
8
  ${scriptBody}
9
9
  </script>`;
10
10
  }
11
+ //# sourceMappingURL=RenderUtils.js.map
@@ -0,0 +1,58 @@
1
+ import type { Store, StoreGenerator } from './types/index.ts';
2
+ declare const _default: {
3
+ /**
4
+ * Register a store generator, a function that takes props and returns a store.
5
+ * @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
6
+ */
7
+ register(storeGenerators: Record<string, StoreGenerator>): void;
8
+ /**
9
+ * Used by components to get the hydrated store which contains props.
10
+ * @param name
11
+ * @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
12
+ * there is no store with the given name.
13
+ * @returns Redux Store, possibly hydrated
14
+ */
15
+ getStore(name: string, throwIfMissing?: boolean): Store | undefined;
16
+ /**
17
+ * Internally used function to get the store creator that was passed to `register`.
18
+ * @param name
19
+ * @returns storeCreator with given name
20
+ */
21
+ getStoreGenerator(name: string): StoreGenerator;
22
+ /**
23
+ * Internally used function to set the hydrated store after a Rails page is loaded.
24
+ * @param name
25
+ * @param store (not the storeGenerator, but the hydrated store)
26
+ */
27
+ setStore(name: string, store: Store): void;
28
+ /**
29
+ * Internally used function to completely clear hydratedStores Map.
30
+ */
31
+ clearHydratedStores(): void;
32
+ /**
33
+ * Get a Map containing all registered store generators. Useful for debugging.
34
+ * @returns Map where key is the component name and values are the store generators.
35
+ */
36
+ storeGenerators(): Map<string, StoreGenerator>;
37
+ /**
38
+ * Get a Map containing all hydrated stores. Useful for debugging.
39
+ * @returns Map where key is the component name and values are the hydrated stores.
40
+ */
41
+ stores(): Map<string, Store>;
42
+ /**
43
+ * Get a store by name, or wait for it to be registered.
44
+ * This is a Pro-only feature that requires React on Rails Pro.
45
+ * @param name
46
+ * @throws Error indicating this is a Pro-only feature
47
+ */
48
+ getOrWaitForStore(name: string): never;
49
+ /**
50
+ * Get a store generator by name, or wait for it to be registered.
51
+ * This is a Pro-only feature that requires React on Rails Pro.
52
+ * @param name
53
+ * @throws Error indicating this is a Pro-only feature
54
+ */
55
+ getOrWaitForStoreGenerator(name: string): never;
56
+ };
57
+ export default _default;
58
+ //# sourceMappingURL=StoreRegistry.d.ts.map
@@ -0,0 +1,113 @@
1
+ const registeredStoreGenerators = new Map();
2
+ const hydratedStores = new Map();
3
+ export default {
4
+ /**
5
+ * Register a store generator, a function that takes props and returns a store.
6
+ * @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
7
+ */
8
+ register(storeGenerators) {
9
+ Object.keys(storeGenerators).forEach((name) => {
10
+ if (registeredStoreGenerators.has(name)) {
11
+ console.warn('Called registerStore for store that is already registered', name);
12
+ }
13
+ const store = storeGenerators[name];
14
+ if (!store) {
15
+ throw new Error('Called ReactOnRails.registerStores with a null or undefined as a value ' +
16
+ `for the store generator with key ${name}.`);
17
+ }
18
+ registeredStoreGenerators.set(name, store);
19
+ });
20
+ },
21
+ /**
22
+ * Used by components to get the hydrated store which contains props.
23
+ * @param name
24
+ * @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
25
+ * there is no store with the given name.
26
+ * @returns Redux Store, possibly hydrated
27
+ */
28
+ getStore(name, throwIfMissing = true) {
29
+ if (hydratedStores.has(name)) {
30
+ return hydratedStores.get(name);
31
+ }
32
+ const storeKeys = Array.from(hydratedStores.keys()).join(', ');
33
+ if (storeKeys.length === 0) {
34
+ const msg = `There are no stores hydrated and you are requesting the store ${name}.
35
+ This can happen if you are server rendering and either:
36
+ 1. You do not call redux_store near the top of your controller action's view (not the layout)
37
+ and before any call to react_component.
38
+ 2. You do not render redux_store_hydration_data anywhere on your page.`;
39
+ throw new Error(msg);
40
+ }
41
+ if (throwIfMissing) {
42
+ console.log('storeKeys', storeKeys);
43
+ throw new Error(`Could not find hydrated store with name '${name}'. ` +
44
+ `Hydrated store names include [${storeKeys}].`);
45
+ }
46
+ return undefined;
47
+ },
48
+ /**
49
+ * Internally used function to get the store creator that was passed to `register`.
50
+ * @param name
51
+ * @returns storeCreator with given name
52
+ */
53
+ getStoreGenerator(name) {
54
+ const registeredStoreGenerator = registeredStoreGenerators.get(name);
55
+ if (registeredStoreGenerator) {
56
+ return registeredStoreGenerator;
57
+ }
58
+ const storeKeys = Array.from(registeredStoreGenerators.keys()).join(', ');
59
+ throw new Error(`Could not find store registered with name '${name}'. Registered store ` +
60
+ `names include [ ${storeKeys} ]. Maybe you forgot to register the store?`);
61
+ },
62
+ /**
63
+ * Internally used function to set the hydrated store after a Rails page is loaded.
64
+ * @param name
65
+ * @param store (not the storeGenerator, but the hydrated store)
66
+ */
67
+ setStore(name, store) {
68
+ hydratedStores.set(name, store);
69
+ },
70
+ /**
71
+ * Internally used function to completely clear hydratedStores Map.
72
+ */
73
+ clearHydratedStores() {
74
+ hydratedStores.clear();
75
+ },
76
+ /**
77
+ * Get a Map containing all registered store generators. Useful for debugging.
78
+ * @returns Map where key is the component name and values are the store generators.
79
+ */
80
+ storeGenerators() {
81
+ return registeredStoreGenerators;
82
+ },
83
+ /**
84
+ * Get a Map containing all hydrated stores. Useful for debugging.
85
+ * @returns Map where key is the component name and values are the hydrated stores.
86
+ */
87
+ stores() {
88
+ return hydratedStores;
89
+ },
90
+ /**
91
+ * Get a store by name, or wait for it to be registered.
92
+ * This is a Pro-only feature that requires React on Rails Pro.
93
+ * @param name
94
+ * @throws Error indicating this is a Pro-only feature
95
+ */
96
+ getOrWaitForStore(name) {
97
+ throw new Error(`getOrWaitForStore('${name}') is only available with React on Rails Pro. ` +
98
+ 'Please upgrade to React on Rails Pro or use the synchronous getStore() method instead. ' +
99
+ 'See https://www.shakacode.com/react-on-rails-pro/ for more information.');
100
+ },
101
+ /**
102
+ * Get a store generator by name, or wait for it to be registered.
103
+ * This is a Pro-only feature that requires React on Rails Pro.
104
+ * @param name
105
+ * @throws Error indicating this is a Pro-only feature
106
+ */
107
+ getOrWaitForStoreGenerator(name) {
108
+ throw new Error(`getOrWaitForStoreGenerator('${name}') is only available with React on Rails Pro. ` +
109
+ 'Please upgrade to React on Rails Pro or use the synchronous getStoreGenerator() method instead. ' +
110
+ 'See https://www.shakacode.com/react-on-rails-pro/ for more information.');
111
+ },
112
+ };
113
+ //# sourceMappingURL=StoreRegistry.js.map
@@ -0,0 +1,25 @@
1
+ import type { RegisteredComponent, ReactComponentOrRenderFunction, Store, StoreGenerator, ReactOnRailsInternal } from '../types/index.ts';
2
+ interface Registries {
3
+ ComponentRegistry: {
4
+ register: (components: Record<string, ReactComponentOrRenderFunction>) => void;
5
+ get: (name: string) => RegisteredComponent;
6
+ components: () => Map<string, RegisteredComponent>;
7
+ };
8
+ StoreRegistry: {
9
+ register: (storeGenerators: Record<string, StoreGenerator>) => void;
10
+ getStore: (name: string, throwIfMissing?: boolean) => Store | undefined;
11
+ getStoreGenerator: (name: string) => StoreGenerator;
12
+ setStore: (name: string, store: Store) => void;
13
+ clearHydratedStores: () => void;
14
+ storeGenerators: () => Map<string, StoreGenerator>;
15
+ stores: () => Map<string, Store>;
16
+ };
17
+ }
18
+ /**
19
+ * Base client object type that includes all core ReactOnRails methods except Pro-specific ones.
20
+ * Derived from ReactOnRailsInternal by omitting Pro-only methods.
21
+ */
22
+ export type BaseClientObjectType = Omit<ReactOnRailsInternal, 'getOrWaitForComponent' | 'getOrWaitForStore' | 'getOrWaitForStoreGenerator' | 'reactOnRailsStoreLoaded' | 'streamServerRenderedReactComponent' | 'serverRenderRSCReactComponent'>;
23
+ export declare function createBaseClientObject(registries: Registries, currentObject?: BaseClientObjectType | null): BaseClientObjectType;
24
+ export {};
25
+ //# sourceMappingURL=client.d.ts.map