Skip to content
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

Feature: Support for Dynamically Inserted Scripts #15

Merged
merged 3 commits into from
Feb 19, 2024

Conversation

jorenrui
Copy link
Contributor

@jorenrui jorenrui commented Feb 19, 2024

Description

  • Other nodes will wait for the dynamically inserted scripts to be finish before evaluating its attributes.

For example, this list item is dynamically inserted into the DOM on a button click. the two script tags are evaluated first before the other items gets evaluated.

<li>
  <script>
    months = [
      { name: 'April', id: 1 },
      { name: 'May', id: 2 },
      { name: 'June', id: 3 },
    ]
  </script>

  <script>
    choco = 'Test'
  </script>

  <ul :each="month, index in months">
    <li :text="`${month.name}'s months: ${month.id} ${choco}`"></li>
  </ul>
</li>
📦 Published PR as canary version: 1.0.1-canary.15.19aaccb.0

✨ Test out this PR locally via:

npm install tonic-minijs@1.0.1-canary.15.19aaccb.0
# or 
yarn add tonic-minijs@1.0.1-canary.15.19aaccb.0

Summary by CodeRabbit

  • New Features

    • Introduced dynamic script loading capabilities for enhanced flexibility in app behavior.
    • Added a new Observer class to efficiently monitor and respond to changes in the app's DOM.
  • Refactor

    • Streamlined DOM observation by transitioning to a more robust and encapsulated Observer class approach.

@jorenrui jorenrui self-assigned this Feb 19, 2024
@jorenrui jorenrui merged commit 19aaccb into main Feb 19, 2024
1 check passed
@jorenrui jorenrui deleted the jr.feature/wait-dynamically-inserted-scripts branch February 19, 2024 09:28
Copy link

coderabbitai bot commented Feb 19, 2024

Walkthrough

These updates introduce enhanced script handling within the Entity class, a refined approach to DOM observation through a new Observer class, and streamline integration within the main application logic. The focus is on improving dynamic content management and observing changes in the DOM more effectively, enhancing the system's responsiveness and adaptability to changes.

Changes

Files Change Summary
lib/entity.js, lib/generators/observer.js, lib/main.js Added support for dynamic scripts in entities, introduced Observer class for DOM mutation observation, and updated main application logic for new observer pattern.

🐰✨
In the realm of code, where changes abound,
A rabbit hopped in, with ideas profound.
"Let's watch the DOM," it said with a grin,
"And welcome dynamic scripts to join in."
With a flick and a hop, the updates were sown,
In the digital fields where progress is grown.
🌟🐇

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 31150fd and 436e932.
Files selected for processing (3)
  • lib/entity.js (3 hunks)
  • lib/generators/observer.js (2 hunks)
  • lib/main.js (4 hunks)
Additional comments: 9
lib/main.js (4)
  • 3-3: Renaming observeDOM to Observer in the import statement aligns with the introduction of the Observer class, ensuring consistency in naming conventions and improving code readability.
  • 12-12: Creating a new instance of Observer with state as a parameter is a good practice, as it encapsulates the state management within the Observer, enhancing modularity and maintainability of the code.
  • 26-26: Replacing the _listenToDOMChanges function with observer.init() in the init function is a significant improvement. It leverages the new Observer class to manage DOM changes, which likely offers a more structured and efficient approach to handling such changes. However, ensure that all functionalities previously covered by _listenToDOMChanges are adequately handled within observer.init().
Verification successful

The verification process has confirmed the existence of the Observer class and its init method within the codebase, specifically in lib/generators/observer.js. The init method's implementation details, as revealed by the output, indicate a comprehensive approach to observing DOM changes, including handling attribute changes, node removals, and the addition of nodes. It also includes logic for handling dynamic scripts, which suggests a broad coverage of functionalities that might have been part of the _listenToDOMChanges method.

Given the detailed functionalities encapsulated within the observer.init() method, it appears that the migration from _listenToDOMChanges to using observer.init() has been successfully implemented, covering a wide range of DOM observation and interaction capabilities. This includes observing document body changes, processing mutations, handling dynamic scripts, and managing entity lifecycle events.

Without direct access to the original _listenToDOMChanges method, it's challenging to compare every specific functionality one-to-one. However, the comprehensive nature of the init method within the Observer class suggests a robust approach to DOM observation and manipulation, likely encompassing the necessary functionalities previously managed by _listenToDOMChanges.

Based on the information gathered, it seems reasonable to conclude that the transition to using observer.init() has been thoughtfully considered, with an emphasis on maintaining or enhancing the functionality related to DOM changes observation and handling.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all functionalities of `_listenToDOMChanges` are migrated to `observer.init()`.
ast-grep --lang javascript --pattern $'class Observer {
  $$$
  init($_) {
    $$$
  }
  $$$  
}'

Length of output: 98



Script:

#!/bin/bash
# Verify the existence of the Observer class and its init method in the codebase.
ast-grep --lang javascript --pattern $'class Observer {
  $$$
}'

Length of output: 6590


Script:

#!/bin/bash
# Search for the init method within the Observer class more broadly.
rg "init\(" --type js

Length of output: 304

* 100-102: Adding a getter method for `observer` enhances the accessibility of the observer instance from other parts of the application, promoting encapsulation and modularity. This is a positive change, assuming there's a valid use case for accessing the observer externally.
lib/generators/observer.js (2)
  • 1-2: Adding imports for Entity and Events at the beginning of the file is necessary for the new functionalities introduced in the Observer class. These imports ensure that the class has access to the necessary components for its operations.
  • 34-143: The introduction of the Observer class represents a significant enhancement to the library's architecture. It encapsulates the logic for observing and handling mutations in the DOM, which is a more modular and maintainable approach compared to a standalone function. The class methods, such as init, observe, createScriptPromises, waitForScripts, resolveScript, and disconnect, are well-structured and seem to cover the necessary functionalities for managing dynamic script execution and DOM mutations efficiently. However, ensure that the observeDOM function's capabilities are fully integrated into the Observer class and that there are no regressions in functionality.
lib/entity.js (3)
  • 8-11: Adding a dynamicScripts parameter to the Entity constructor and initializing this.dynamicScripts with it is a thoughtful approach to managing dynamic scripts associated with an entity. This change allows entities to be aware of scripts that need to be executed before they can be fully initialized, improving the handling of dynamically inserted scripts in the DOM.
  • 232-234: The modification to the init method to check if the element is a script tag and, if not, wait for scripts specified in dynamicScripts using MiniJS.observer.waitForScripts is a crucial enhancement. It ensures that entities wait for the necessary scripts to execute before initializing, which is essential for the correct functioning of dynamically generated content. This change effectively addresses the core objective of the pull request.
  • 249-249: Passing this.dynamicScripts as an argument when creating new Entity instances within initChildren ensures that the dynamic script handling is propagated to child entities. This is a good practice, as it maintains consistency in how dynamic scripts are managed across the application.

Copy link

🚀 PR was released in v1.0.1 🚀

@github-actions github-actions bot added the released This has been released in npm label Feb 26, 2024
Copy link

🚀 PR was released in v1.0.1 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This has been released in npm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant