Skip to content

Commit 2b692cc

Browse files
alexeyr-ci2alexeyr
andauthored
Convert some default exports to named ones (#1717)
Co-authored-by: Alexey Romanov <[email protected]>
1 parent 12c11ae commit 2b692cc

12 files changed

+162
-187
lines changed

node_package/src/Authenticity.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import type { AuthenticityHeaders } from './types/index';
22

3-
export default {
4-
authenticityToken(): string | null {
5-
const token = document.querySelector('meta[name="csrf-token"]');
6-
if (token instanceof HTMLMetaElement) {
7-
return token.content;
8-
}
9-
return null;
10-
},
3+
export function authenticityToken(): string | null {
4+
const token = document.querySelector('meta[name="csrf-token"]');
5+
if (token instanceof HTMLMetaElement) {
6+
return token.content;
7+
}
8+
return null;
9+
}
1110

12-
authenticityHeaders(otherHeaders: Record<string, string> = {}): AuthenticityHeaders {
13-
return Object.assign(otherHeaders, {
14-
'X-CSRF-Token': this.authenticityToken(),
15-
'X-Requested-With': 'XMLHttpRequest',
16-
});
17-
},
18-
};
11+
export const authenticityHeaders = (otherHeaders: Record<string, string> = {}): AuthenticityHeaders =>
12+
Object.assign(otherHeaders, {
13+
'X-CSRF-Token': authenticityToken(),
14+
'X-Requested-With': 'XMLHttpRequest',
15+
});

node_package/src/ComponentRegistry.ts

+45-51
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,49 @@ import CallbackRegistry from './CallbackRegistry';
44

55
const componentRegistry = new CallbackRegistry<RegisteredComponent>('component');
66

7-
export default {
8-
/**
9-
* @param components { component1: component1, component2: component2, etc. }
10-
*/
11-
register(components: Record<string, ReactComponentOrRenderFunction>): void {
12-
Object.keys(components).forEach((name) => {
13-
if (componentRegistry.has(name)) {
14-
console.warn('Called register for component that is already registered', name);
15-
}
16-
17-
const component = components[name];
18-
if (!component) {
19-
throw new Error(`Called register with null component named ${name}`);
20-
}
21-
22-
const renderFunction = isRenderFunction(component);
23-
const isRenderer = renderFunction && component.length === 3;
24-
25-
componentRegistry.set(name, {
26-
name,
27-
component,
28-
renderFunction,
29-
isRenderer,
30-
});
7+
/**
8+
* @param components { component1: component1, component2: component2, etc. }
9+
*/
10+
export function register(components: Record<string, ReactComponentOrRenderFunction>): void {
11+
Object.keys(components).forEach((name) => {
12+
if (componentRegistry.has(name)) {
13+
console.warn('Called register for component that is already registered', name);
14+
}
15+
16+
const component = components[name];
17+
if (!component) {
18+
throw new Error(`Called register with null component named ${name}`);
19+
}
20+
21+
const renderFunction = isRenderFunction(component);
22+
const isRenderer = renderFunction && component.length === 3;
23+
24+
componentRegistry.set(name, {
25+
name,
26+
component,
27+
renderFunction,
28+
isRenderer,
3129
});
32-
},
33-
34-
/**
35-
* @param name
36-
* @returns { name, component, isRenderFunction, isRenderer }
37-
*/
38-
get(name: string): RegisteredComponent {
39-
return componentRegistry.get(name);
40-
},
41-
42-
getOrWaitForComponent(name: string): Promise<RegisteredComponent> {
43-
return componentRegistry.getOrWaitForItem(name);
44-
},
45-
46-
/**
47-
* Get a Map containing all registered components. Useful for debugging.
48-
* @returns Map where key is the component name and values are the
49-
* { name, component, renderFunction, isRenderer}
50-
*/
51-
components(): Map<string, RegisteredComponent> {
52-
return componentRegistry.getAll();
53-
},
54-
55-
clear(): void {
56-
componentRegistry.clear();
57-
},
58-
};
30+
});
31+
}
32+
33+
/**
34+
* @param name
35+
* @returns { name, component, isRenderFunction, isRenderer }
36+
*/
37+
export const get = (name: string): RegisteredComponent => componentRegistry.get(name);
38+
39+
export const getOrWaitForComponent = (name: string): Promise<RegisteredComponent> =>
40+
componentRegistry.getOrWaitForItem(name);
41+
42+
/**
43+
* Get a Map containing all registered components. Useful for debugging.
44+
* @returns Map where key is the component name and values are the
45+
* { name, component, renderFunction, isRenderer}
46+
*/
47+
export const components = (): Map<string, RegisteredComponent> => componentRegistry.getAll();
48+
49+
/** @internal Exported only for tests */
50+
export function clear(): void {
51+
componentRegistry.clear();
52+
}

node_package/src/ReactOnRails.client.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ReactElement } from 'react';
22
import * as ClientStartup from './clientStartup';
33
import { renderOrHydrateComponent, hydrateStore } from './ClientSideRenderer';
4-
import ComponentRegistry from './ComponentRegistry';
5-
import StoreRegistry from './StoreRegistry';
4+
import * as ComponentRegistry from './ComponentRegistry';
5+
import * as StoreRegistry from './StoreRegistry';
66
import buildConsoleReplay from './buildConsoleReplay';
77
import createReactOutput from './createReactOutput';
8-
import Authenticity from './Authenticity';
8+
import * as Authenticity from './Authenticity';
99
import context from './context';
1010
import type {
1111
RegisteredComponent,

node_package/src/RenderUtils.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export default {
2-
wrapInScriptTags(scriptId: string, scriptBody: string): string {
3-
if (!scriptBody) {
4-
return '';
5-
}
1+
// eslint-disable-next-line import/prefer-default-export -- only one export for now, but others may be added later
2+
export function wrapInScriptTags(scriptId: string, scriptBody: string): string {
3+
if (!scriptBody) {
4+
return '';
5+
}
66

7-
return `\n<script id="${scriptId}">
7+
return `
8+
<script id="${scriptId}">
89
${scriptBody}
910
</script>`;
10-
},
11-
};
11+
}

node_package/src/StoreRegistry.ts

+83-93
Original file line numberDiff line numberDiff line change
@@ -4,112 +4,102 @@ import type { Store, StoreGenerator } from './types';
44
const storeGeneratorRegistry = new CallbackRegistry<StoreGenerator>('store generator');
55
const hydratedStoreRegistry = new CallbackRegistry<Store>('hydrated store');
66

7-
export default {
8-
/**
9-
* Register a store generator, a function that takes props and returns a store.
10-
* @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
11-
*/
12-
register(storeGenerators: Record<string, StoreGenerator>): void {
13-
Object.keys(storeGenerators).forEach((name) => {
14-
if (storeGeneratorRegistry.has(name)) {
15-
console.warn('Called registerStore for store that is already registered', name);
16-
}
7+
/**
8+
* Register a store generator, a function that takes props and returns a store.
9+
* @param storeGenerators { name1: storeGenerator1, name2: storeGenerator2 }
10+
*/
11+
export function register(storeGenerators: Record<string, StoreGenerator>): void {
12+
Object.keys(storeGenerators).forEach((name) => {
13+
if (storeGeneratorRegistry.has(name)) {
14+
console.warn('Called registerStore for store that is already registered', name);
15+
}
1716

18-
const store = storeGenerators[name];
19-
if (!store) {
20-
throw new Error(
21-
'Called ReactOnRails.registerStores with a null or undefined as a value ' +
22-
`for the store generator with key ${name}.`,
23-
);
24-
}
17+
const storeGenerator = storeGenerators[name];
18+
if (!storeGenerator) {
19+
throw new Error(
20+
'Called ReactOnRails.registerStoreGenerators with a null or undefined as a value ' +
21+
`for the store generator with key ${name}.`,
22+
);
23+
}
2524

26-
storeGeneratorRegistry.set(name, store);
27-
});
28-
},
25+
storeGeneratorRegistry.set(name, storeGenerator);
26+
});
27+
}
2928

30-
/**
31-
* Used by components to get the hydrated store which contains props.
32-
* @param name
33-
* @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
34-
* there is no store with the given name.
35-
* @returns Redux Store, possibly hydrated
36-
*/
37-
getStore(name: string, throwIfMissing = true): Store | undefined {
38-
try {
39-
return hydratedStoreRegistry.get(name);
40-
} catch (error) {
41-
if (hydratedStoreRegistry.getAll().size === 0) {
42-
const msg = `There are no stores hydrated and you are requesting the store ${name}.
29+
/**
30+
* Used by components to get the hydrated store which contains props.
31+
* @param name
32+
* @param throwIfMissing Defaults to true. Set to false to have this call return undefined if
33+
* there is no store with the given name.
34+
* @returns Redux Store, possibly hydrated
35+
*/
36+
export function getStore(name: string, throwIfMissing = true): Store | undefined {
37+
try {
38+
return hydratedStoreRegistry.get(name);
39+
} catch (error) {
40+
if (hydratedStoreRegistry.getAll().size === 0) {
41+
const msg = `There are no stores hydrated and you are requesting the store ${name}.
4342
This can happen if you are server rendering and either:
4443
1. You do not call redux_store near the top of your controller action's view (not the layout)
4544
and before any call to react_component.
4645
2. You do not render redux_store_hydration_data anywhere on your page.`;
47-
throw new Error(msg);
48-
}
46+
throw new Error(msg);
47+
}
4948

50-
if (throwIfMissing) {
51-
throw error;
52-
}
53-
return undefined;
49+
if (throwIfMissing) {
50+
throw error;
5451
}
55-
},
52+
return undefined;
53+
}
54+
}
5655

57-
/**
58-
* Internally used function to get the store creator that was passed to `register`.
59-
* @param name
60-
* @returns storeCreator with given name
61-
*/
62-
getStoreGenerator(name: string): StoreGenerator {
63-
return storeGeneratorRegistry.get(name);
64-
},
56+
/**
57+
* Internally used function to get the store creator that was passed to `register`.
58+
* @param name
59+
* @returns storeCreator with given name
60+
*/
61+
export const getStoreGenerator = (name: string): StoreGenerator => storeGeneratorRegistry.get(name);
6562

66-
/**
67-
* Internally used function to set the hydrated store after a Rails page is loaded.
68-
* @param name
69-
* @param store (not the storeGenerator, but the hydrated store)
70-
*/
71-
setStore(name: string, store: Store): void {
72-
hydratedStoreRegistry.set(name, store);
73-
},
63+
/**
64+
* Internally used function to set the hydrated store after a Rails page is loaded.
65+
* @param name
66+
* @param store (not the storeGenerator, but the hydrated store)
67+
*/
68+
export function setStore(name: string, store: Store): void {
69+
hydratedStoreRegistry.set(name, store);
70+
}
7471

75-
/**
76-
* Internally used function to completely clear hydratedStores Map.
77-
*/
78-
clearHydratedStores(): void {
79-
hydratedStoreRegistry.clear();
80-
},
72+
/**
73+
* Internally used function to completely clear hydratedStores Map.
74+
*/
75+
export function clearHydratedStores(): void {
76+
hydratedStoreRegistry.clear();
77+
}
8178

82-
/**
83-
* Get a Map containing all registered store generators. Useful for debugging.
84-
* @returns Map where key is the component name and values are the store generators.
85-
*/
86-
storeGenerators(): Map<string, StoreGenerator> {
87-
return storeGeneratorRegistry.getAll();
88-
},
79+
/**
80+
* Get a Map containing all registered store generators. Useful for debugging.
81+
* @returns Map where key is the component name and values are the store generators.
82+
*/
83+
export const storeGenerators = (): Map<string, StoreGenerator> => storeGeneratorRegistry.getAll();
8984

90-
/**
91-
* Get a Map containing all hydrated stores. Useful for debugging.
92-
* @returns Map where key is the component name and values are the hydrated stores.
93-
*/
94-
stores(): Map<string, Store> {
95-
return hydratedStoreRegistry.getAll();
96-
},
85+
/**
86+
* Get a Map containing all hydrated stores. Useful for debugging.
87+
* @returns Map where key is the component name and values are the hydrated stores.
88+
*/
89+
export const stores = (): Map<string, Store> => hydratedStoreRegistry.getAll();
9790

98-
/**
99-
* Used by components to get the hydrated store, waiting for it to be hydrated if necessary.
100-
* @param name Name of the store to wait for
101-
* @returns Promise that resolves with the Store once hydrated
102-
*/
103-
getOrWaitForStore(name: string): Promise<Store> {
104-
return hydratedStoreRegistry.getOrWaitForItem(name);
105-
},
91+
/**
92+
* Used by components to get the hydrated store, waiting for it to be hydrated if necessary.
93+
* @param name Name of the store to wait for
94+
* @returns Promise that resolves with the Store once hydrated
95+
*/
96+
export const getOrWaitForStore = (name: string): Promise<Store> =>
97+
hydratedStoreRegistry.getOrWaitForItem(name);
10698

107-
/**
108-
* Used by components to get the store generator, waiting for it to be registered if necessary.
109-
* @param name Name of the store generator to wait for
110-
* @returns Promise that resolves with the StoreGenerator once registered
111-
*/
112-
getOrWaitForStoreGenerator(name: string): Promise<StoreGenerator> {
113-
return storeGeneratorRegistry.getOrWaitForItem(name);
114-
},
115-
};
99+
/**
100+
* Used by components to get the store generator, waiting for it to be registered if necessary.
101+
* @param name Name of the store generator to wait for
102+
* @returns Promise that resolves with the StoreGenerator once registered
103+
*/
104+
export const getOrWaitForStoreGenerator = (name: string): Promise<StoreGenerator> =>
105+
storeGeneratorRegistry.getOrWaitForItem(name);

node_package/src/buildConsoleReplay.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import RenderUtils from './RenderUtils';
1+
import { wrapInScriptTags } from './RenderUtils';
22
import scriptSanitizedVal from './scriptSanitizedVal';
33

44
declare global {
@@ -54,8 +54,5 @@ export default function buildConsoleReplay(
5454
customConsoleHistory: (typeof console)['history'] | undefined = undefined,
5555
numberOfMessagesToSkip: number = 0,
5656
): string {
57-
return RenderUtils.wrapInScriptTags(
58-
'consoleReplayLog',
59-
consoleReplay(customConsoleHistory, numberOfMessagesToSkip),
60-
);
57+
return wrapInScriptTags('consoleReplayLog', consoleReplay(customConsoleHistory, numberOfMessagesToSkip));
6158
}

node_package/src/serverRenderReactComponent.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import * as ReactDOMServer from 'react-dom/server';
33
import type { ReactElement } from 'react';
44

5-
import ComponentRegistry from './ComponentRegistry';
5+
import * as ComponentRegistry from './ComponentRegistry';
66
import createReactOutput from './createReactOutput';
77
import { isPromise, isServerRenderHash } from './isServerRenderResult';
88
import buildConsoleReplay from './buildConsoleReplay';
@@ -144,10 +144,7 @@ function serverRenderReactComponentInternal(options: RenderParams): null | strin
144144
throwJsErrors,
145145
} = options;
146146

147-
let renderState: RenderState = {
148-
result: null,
149-
hasErrors: false,
150-
};
147+
let renderState: RenderState;
151148

152149
try {
153150
const componentObj = ComponentRegistry.get(componentName);

0 commit comments

Comments
 (0)