Skip to content

Replace context with globalThis #1727

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

Merged
merged 4 commits into from
Apr 10, 2025
Merged

Replace context with globalThis #1727

merged 4 commits into from
Apr 10, 2025

Conversation

alexeyr-ci2
Copy link
Collaborator

@alexeyr-ci2 alexeyr-ci2 commented Apr 9, 2025

Summary

Replace context() function with globalThis.

Pull Request checklist

  • [ ] Add/update test to cover these changes
  • [ ] Update documentation
  • Update CHANGELOG file

This change is Reviewable

Summary by CodeRabbit

  • Documentation

    • Updated release notes to explain the adoption of a modern, standardized approach for accessing the global environment.
    • Revised changelog highlights a configuration update—switching from a deprecated option to a new one—with guidance for users needing compatibility adjustments.
  • Refactor

    • Streamlined global context handling throughout the system to improve consistency and simplify initialization.
    • Simplified method signatures and logic for context management, enhancing clarity and efficiency.

Copy link
Contributor

coderabbitai bot commented Apr 9, 2025

Walkthrough

This pull request standardizes global context management by replacing legacy context retrieval methods with the modern globalThis reference. Documentation has been updated to reflect the new approach and deprecate outdated configuration options. Multiple source files have been refactored to remove combined context objects, update import statements, and adjust function signatures. Test mocks were also updated to align with these modifications, ensuring that all components now consistently use the new global context handling.

Changes

File(s) Change Summary
CHANGELOG.md
docs/release-notes/15.0.0.md
Added new sections documenting the use of globalThis for accessing global variables and noted the deprecation of the defer_generated_component_packs option in favor of generated_component_packs_loading_strategy.
node_package/src/CallbackRegistry.ts
node_package/src/ClientSideRenderer.ts
node_package/src/ReactOnRails.client.ts
node_package/src/clientStartup.ts
node_package/src/turbolinksUtils.ts
Updated context handling by removing legacy combined context objects and functions. Replaced getContextAndRailsContext with getRailsContext, removed local context variables, and modified method signatures and imports to directly utilize globalThis.
node_package/src/context.ts Refactored context retrieval logic: removed legacy Window interface and globalThis namespace encapsulation, eliminated the Context type and associated functions, and streamlined the retrieval to return only RailsContext with getRailsContext and resetRailsContext.
node_package/tests/ComponentRegistry.test.js Updated mock implementation to replace getContextAndRailsContext with getRailsContext, simplifying the mocked return value for testing purposes.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Renderer as ClientSideRenderer/StoreRenderer
    participant GlobalRails as globalThis.ReactOnRails

    App->>Renderer: Initiate rendering/hydration
    Renderer->>GlobalRails: Request store using getOrWaitForStore / getOrWaitForStoreGenerator
    GlobalRails-->>Renderer: Return store instance
    Renderer->>GlobalRails: Reset context via resetRailsContext
Loading

Possibly related PRs

  • Add configurable loading strategy for generated component packs #1712: The changes regarding the deprecation of defer_generated_component_packs and the introduction of generated_component_packs_loading_strategy are directly related to the changes in this PR.
  • Reduce bundle size #1697: The changes in this PR regarding the introduction of globalThis for accessing the global context are related to modifications in ReactOnRails.client.ts.
  • Fix TypeScript issues #1715: The changes regarding the introduction of globalThis for accessing global variables are related to updates in the handling of context in various files.

Suggested reviewers

  • AbanoubGhadban
  • Judahmeek

Poem

I'm a little rabbit, hopping with glee,
Global context now sets my code free!
Old paths retired, new trends in sight,
globalThis shines with modern light.
Hop along, code friends, with joy so bright! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b50fac9 and 150f5da.

📒 Files selected for processing (9)
  • CHANGELOG.md (1 hunks)
  • docs/release-notes/15.0.0.md (1 hunks)
  • node_package/src/CallbackRegistry.ts (2 hunks)
  • node_package/src/ClientSideRenderer.ts (6 hunks)
  • node_package/src/ReactOnRails.client.ts (2 hunks)
  • node_package/src/clientStartup.ts (2 hunks)
  • node_package/src/context.ts (1 hunks)
  • node_package/src/turbolinksUtils.ts (2 hunks)
  • node_package/tests/ComponentRegistry.test.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • node_package/tests/ComponentRegistry.test.js
  • docs/release-notes/15.0.0.md
  • node_package/src/CallbackRegistry.ts
  • CHANGELOG.md
  • node_package/src/turbolinksUtils.ts
🧰 Additional context used
🧬 Code Graph Analysis (2)
node_package/src/context.ts (1)
node_package/src/types/index.ts (3)
  • ReactOnRails (209-273)
  • ReactOnRailsInternal (276-379)
  • RailsContext (20-38)
node_package/src/ClientSideRenderer.ts (2)
node_package/src/context.ts (2)
  • getRailsContext (14-32)
  • resetRailsContext (34-36)
node_package/src/types/index.ts (1)
  • RailsContext (20-38)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: build
  • GitHub Check: rspec-package-tests (newest)
🔇 Additional comments (26)
node_package/src/context.ts (6)

1-1: Import type updated appropriately

The import type has been updated from ReactOnRailsType to ReactOnRailsInternal to reflect the new approach of using the global scope.


3-8: Clean global declarations with proper ESLint handling

The global variable declarations have been properly moved to the global scope with appropriate ESLint directives to handle known linting issues. This aligns with the PR objective of using globalThis instead of context functions.


13-14: Function renamed to reflect its actual purpose

The function has been renamed from getContextAndRailsContext to getRailsContext to better reflect its purpose now that it only returns the Rails context. This is a more accurate name and improves code clarity.


16-23: Simplified conditional logic for railsContext retrieval

The function now directly returns the cached context if available or attempts to retrieve it from the DOM. The removal of additional context-related logic makes this more straightforward.


25-31: Proper error handling maintained

The error handling for JSON parsing has been preserved while simplifying the return value to directly return null on error, which is consistent with the function's new signature.


34-36: Function renamed for consistency

The reset function has been renamed from resetContextAndRailsContext to resetRailsContext to match the new function name pattern and its reduced scope.

node_package/src/clientStartup.ts (4)

1-7: Reordered imports for better organization

The imports have been reordered to better group related functionality.


21-21: Simplified function signature by removing context parameter

The clientStartup function no longer requires a context parameter, aligning with the PR's objective of using globalThis instead of passing context objects.


23-25: Improved server-side rendering detection

The code now directly checks for globalThis.document being undefined to detect server-side rendering, which is more straightforward than the previous check.


27-34: Updated event handler flag management

The code now uses globalThis.__REACT_ON_RAILS_EVENT_HANDLERS_RAN_ONCE__ to check and set the flag, which is consistent with the global approach being adopted throughout the codebase.

node_package/src/ReactOnRails.client.ts (4)

21-26: Updated guard clause to use globalThis

The check for existing ReactOnRails instance now properly uses globalThis.ReactOnRails instead of checking a local context variable, with an appropriately updated error message.


33-33: ReactOnRails now assigned to globalThis

ReactOnRails is now directly assigned to globalThis instead of to a local context variable, aligning with the PR objective.


193-195: Updated option reset and startup call

The option reset and startup calls now use globalThis.ReactOnRails and no longer pass a context parameter to clientStartup(), consistent with the other changes.


198-198: Updated default export to use globalThis

The default export now correctly references globalThis.ReactOnRails, maintaining consistency with the other changes.

node_package/src/ClientSideRenderer.ts (12)

8-8: Updated context imports

The imports have been updated to use the new function names getRailsContext and resetRailsContext, reflecting the changes in the context module.


14-15: Added direct imports for registry modules

Direct imports for StoreRegistry and ComponentRegistry have been added, replacing the previous approach of accessing these through the context.


66-67: Simplified railsContext retrieval in ComponentRenderer

The code now directly retrieves and checks only the railsContext, removing the unnecessary context object.


71-71: Updated store dependency resolution

Store dependencies are now resolved directly through the StoreRegistry instead of through the context.


74-74: Updated render call to remove context parameter

The render method call no longer passes a context parameter, consistent with the updated method signature.


82-82: Simplified render method signature

The render method signature has been simplified to only accept the element and railsContext parameters, removing the unnecessary context parameter.


92-92: Direct ComponentRegistry usage

The component is now retrieved directly from ComponentRegistry instead of through the context.


186-189: Simplified railsContext retrieval in StoreRenderer

The StoreRenderer now directly retrieves and checks only the railsContext, removing the unnecessary context object.


196-196: Updated hydrate call to remove context parameter

The hydrate method call no longer passes a context parameter, consistent with the updated method signature.


199-200: Simplified hydrate method and store generator retrieval

The hydrate method signature has been simplified to remove the context parameter, and the store generator is now retrieved directly from StoreRegistry.


206-206: Direct StoreRegistry usage

The store is now set directly in StoreRegistry instead of through the context.


252-252: Updated reset function call

The function call has been updated from resetContextAndRailsContext() to resetRailsContext() to match the renamed function.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@alexeyr-ci alexeyr-ci force-pushed the alexeyr/globalThis branch from 038ba7e to 9b94ce0 Compare April 9, 2025 18:17
@alexeyr-ci2 alexeyr-ci2 marked this pull request as ready for review April 9, 2025 19:23
@alexeyr-ci2
Copy link
Collaborator Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Apr 9, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@alexeyr-ci alexeyr-ci force-pushed the alexeyr/globalThis branch from 838394a to ea8f289 Compare April 9, 2025 22:12
@alexeyr-ci2 alexeyr-ci2 merged commit a0a4c1c into master Apr 10, 2025
11 checks passed
@alexeyr-ci2 alexeyr-ci2 deleted the alexeyr/globalThis branch April 10, 2025 10:00
@coderabbitai coderabbitai bot mentioned this pull request Apr 18, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants