Skip to content

feat(common): use the hotspot function provided by the ArkWeb JS engine to optimize the execution of defineProperties function in the common adaptation layer #3530

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 1 commit into from
Jun 24, 2025

Conversation

zzcr
Copy link
Member

@zzcr zzcr commented Jun 24, 2025

使用ArkWeb JS引擎提供的热点函数,优化common适配层defineProperties函数执行

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Improved property delegation in Vue 2, Vue 2.7, and Vue 3 adapters, enabling more flexible and filtered property handling when certain conditions are met.
  • Chores

    • Updated TypeScript configuration files to include new type declarations and improved formatting for better maintainability.
  • Style

    • Reformatted TypeScript configuration files for consistency and readability.

…ne to optimize the execution of defineProperties function in the common adaptation layer
Copy link

coderabbitai bot commented Jun 24, 2025

Walkthrough

A new TypeScript declaration for an interface ILongque and related property filter flags was added, along with a global window extension. An adapter module was introduced to provide a conditional delegate (__TINY__). The defineProperties logic in Vue 2, 2.7, and 3 adapters was refactored to use this delegate when available. TypeScript config files were updated for formatting and to include the new declaration.

Changes

File(s) Change Summary
packages/vue-common/src/longque.d.ts Added ILongque interface, property filter flags, and extended global Window interface.
packages/vue-common/src/adapter/longque.ts Added module exporting __TINY__, referencing window.__Longque__ if available, else null.
packages/vue-common/src/adapter/vue2/index.ts
packages/vue-common/src/adapter/vue2.7/index.ts
packages/vue-common/src/adapter/vue3/index.ts
Refactored defineProperties to use a new delegate-based approach with __TINY__ when available; original logic renamed and preserved.
tsconfig.vue2.json
tsconfig.vue2.7.json
tsconfig.vue3.json
Reformatted arrays, added "packages/vue-common/src/__longque__.d.ts" to include, no semantic changes.

Sequence Diagram(s)

sequenceDiagram
    participant VM as vm (proxy)
    participant Instance as instance (source)
    participant TINY as __TINY__ (delegate)
    alt __TINY__ available
        VM->>TINY: createDelegate(instance, vm, filterFlags)
        TINY-->>VM: Delegated properties assigned
    else
        VM->>Instance: Manual property definition (filtered)
        Instance-->>VM: Properties assigned
    end
Loading

Poem

In the meadow of code, a new friend appears,
Longque the delegate, allaying old fears.
With flags and with filters, it hops into view,
Lending its paws to Vue’s clever crew.
Now properties leap, with a magical spring—
Oh, what delight these new changes bring!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/vue-common/src/__longque__.d.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-vue".

(The package "eslint-plugin-vue" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-vue@latest --save-dev

The plugin "eslint-plugin-vue" was referenced from the config file in ".eslintrc.js » @antfu/eslint-config » @antfu/eslint-config-vue".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

packages/vue-common/src/adapter/__longque__.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-vue".

(The package "eslint-plugin-vue" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-vue@latest --save-dev

The plugin "eslint-plugin-vue" was referenced from the config file in ".eslintrc.js » @antfu/eslint-config » @antfu/eslint-config-vue".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

packages/vue-common/src/adapter/vue2/index.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-vue".

(The package "eslint-plugin-vue" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-vue@latest --save-dev

The plugin "eslint-plugin-vue" was referenced from the config file in ".eslintrc.js » @antfu/eslint-config » @antfu/eslint-config-vue".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

  • 2 others
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

@github-actions github-actions bot added the enhancement New feature or request (功能增强) label Jun 24, 2025
@zzcr zzcr merged commit 6691034 into dev Jun 24, 2025
11 checks passed
@zzcr zzcr deleted the zzc/add-longque-0624 branch June 24, 2025 07:25
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.

Actionable comments posted: 3

♻️ Duplicate comments (4)
packages/vue-common/src/adapter/vue3/index.ts (1)

16-16: Same dependency verification needed as Vue 2 adapter.

This has the same dependency concerns as the Vue 2 adapter regarding the ../__longque__ module.

packages/vue-common/src/adapter/vue2.7/index.ts (3)

16-16: Same dependency verification needed.

This has the same dependency concerns as the other adapters regarding the ../__longque__ module.


202-205: Apply the same error handling as Vue 2 adapter.

The harmonyDefineProperties function needs the same error handling improvements suggested for the Vue 2 adapter.


207-207: Apply the same runtime validation as Vue 2 adapter.

The conditional assignment needs the same runtime validation improvements suggested for the Vue 2 adapter.

🧹 Nitpick comments (3)
packages/vue-common/src/__longque__.d.ts (1)

19-20: Loosen the object return/argument types

Using bare object inhibits access to properties without additional casts.
Consider something like Record<string, unknown> (or a generic) for better ergonomics:

-  createDelegate: (underlyingObject: object, initObject?: object, propertyFilterFlags?: number) => object
+  createDelegate: <T extends object, D extends object = {}>(
+    underlyingObject: T,
+    initObject?: D,
+    propertyFilterFlags?: number
+  ) => T & D

This keeps type information of both objects and avoids unsafe casts downstream.

packages/vue-common/src/adapter/__longque__.ts (1)

1-4: Harden the environment check & add typing

window only exists in browsers; using globalThis covers both browsers and workers.
Adding an explicit type eliminates any leakage.

-import { __TINY__ }
+import type { ILongque } from '../__longque__'

-const __TINY__ = typeof window === 'undefined' ? null : window.__Longque__
+const __TINY__: ILongque | null =
+  typeof globalThis !== 'undefined' && '__Longque__' in globalThis
+    ? (globalThis as any).__Longque__ as ILongque
+    : null
+
+export { __TINY__ }
packages/vue-common/src/adapter/vue3/index.ts (1)

262-264: Document the selective optimization strategy.

The selective application of optimization (using originalDefineProperties for 'setupState' and 'props', but conditional defineProperties for 'ctx') suggests specific compatibility requirements. Please document why this distinction is necessary.

Add a comment explaining the selective optimization:

 const defineInstanceVm = (vm, instance) => {
+  // Use original implementation for setupState and props to ensure compatibility
   originalDefineProperties(vm, instance, 'setupState', null)
   originalDefineProperties(vm, instance, 'props', filter)
+  // Use optimized delegation for ctx when available
   defineProperties(vm, instance, 'ctx', filter)

   return vm
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a602aa5 and bb3bdb9.

📒 Files selected for processing (8)
  • packages/vue-common/src/__longque__.d.ts (1 hunks)
  • packages/vue-common/src/adapter/__longque__.ts (1 hunks)
  • packages/vue-common/src/adapter/vue2.7/index.ts (3 hunks)
  • packages/vue-common/src/adapter/vue2/index.ts (3 hunks)
  • packages/vue-common/src/adapter/vue3/index.ts (3 hunks)
  • tsconfig.vue2.7.json (2 hunks)
  • tsconfig.vue2.json (2 hunks)
  • tsconfig.vue3.json (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*`: 用于写产品需求和开发文档的文件,需求应尽量写得简单清晰易懂。 在需求设计阶段应遵从逐步迭代的原则,早期版本可以尽量简洁。

**/*: 用于写产品需求和开发文档的文件,需求应尽量写得简单清晰易懂。
在需求设计阶段应遵从逐步迭代的原则,早期版本可以尽量简洁。

  • tsconfig.vue3.json
  • tsconfig.vue2.7.json
  • tsconfig.vue2.json
  • packages/vue-common/src/adapter/__longque__.ts
  • packages/vue-common/src/__longque__.d.ts
  • packages/vue-common/src/adapter/vue3/index.ts
  • packages/vue-common/src/adapter/vue2.7/index.ts
  • packages/vue-common/src/adapter/vue2/index.ts
🔇 Additional comments (6)
tsconfig.vue3.json (1)

28-29: Path looks correct – ensure publish script bundles it

Adding the declaration to include is necessary and looks fine.
Double-check that the build/publish pipeline copies packages/vue-common/src/__longque__.d.ts into the final artifacts.

tsconfig.vue2.7.json (1)

28-29: Include entry added – LGTM

Same remark as for the Vue 3 config: verify the file ends up in the distributed typings.

tsconfig.vue2.json (1)

28-29: Typings path added – no issues

Nothing further to flag.

packages/vue-common/src/adapter/vue2/index.ts (1)

6-6: ```shell
#!/bin/bash

Display longque adapter module source and type definitions

echo "---- packages/vue-common/src/adapter/longque.ts ----"
sed -n '1,200p' packages/vue-common/src/adapter/longque.ts
echo
echo "---- packages/vue-common/src/longque.d.ts ----"
sed -n '1,200p' packages/vue-common/src/longque.d.ts


</details>
<details>
<summary>packages/vue-common/src/adapter/vue3/index.ts (1)</summary>

`252-255`: ```shell
#!/bin/bash
# Search for propertyFilterFlags definitions in both Vue2 and Vue3 adapters
rg "propertyFilterFlags" -n -C 5

# Search for any originalDefineProperties definitions or references
rg "originalDefineProperties" -n -C 3
packages/vue-common/src/adapter/vue2.7/index.ts (1)

202-207: Good consistency with Vue 2 implementation.

The Vue 2.7 adapter correctly uses the same filter flags and implementation pattern as Vue 2, which maintains consistency across similar Vue versions.

Comment on lines +50 to +52
interface Window {
__Longque__: ILongque
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Make __Longque__ optional to match runtime reality

__Longque__ is not guaranteed to exist – your own runtime guard (typeof window === 'undefined' ? null : window.__Longque__) proves that.
Marking the property as mandatory will break type-checking when strictNullChecks is on, or when the file is imported in non-ArkWeb environments.

-interface Window {
-  __Longque__: ILongque
-}
+interface Window {
+  /** Present only when running inside ArkWeb JS engine */
+  __Longque__?: ILongque
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
interface Window {
__Longque__: ILongque
}
interface Window {
/** Present only when running inside ArkWeb JS engine */
__Longque__?: ILongque
}
🤖 Prompt for AI Agents
In packages/vue-common/src/__longque__.d.ts around lines 50 to 52, the Window
interface declares __Longque__ as a mandatory property, but it is not guaranteed
to exist at runtime. To fix this, make the __Longque__ property optional by
adding a question mark after its name in the interface declaration. This change
will align the type definition with the runtime reality and prevent
type-checking errors when strictNullChecks is enabled or in environments where
__Longque__ is absent.

__TINY__.createDelegate(instance, vm, propertyFilterFlags)
}

const defineProperties = __TINY__ ? harmonyDefineProperties : originalDefineProperties
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider runtime validation of TINY capabilities.

The conditional assignment only checks for the existence of __TINY__ but doesn't validate if it has the required methods. Consider checking for __TINY__.createDelegate specifically.

-const defineProperties = __TINY__ ? harmonyDefineProperties : originalDefineProperties
+const defineProperties = (__TINY__ && typeof __TINY__.createDelegate === 'function') ? harmonyDefineProperties : originalDefineProperties
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const defineProperties = __TINY__ ? harmonyDefineProperties : originalDefineProperties
-const defineProperties = __TINY__ ? harmonyDefineProperties : originalDefineProperties
+const defineProperties = (__TINY__ && typeof __TINY__.createDelegate === 'function')
+ ? harmonyDefineProperties
+ : originalDefineProperties
🤖 Prompt for AI Agents
In packages/vue-common/src/adapter/vue2/index.ts at line 203, the assignment to
defineProperties only checks if __TINY__ exists but does not verify if it has
the necessary method createDelegate. Update the condition to check that __TINY__
is truthy and that __TINY__.createDelegate is a function before assigning
harmonyDefineProperties; otherwise, fallback to originalDefineProperties.

Comment on lines +198 to +201
const harmonyDefineProperties = (vm, instance) => {
const propertyFilterFlags = __TINY__.SKIP_PREFIX_UNDERSCORE | __TINY__.SKIP_PREFIX_DOLLAR | __TINY__.SKIP_CONSTRUCTOR
__TINY__.createDelegate(instance, vm, propertyFilterFlags)
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for the delegate creation.

The harmonyDefineProperties function calls __TINY__.createDelegate without error handling. Consider adding try-catch to gracefully fall back to the original implementation if delegation fails.

 const harmonyDefineProperties = (vm, instance) => {
   const propertyFilterFlags = __TINY__.SKIP_PREFIX_UNDERSCORE | __TINY__.SKIP_PREFIX_DOLLAR | __TINY__.SKIP_CONSTRUCTOR
-  __TINY__.createDelegate(instance, vm, propertyFilterFlags)
+  try {
+    __TINY__.createDelegate(instance, vm, propertyFilterFlags)
+  } catch (error) {
+    console.warn('Failed to create delegate, falling back to original implementation:', error)
+    return originalDefineProperties(vm, instance, filter)
+  }
+  return vm
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In packages/vue-common/src/adapter/vue2/index.ts around lines 198 to 201, the
harmonyDefineProperties function calls __TINY__.createDelegate without error
handling. Wrap the call to __TINY__.createDelegate in a try-catch block, and in
the catch block, implement a fallback to the original property definition method
to ensure graceful degradation if delegation fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request (功能增强)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant