-
-
Notifications
You must be signed in to change notification settings - Fork 638
Fix TypeScript issues #1715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix TypeScript issues #1715
Changes from 7 commits
04ce85a
c885def
4b7ccb4
ac50e94
c2f2fc7
2c1f4f3
f2c83de
c00e52f
48d03d1
cc24282
f403473
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||
| /* eslint-disable max-classes-per-file */ | ||||||||
| /* eslint-disable react/no-deprecated -- while we need to support React 16 */ | ||||||||
| /* eslint-disable react/no-deprecated,@typescript-eslint/no-deprecated -- while we need to support React 16 */ | ||||||||
|
|
||||||||
| import * as ReactDOM from 'react-dom'; | ||||||||
| import type { ReactElement } from 'react'; | ||||||||
|
|
@@ -14,13 +14,13 @@ import { debugTurbolinks } from './turbolinksUtils'; | |||||||
|
|
||||||||
| const REACT_ON_RAILS_STORE_ATTRIBUTE = 'data-js-react-on-rails-store'; | ||||||||
|
|
||||||||
| function delegateToRenderer( | ||||||||
| async function delegateToRenderer( | ||||||||
| componentObj: RegisteredComponent, | ||||||||
| props: Record<string, string>, | ||||||||
| props: Record<string, unknown>, | ||||||||
| railsContext: RailsContext, | ||||||||
| domNodeId: string, | ||||||||
| trace: boolean, | ||||||||
| ): boolean { | ||||||||
| ): Promise<boolean> { | ||||||||
| const { name, component, isRenderer } = componentObj; | ||||||||
|
|
||||||||
| if (isRenderer) { | ||||||||
|
|
@@ -32,7 +32,7 @@ function delegateToRenderer( | |||||||
| ); | ||||||||
| } | ||||||||
|
|
||||||||
| (component as RenderFunction)(props, railsContext, domNodeId); | ||||||||
| await (component as RenderFunction)(props, railsContext, domNodeId); | ||||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -81,7 +81,7 @@ class ComponentRenderer { | |||||||
| // This must match lib/react_on_rails/helper.rb | ||||||||
| const name = el.getAttribute('data-component-name') || ''; | ||||||||
| const { domNodeId } = this; | ||||||||
| const props = el.textContent !== null ? JSON.parse(el.textContent) : {}; | ||||||||
| const props = el.textContent !== null ? (JSON.parse(el.textContent) as Record<string, unknown>) : {}; | ||||||||
| const trace = el.getAttribute('data-trace') === 'true'; | ||||||||
|
|
||||||||
| try { | ||||||||
|
|
@@ -92,7 +92,7 @@ class ComponentRenderer { | |||||||
| return; | ||||||||
| } | ||||||||
|
|
||||||||
| if (delegateToRenderer(componentObj, props, railsContext, domNodeId, trace)) { | ||||||||
| if (await delegateToRenderer(componentObj, props, railsContext, domNodeId, trace)) { | ||||||||
|
||||||||
| if (await delegateToRenderer(componentObj, props, railsContext, domNodeId, trace)) { | |
| // Abort if component unmounted during awaiting the async operation | |
| if (await delegateToRenderer(componentObj, props, railsContext, domNodeId, trace) || this.state === 'unmounted') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,9 @@ import { debugTurbolinks } from './turbolinksUtils'; | |
|
|
||
| export async function reactOnRailsPageLoaded() { | ||
| debugTurbolinks('reactOnRailsPageLoaded'); | ||
| await Promise.all([hydrateAllStores(), renderOrHydrateAllComponents()]); | ||
| const promise = hydrateAllStores(); | ||
| renderOrHydrateAllComponents(); | ||
|
||
| await promise; | ||
| } | ||
|
|
||
| function reactOnRailsPageUnloaded(): void { | ||
|
|
@@ -36,9 +38,9 @@ export async function clientStartup(context: Context): Promise<void> { | |
|
|
||
| // force loaded components and stores are rendered and hydrated immediately | ||
| renderOrHydrateForceLoadedComponents(); | ||
| hydrateForceLoadedStores(); | ||
| await hydrateForceLoadedStores(); | ||
|
||
|
|
||
| // Other components and stores are rendered and hydrated when the page is fully loaded | ||
| onPageLoaded(reactOnRailsPageLoaded); | ||
| onPageUnloaded(reactOnRailsPageUnloaded); | ||
| await onPageLoaded(reactOnRailsPageLoaded); | ||
| await onPageUnloaded(reactOnRailsPageUnloaded); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,24 @@ | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs/promises'; | ||
|
|
||
| const loadedReactClientManifests = new Map<string, Record<string, unknown>>(); | ||
| type ClientManifest = Record<string, unknown>; | ||
| const loadedReactClientManifests = new Map<string, ClientManifest>(); | ||
|
|
||
| export default async function loadReactClientManifest(reactClientManifestFileName: string) { | ||
| // React client manifest is uploaded to node renderer as an asset. | ||
| // Renderer copies assets to the same place as the server-bundle.js and rsc-bundle.js. | ||
| // Thus, the __dirname of this code is where we can find the manifest file. | ||
| const manifestPath = path.resolve(__dirname, reactClientManifestFileName); | ||
| if (!loadedReactClientManifests.has(manifestPath)) { | ||
| // TODO: convert to async | ||
| try { | ||
| const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8')); | ||
| loadedReactClientManifests.set(manifestPath, manifest); | ||
| } catch (error) { | ||
| throw new Error(`Failed to load React client manifest from ${manifestPath}: ${error}`); | ||
| } | ||
| const loadedReactClientManifest = loadedReactClientManifests.get(manifestPath); | ||
| if (loadedReactClientManifest) { | ||
| return loadedReactClientManifest; | ||
| } | ||
|
|
||
| return loadedReactClientManifests.get(manifestPath)!; | ||
| try { | ||
| const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8')) as ClientManifest; | ||
| loadedReactClientManifests.set(manifestPath, manifest); | ||
| return manifest; | ||
| } catch (error) { | ||
| throw new Error(`Failed to load React client manifest from ${manifestPath}: ${error}`); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.