Skip to content

Commit c9319ca

Browse files
committed
Fix promise issues
1 parent 443d1f1 commit c9319ca

File tree

7 files changed

+32
-22
lines changed

7 files changed

+32
-22
lines changed

eslint.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ const config = tsEslint.config([
8080
'function-paren-newline': 'off',
8181
'object-curly-newline': 'off',
8282
'no-restricted-syntax': ['error', 'SequenceExpression'],
83+
'no-void': [
84+
'error',
85+
{
86+
allowAsStatement: true,
87+
},
88+
],
8389

8490
'import/extensions': [
8591
'error',

node_package/src/CallbackRegistry.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class CallbackRegistry<T> {
2929
this.registryType = registryType;
3030
}
3131

32-
private initializeTimeoutEvents() {
32+
private async initializeTimeoutEvents() {
3333
if (this.timeoutEventsInitialized) return;
3434
this.timeoutEventsInitialized = true;
3535

@@ -46,14 +46,14 @@ export default class CallbackRegistry<T> {
4646
});
4747
};
4848

49-
onPageLoaded(() => {
49+
await onPageLoaded(() => {
5050
const registryTimeout = getContextAndRailsContext().railsContext?.componentRegistryTimeout;
5151
if (!registryTimeout) return;
5252

5353
timeoutId = setTimeout(triggerTimeout, registryTimeout);
5454
});
5555

56-
onPageUnloaded(() => {
56+
await onPageUnloaded(() => {
5757
this.waitingPromises.clear();
5858
this.timedout = false;
5959
clearTimeout(timeoutId);
@@ -97,7 +97,7 @@ export default class CallbackRegistry<T> {
9797
}
9898

9999
async getOrWaitForItem(name: string): Promise<T> {
100-
this.initializeTimeoutEvents();
100+
await this.initializeTimeoutEvents();
101101
try {
102102
return this.get(name);
103103
} catch (error) {

node_package/src/ClientSideRenderer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import { debugTurbolinks } from './turbolinksUtils';
1414

1515
const REACT_ON_RAILS_STORE_ATTRIBUTE = 'data-js-react-on-rails-store';
1616

17-
function delegateToRenderer(
17+
async function delegateToRenderer(
1818
componentObj: RegisteredComponent,
1919
props: Record<string, unknown>,
2020
railsContext: RailsContext,
2121
domNodeId: string,
2222
trace: boolean,
23-
): boolean {
23+
): Promise<boolean> {
2424
const { name, component, isRenderer } = componentObj;
2525

2626
if (isRenderer) {
@@ -32,7 +32,7 @@ function delegateToRenderer(
3232
);
3333
}
3434

35-
(component as RenderFunction)(props, railsContext, domNodeId);
35+
await (component as RenderFunction)(props, railsContext, domNodeId);
3636
return true;
3737
}
3838

@@ -92,7 +92,7 @@ class ComponentRenderer {
9292
return;
9393
}
9494

95-
if (delegateToRenderer(componentObj, props, railsContext, domNodeId, trace)) {
95+
if (await delegateToRenderer(componentObj, props, railsContext, domNodeId, trace)) {
9696
return;
9797
}
9898

node_package/src/ReactOnRails.client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ ctx.ReactOnRails = {
155155
renderOrHydrateComponent(domId);
156156
},
157157

158-
reactOnRailsStoreLoaded(storeName: string): void {
159-
hydrateStore(storeName);
158+
async reactOnRailsStoreLoaded(storeName: string): Promise<void> {
159+
await hydrateStore(storeName);
160160
},
161161

162162
/**
@@ -347,7 +347,7 @@ ctx.ReactOnRails = {
347347

348348
ctx.ReactOnRails.resetOptions();
349349

350-
ClientStartup.clientStartup(ctx);
350+
void ClientStartup.clientStartup(ctx);
351351

352352
export * from './types';
353353
export default ctx.ReactOnRails;

node_package/src/clientStartup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export async function clientStartup(context: Context): Promise<void> {
3636

3737
// force loaded components and stores are rendered and hydrated immediately
3838
renderOrHydrateForceLoadedComponents();
39-
hydrateForceLoadedStores();
39+
await hydrateForceLoadedStores();
4040

4141
// Other components and stores are rendered and hydrated when the page is fully loaded
42-
onPageLoaded(reactOnRailsPageLoaded);
43-
onPageUnloaded(reactOnRailsPageUnloaded);
42+
await onPageLoaded(reactOnRailsPageLoaded);
43+
await onPageUnloaded(reactOnRailsPageUnloaded);
4444
}

node_package/src/pageLifecycle.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
turbolinksVersion5,
77
} from './turbolinksUtils';
88

9-
type PageLifecycleCallback = () => void;
9+
type PageLifecycleCallback = () => void | Promise<void>;
1010
type PageState = 'load' | 'unload' | 'initial';
1111

1212
const pageLoadedCallbacks = new Set<PageLifecycleCallback>();
@@ -16,12 +16,16 @@ let currentPageState: PageState = 'initial';
1616

1717
function runPageLoadedCallbacks(): void {
1818
currentPageState = 'load';
19-
pageLoadedCallbacks.forEach((callback) => callback());
19+
pageLoadedCallbacks.forEach((callback) => {
20+
void callback();
21+
});
2022
}
2123

2224
function runPageUnloadedCallbacks(): void {
2325
currentPageState = 'unload';
24-
pageUnloadedCallbacks.forEach((callback) => callback());
26+
pageUnloadedCallbacks.forEach((callback) => {
27+
void callback();
28+
});
2529
}
2630

2731
function setupTurbolinksEventListeners(): void {
@@ -69,17 +73,17 @@ function initializePageEventListeners(): void {
6973
}
7074
}
7175

72-
export function onPageLoaded(callback: PageLifecycleCallback): void {
76+
export async function onPageLoaded(callback: PageLifecycleCallback): Promise<void> {
7377
if (currentPageState === 'load') {
74-
callback();
78+
await callback();
7579
}
7680
pageLoadedCallbacks.add(callback);
7781
initializePageEventListeners();
7882
}
7983

80-
export function onPageUnloaded(callback: PageLifecycleCallback): void {
84+
export async function onPageUnloaded(callback: PageLifecycleCallback): Promise<void> {
8185
if (currentPageState === 'unload') {
82-
callback();
86+
await callback();
8387
}
8488
pageUnloadedCallbacks.add(callback);
8589
initializePageEventListeners();

node_package/src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export interface ReactOnRails {
180180
reactHydrateOrRender(domNode: Element, reactElement: ReactElement, hydrate: boolean): RenderReturnType;
181181
reactOnRailsPageLoaded(): Promise<void>;
182182
reactOnRailsComponentLoaded(domId: string): void;
183-
reactOnRailsStoreLoaded(storeName: string): void;
183+
reactOnRailsStoreLoaded(storeName: string): Promise<void>;
184184
authenticityToken(): string | null;
185185
authenticityHeaders(otherHeaders: Record<string, string>): AuthenticityHeaders;
186186
option(key: string): string | number | boolean | undefined;

0 commit comments

Comments
 (0)