Skip to content

Commit 37994aa

Browse files
committed
Separate options file
1 parent fe7c97c commit 37994aa

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

node_package/src/ReactOnRails.client.ts

+4-36
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import type {
1313
AuthenticityHeaders,
1414
Store,
1515
StoreGenerator,
16-
ReactOnRailsOptions,
1716
} from './types/index.ts';
1817
import reactHydrateOrRenderInternal from './reactHydrateOrRender.ts';
18+
import { resetOptions } from './options.ts';
1919

2020
export { default as buildConsoleReplay } from './buildConsoleReplay.ts';
2121

@@ -25,22 +25,17 @@ declare global {
2525
/* eslint-enable no-var,vars-on-top,no-underscore-dangle */
2626
}
2727

28+
// eslint-disable-next-line no-underscore-dangle
2829
if (globalThis.__REACT_ON_RAILS_LOADED__) {
2930
throw new Error(`\
3031
The ReactOnRails value exists in the ${globalThis} scope, it may not be safe to overwrite it.
3132
This could be caused by setting Webpack's optimization.runtimeChunk to "true" or "multiple," rather than "single."
3233
Check your Webpack configuration. Read more at https://github.com/shakacode/react_on_rails/issues/1558.`);
3334
}
3435

36+
// eslint-disable-next-line no-underscore-dangle
3537
globalThis.__REACT_ON_RAILS_LOADED__ = true;
3638

37-
const DEFAULT_OPTIONS = {
38-
traceTurbolinks: false,
39-
turbo: false,
40-
};
41-
42-
let options: ReactOnRailsOptions = {};
43-
4439
// TODO: convert to re-exports if everything works fine
4540
export function register(components: Record<string, ReactComponentOrRenderFunction>): void {
4641
ComponentRegistry.register(components);
@@ -83,26 +78,6 @@ export function reactHydrateOrRender(
8378
return reactHydrateOrRenderInternal(domNode, reactElement, hydrate);
8479
}
8580

86-
export function setOptions(newOptions: Partial<ReactOnRailsOptions>): void {
87-
if (typeof newOptions.traceTurbolinks !== 'undefined') {
88-
options.traceTurbolinks = newOptions.traceTurbolinks;
89-
90-
// eslint-disable-next-line no-param-reassign
91-
delete newOptions.traceTurbolinks;
92-
}
93-
94-
if (typeof newOptions.turbo !== 'undefined') {
95-
options.turbo = newOptions.turbo;
96-
97-
// eslint-disable-next-line no-param-reassign
98-
delete newOptions.turbo;
99-
}
100-
101-
if (Object.keys(newOptions).length > 0) {
102-
throw new Error(`Invalid options passed to ReactOnRails.options: ${JSON.stringify(newOptions)}`);
103-
}
104-
}
105-
10681
export function reactOnRailsPageLoaded() {
10782
return ClientStartup.reactOnRailsPageLoaded();
10883
}
@@ -127,10 +102,6 @@ export function authenticityHeaders(otherHeaders: Record<string, string> = {}):
127102
// INTERNALLY USED APIs
128103
// /////////////////////////////////////////////////////////////////////////////
129104

130-
export function option<K extends keyof ReactOnRailsOptions>(key: K): ReactOnRailsOptions[K] | undefined {
131-
return options[key];
132-
}
133-
134105
export function getStoreGenerator(name: string): StoreGenerator {
135106
return StoreRegistry.getStoreGenerator(name);
136107
}
@@ -201,12 +172,9 @@ export function stores(): Map<string, Store> {
201172
return StoreRegistry.stores();
202173
}
203174

204-
export function resetOptions(): void {
205-
options = { ...DEFAULT_OPTIONS };
206-
}
207-
208175
resetOptions();
209176

210177
ClientStartup.clientStartup();
211178

212179
export * from './types/index.ts';
180+
export * from './options.ts';

node_package/src/options.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { ReactOnRailsOptions } from './types/index.ts';
2+
3+
const DEFAULT_OPTIONS = {
4+
traceTurbolinks: false,
5+
turbo: false,
6+
};
7+
8+
let options: ReactOnRailsOptions = {};
9+
10+
export function setOptions(newOptions: Partial<ReactOnRailsOptions>): void {
11+
if (typeof newOptions.traceTurbolinks !== 'undefined') {
12+
options.traceTurbolinks = newOptions.traceTurbolinks;
13+
14+
// eslint-disable-next-line no-param-reassign
15+
delete newOptions.traceTurbolinks;
16+
}
17+
18+
if (typeof newOptions.turbo !== 'undefined') {
19+
options.turbo = newOptions.turbo;
20+
21+
// eslint-disable-next-line no-param-reassign
22+
delete newOptions.turbo;
23+
}
24+
25+
if (Object.keys(newOptions).length > 0) {
26+
throw new Error(`Invalid options passed to ReactOnRails.options: ${JSON.stringify(newOptions)}`);
27+
}
28+
}
29+
30+
export function option<K extends keyof ReactOnRailsOptions>(key: K): ReactOnRailsOptions[K] | undefined {
31+
return options[key];
32+
}
33+
34+
export function resetOptions(): void {
35+
options = { ...DEFAULT_OPTIONS };
36+
}

node_package/src/turbolinksUtils.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// TODO: move option to a separate file to avoid circular dependencies
2-
import { option } from './ReactOnRails.client';
1+
import { option } from './options.ts';
32

43
declare global {
54
namespace Turbolinks {

0 commit comments

Comments
 (0)