Skip to content

Commit bac9316

Browse files
committed
Simplify getRailsContext
1 parent d337826 commit bac9316

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

node_package/src/CallbackRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class CallbackRegistry<T> {
4747
};
4848

4949
onPageLoaded(() => {
50-
const registryTimeout = getRailsContext().railsContext?.componentRegistryTimeout;
50+
const registryTimeout = getRailsContext()?.componentRegistryTimeout;
5151
if (!registryTimeout) return;
5252

5353
timeoutId = setTimeout(triggerTimeout, registryTimeout);

node_package/src/ClientSideRenderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ComponentRenderer {
6161
const storeDependencies = el.getAttribute('data-store-dependencies');
6262
const storeDependenciesArray = storeDependencies ? (JSON.parse(storeDependencies) as string[]) : [];
6363

64-
const { railsContext } = getRailsContext();
64+
const railsContext = getRailsContext();
6565
if (!railsContext) return;
6666

6767
// Wait for all store dependencies to be loaded
@@ -181,7 +181,7 @@ class StoreRenderer {
181181

182182
constructor(storeDataElement: Element) {
183183
this.state = 'hydrating';
184-
const { railsContext } = getRailsContext();
184+
const railsContext = getRailsContext();
185185
if (!railsContext) {
186186
return;
187187
}

node_package/src/context.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@ let currentRailsContext: RailsContext | null = null;
1111

1212
// caches context and railsContext to avoid re-parsing rails-context each time a component is rendered
1313
// Cached values will be reset when resetRailsContext() is called
14-
export function getRailsContext(): { railsContext: RailsContext | null } {
14+
export function getRailsContext(): RailsContext | null {
1515
// Return cached values if already set
1616
if (currentRailsContext) {
17-
return { railsContext: currentRailsContext };
17+
return currentRailsContext;
1818
}
1919

2020
const el = document.getElementById('js-react-on-rails-context');
2121
if (!el?.textContent) {
22-
return { railsContext: null };
22+
return null;
2323
}
2424

2525
try {
2626
currentRailsContext = JSON.parse(el.textContent) as RailsContext;
27+
return currentRailsContext;
2728
} catch (e) {
2829
console.error('Error parsing Rails context:', e);
29-
return { railsContext: null };
30+
return null;
3031
}
31-
32-
return { railsContext: currentRailsContext };
3332
}
3433

3534
export function resetRailsContext(): void {

node_package/tests/ComponentRegistry.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jest.mock('../src/pageLifecycle', () => ({
2323
}));
2424

2525
jest.mock('../src/context', () => ({
26-
getRailsContext: () => ({ railsContext: { componentRegistryTimeout: 100 } }),
26+
getRailsContext: () => ({ componentRegistryTimeout: 100 }),
2727
}));
2828

2929
describe('ComponentRegistry', () => {

0 commit comments

Comments
 (0)